utility.c revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
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 <errno.h>
22
23#include "utility.h"
24
25/* Get a word from pid using ptrace. The result is the return value. */
26int get_remote_word(int pid, void *src)
27{
28    return ptrace(PTRACE_PEEKTEXT, pid, src, NULL);
29}
30
31
32/* Handy routine to read aggregated data from pid using ptrace. The read
33 * values are written to the dest locations directly.
34 */
35void get_remote_struct(int pid, void *src, void *dst, size_t size)
36{
37    unsigned int i;
38
39    for (i = 0; i+4 <= size; i+=4) {
40        *(int *)(dst+i) = ptrace(PTRACE_PEEKTEXT, pid, src+i, NULL);
41    }
42
43    if (i < size) {
44        int val;
45
46        assert((size - i) < 4);
47        val = ptrace(PTRACE_PEEKTEXT, pid, src+i, NULL);
48        while (i < size) {
49            ((unsigned char *)dst)[i] = val & 0xff;
50            i++;
51            val >>= 8;
52        }
53    }
54}
55
56/* Map a pc address to the name of the containing ELF file */
57const char *map_to_name(mapinfo *mi, unsigned pc, const char* def)
58{
59    while(mi) {
60        if((pc >= mi->start) && (pc < mi->end)){
61            return mi->name;
62        }
63        mi = mi->next;
64    }
65    return def;
66}
67
68/* Find the containing map info for the pc */
69const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc)
70{
71    while(mi) {
72        if((pc >= mi->start) && (pc < mi->end)){
73            return mi;
74        }
75        mi = mi->next;
76    }
77    return NULL;
78}
79