Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include "dlmalloc.h" |
| 34 | #include "malloc_debug_check_mapinfo.h" |
| 35 | |
| 36 | // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so |
| 37 | // 012345678901234567890123456789012345678901234567890123456789 |
| 38 | // 0 1 2 3 4 5 |
| 39 | |
| 40 | static mapinfo *parse_maps_line(char *line) |
| 41 | { |
| 42 | mapinfo *mi; |
| 43 | int len = strlen(line); |
| 44 | |
| 45 | if(len < 1) return 0; |
| 46 | line[--len] = 0; |
| 47 | |
| 48 | if(len < 50) return 0; |
| 49 | if(line[20] != 'x') return 0; |
| 50 | |
| 51 | mi = dlmalloc(sizeof(mapinfo) + (len - 47)); |
| 52 | if(mi == 0) return 0; |
| 53 | |
| 54 | mi->start = strtoul(line, 0, 16); |
| 55 | mi->end = strtoul(line + 9, 0, 16); |
| 56 | /* To be filled in parse_elf_info if the mapped section starts with |
| 57 | * elf_header |
| 58 | */ |
| 59 | mi->next = 0; |
| 60 | strcpy(mi->name, line + 49); |
| 61 | |
| 62 | return mi; |
| 63 | } |
| 64 | |
| 65 | __LIBC_HIDDEN__ |
| 66 | mapinfo *init_mapinfo(int pid) |
| 67 | { |
| 68 | struct mapinfo *milist = NULL; |
| 69 | char data[1024]; |
| 70 | sprintf(data, "/proc/%d/maps", pid); |
| 71 | FILE *fp = fopen(data, "r"); |
| 72 | if(fp) { |
| 73 | while(fgets(data, sizeof(data), fp)) { |
| 74 | mapinfo *mi = parse_maps_line(data); |
| 75 | if(mi) { |
| 76 | mi->next = milist; |
| 77 | milist = mi; |
| 78 | } |
| 79 | } |
| 80 | fclose(fp); |
| 81 | } |
| 82 | |
| 83 | return milist; |
| 84 | } |
| 85 | |
| 86 | __LIBC_HIDDEN__ |
| 87 | void deinit_mapinfo(mapinfo *mi) |
| 88 | { |
| 89 | mapinfo *del; |
| 90 | while(mi) { |
| 91 | del = mi; |
| 92 | mi = mi->next; |
| 93 | dlfree(del); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Map a pc address to the name of the containing ELF file */ |
| 98 | __LIBC_HIDDEN__ |
| 99 | const char *map_to_name(mapinfo *mi, unsigned pc, const char* def) |
| 100 | { |
| 101 | while(mi) { |
| 102 | if((pc >= mi->start) && (pc < mi->end)){ |
| 103 | return mi->name; |
| 104 | } |
| 105 | mi = mi->next; |
| 106 | } |
| 107 | return def; |
| 108 | } |
| 109 | |
| 110 | /* Find the containing map info for the pc */ |
| 111 | __LIBC_HIDDEN__ |
| 112 | const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc) |
| 113 | { |
| 114 | *rel_pc = pc; |
| 115 | while(mi) { |
| 116 | if((pc >= mi->start) && (pc < mi->end)){ |
| 117 | // Only calculate the relative offset for shared libraries |
| 118 | if (strstr(mi->name, ".so")) { |
| 119 | *rel_pc -= mi->start; |
| 120 | } |
| 121 | return mi; |
| 122 | } |
| 123 | mi = mi->next; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |