blob: 2afdb46f9f8add6bb0edc93dc43f5dde735d604b [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* 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. */
27int 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 */
36void 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) {
Bruce Beare84924902010-10-13 14:21:30 -070041 *(int *)((char *)dst+i) = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042 }
43
44 if (i < size) {
45 int val;
46
47 assert((size - i) < 4);
Bruce Beare84924902010-10-13 14:21:30 -070048 val = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 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 */
58const 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 */
70const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc)
71{
Bruce Beare6cc49232010-10-13 16:11:15 -070072 *rel_pc = pc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 while(mi) {
74 if((pc >= mi->start) && (pc < mi->end)){
75 // Only calculate the relative offset for shared libraries
76 if (strstr(mi->name, ".so")) {
Bruce Beare6cc49232010-10-13 16:11:15 -070077 *rel_pc -= mi->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078 }
79 return mi;
80 }
81 mi = mi->next;
82 }
83 return NULL;
84}