blob: 409209c21dacf6c5061c52c3d834ad9beb964167 [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>
Andy McFadden136dcc52011-09-22 16:37:06 -070020#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <assert.h>
22#include <string.h>
23#include <errno.h>
24
25#include "utility.h"
26
27/* Get a word from pid using ptrace. The result is the return value. */
28int get_remote_word(int pid, void *src)
29{
30 return ptrace(PTRACE_PEEKTEXT, pid, src, NULL);
31}
32
33
34/* Handy routine to read aggregated data from pid using ptrace. The read
35 * values are written to the dest locations directly.
36 */
37void get_remote_struct(int pid, void *src, void *dst, size_t size)
38{
39 unsigned int i;
40
41 for (i = 0; i+4 <= size; i+=4) {
Bruce Beare84924902010-10-13 14:21:30 -070042 *(int *)((char *)dst+i) = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043 }
44
45 if (i < size) {
46 int val;
47
48 assert((size - i) < 4);
Bruce Beare84924902010-10-13 14:21:30 -070049 val = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 while (i < size) {
51 ((unsigned char *)dst)[i] = val & 0xff;
52 i++;
53 val >>= 8;
54 }
55 }
56}
57
58/* Map a pc address to the name of the containing ELF file */
59const char *map_to_name(mapinfo *mi, unsigned pc, const char* def)
60{
61 while(mi) {
62 if((pc >= mi->start) && (pc < mi->end)){
63 return mi->name;
64 }
65 mi = mi->next;
66 }
67 return def;
68}
69
70/* Find the containing map info for the pc */
71const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc)
72{
Bruce Beare6cc49232010-10-13 16:11:15 -070073 *rel_pc = pc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 while(mi) {
75 if((pc >= mi->start) && (pc < mi->end)){
76 // Only calculate the relative offset for shared libraries
77 if (strstr(mi->name, ".so")) {
Bruce Beare6cc49232010-10-13 16:11:15 -070078 *rel_pc -= mi->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 }
80 return mi;
81 }
82 mi = mi->next;
83 }
84 return NULL;
85}
Andy McFadden136dcc52011-09-22 16:37:06 -070086
87/*
88 * Returns true if the specified signal has an associated address (i.e. it
89 * sets siginfo_t.si_addr).
90 */
91bool signal_has_address(int sig)
92{
93 switch (sig) {
94 case SIGILL:
95 case SIGFPE:
96 case SIGSEGV:
97 case SIGBUS:
98 return true;
99 default:
100 return false;
101 }
102}