Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [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> |
Christopher Ferris | 6425327 | 2014-07-18 12:26:45 -0700 | [diff] [blame] | 32 | #include <sys/mman.h> |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 34 | #include "debug_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_t* parse_maps_line(char* line) { |
| 41 | int len = strlen(line); |
| 42 | |
| 43 | if (len < 1) return 0; |
| 44 | line[--len] = 0; |
| 45 | |
| 46 | if (len < 50) return 0; |
| 47 | if (line[20] != 'x') return 0; |
| 48 | |
Christopher Ferris | 6425327 | 2014-07-18 12:26:45 -0700 | [diff] [blame] | 49 | mapinfo_t* mi = static_cast<mapinfo_t*>( |
| 50 | mmap(NULL, sizeof(mapinfo_t) + (len - 47), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0)); |
| 51 | if (mi == MAP_FAILED) return 0; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 52 | |
| 53 | mi->start = strtoul(line, 0, 16); |
| 54 | mi->end = strtoul(line + 9, 0, 16); |
| 55 | mi->next = 0; |
| 56 | strcpy(mi->name, line + 49); |
| 57 | |
| 58 | return mi; |
| 59 | } |
| 60 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 61 | __LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 62 | struct mapinfo_t* milist = NULL; |
| 63 | char data[1024]; // Used to read lines as well as to construct the filename. |
| 64 | snprintf(data, sizeof(data), "/proc/%d/maps", pid); |
| 65 | FILE* fp = fopen(data, "r"); |
| 66 | if (fp != NULL) { |
| 67 | while (fgets(data, sizeof(data), fp) != NULL) { |
| 68 | mapinfo_t* mi = parse_maps_line(data); |
| 69 | if (mi) { |
| 70 | mi->next = milist; |
| 71 | milist = mi; |
| 72 | } |
| 73 | } |
| 74 | fclose(fp); |
| 75 | } |
| 76 | return milist; |
| 77 | } |
| 78 | |
| 79 | __LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 80 | while (mi != NULL) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 81 | mapinfo_t* del = mi; |
| 82 | mi = mi->next; |
Christopher Ferris | 6425327 | 2014-07-18 12:26:45 -0700 | [diff] [blame] | 83 | munmap(del, sizeof(mapinfo_t) + strlen(del->name) + 2); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | // Find the containing map info for the PC. |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 88 | __LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 89 | for (; mi != NULL; mi = mi->next) { |
| 90 | if ((pc >= mi->start) && (pc < mi->end)) { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 91 | *rel_pc = pc - mi->start; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 92 | return mi; |
| 93 | } |
| 94 | } |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 95 | *rel_pc = pc; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 96 | return NULL; |
| 97 | } |