• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /* system/debuggerd/utility.c
2  **
3  ** Copyright 2008, The Android Open Source Project
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17  
18  #include <sys/ptrace.h>
19  #include <sys/exec_elf.h>
20  #include <assert.h>
21  #include <string.h>
22  #include <errno.h>
23  
24  #include "utility.h"
25  
26  /* Get a word from pid using ptrace. The result is the return value. */
get_remote_word(int pid,void * src)27  int get_remote_word(int pid, void *src)
28  {
29      return ptrace(PTRACE_PEEKTEXT, pid, src, NULL);
30  }
31  
32  
33  /* Handy routine to read aggregated data from pid using ptrace. The read
34   * values are written to the dest locations directly.
35   */
get_remote_struct(int pid,void * src,void * dst,size_t size)36  void get_remote_struct(int pid, void *src, void *dst, size_t size)
37  {
38      unsigned int i;
39  
40      for (i = 0; i+4 <= size; i+=4) {
41          *(int *)(dst+i) = ptrace(PTRACE_PEEKTEXT, pid, src+i, NULL);
42      }
43  
44      if (i < size) {
45          int val;
46  
47          assert((size - i) < 4);
48          val = ptrace(PTRACE_PEEKTEXT, pid, src+i, NULL);
49          while (i < size) {
50              ((unsigned char *)dst)[i] = val & 0xff;
51              i++;
52              val >>= 8;
53          }
54      }
55  }
56  
57  /* Map a pc address to the name of the containing ELF file */
map_to_name(mapinfo * mi,unsigned pc,const char * def)58  const char *map_to_name(mapinfo *mi, unsigned pc, const char* def)
59  {
60      while(mi) {
61          if((pc >= mi->start) && (pc < mi->end)){
62              return mi->name;
63          }
64          mi = mi->next;
65      }
66      return def;
67  }
68  
69  /* Find the containing map info for the pc */
pc_to_mapinfo(mapinfo * mi,unsigned pc,unsigned * rel_pc)70  const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc)
71  {
72      while(mi) {
73          if((pc >= mi->start) && (pc < mi->end)){
74              // Only calculate the relative offset for shared libraries
75              if (strstr(mi->name, ".so")) {
76                  *rel_pc = pc - mi->start;
77              }
78              return mi;
79          }
80          mi = mi->next;
81      }
82      return NULL;
83  }
84