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 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 40 | static mapinfo* parse_maps_line(char* line) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 41 | int len = strlen(line); |
| 42 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 43 | if (len < 1) return 0; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 44 | line[--len] = 0; |
| 45 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 46 | if (len < 50) return 0; |
| 47 | if (line[20] != 'x') return 0; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 49 | mapinfo* mi = static_cast<mapinfo*>(dlmalloc(sizeof(mapinfo) + (len - 47))); |
| 50 | if (mi == 0) return 0; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 51 | |
| 52 | mi->start = strtoul(line, 0, 16); |
| 53 | mi->end = strtoul(line + 9, 0, 16); |
| 54 | /* To be filled in parse_elf_info if the mapped section starts with |
| 55 | * elf_header |
| 56 | */ |
| 57 | mi->next = 0; |
| 58 | strcpy(mi->name, line + 49); |
| 59 | |
| 60 | return mi; |
| 61 | } |
| 62 | |
| 63 | __LIBC_HIDDEN__ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 64 | mapinfo *init_mapinfo(int pid) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 65 | struct mapinfo *milist = NULL; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 66 | char data[1024]; // Used to read lines as well as to construct the filename. |
| 67 | snprintf(data, sizeof(data), "/proc/%d/maps", pid); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 68 | FILE *fp = fopen(data, "r"); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 69 | if (fp) { |
| 70 | while (fgets(data, sizeof(data), fp)) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 71 | mapinfo *mi = parse_maps_line(data); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 72 | if (mi) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 73 | mi->next = milist; |
| 74 | milist = mi; |
| 75 | } |
| 76 | } |
| 77 | fclose(fp); |
| 78 | } |
| 79 | |
| 80 | return milist; |
| 81 | } |
| 82 | |
| 83 | __LIBC_HIDDEN__ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 84 | void deinit_mapinfo(mapinfo *mi) { |
| 85 | mapinfo *del; |
| 86 | while (mi) { |
| 87 | del = mi; |
| 88 | mi = mi->next; |
| 89 | dlfree(del); |
| 90 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Map a pc address to the name of the containing ELF file */ |
| 94 | __LIBC_HIDDEN__ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 95 | const char *map_to_name(mapinfo *mi, unsigned pc, const char* def) { |
| 96 | while (mi) { |
| 97 | if ((pc >= mi->start) && (pc < mi->end)) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 98 | return mi->name; |
| 99 | } |
| 100 | mi = mi->next; |
| 101 | } |
| 102 | return def; |
| 103 | } |
| 104 | |
| 105 | /* Find the containing map info for the pc */ |
| 106 | __LIBC_HIDDEN__ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 107 | const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 108 | *rel_pc = pc; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 109 | while (mi) { |
| 110 | if ((pc >= mi->start) && (pc < mi->end)) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 111 | // Only calculate the relative offset for shared libraries |
| 112 | if (strstr(mi->name, ".so")) { |
| 113 | *rel_pc -= mi->start; |
| 114 | } |
| 115 | return mi; |
| 116 | } |
| 117 | mi = mi->next; |
| 118 | } |
| 119 | return NULL; |
| 120 | } |