blob: d83799afa4c5fd47bca599fbf111cc9c762ba598 [file] [log] [blame]
Elliott Hughes1e980b62013-01-17 18:36:06 -08001/*
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
Christopher Ferris861c0ef2014-07-24 17:52:23 -070029#include <ctype.h>
30#include <inttypes.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34
Elliott Hughes1e980b62013-01-17 18:36:06 -080035#include "debug_mapinfo.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070036#include "malloc_debug_disable.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080037
Christopher Ferris861c0ef2014-07-24 17:52:23 -070038// Format of /proc/<PID>/maps:
39// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
Elliott Hughes1e980b62013-01-17 18:36:06 -080040static mapinfo_t* parse_maps_line(char* line) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070041 uintptr_t start;
42 uintptr_t end;
43 int name_pos;
44 if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d%n", &start,
45 &end, &name_pos) < 2) {
46 return NULL;
47 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080048
Christopher Ferris861c0ef2014-07-24 17:52:23 -070049 while (isspace(line[name_pos])) {
50 name_pos += 1;
51 }
52 const char* name = line + name_pos;
53 size_t name_len = strlen(name);
54 if (name_len && name[name_len - 1] == '\n') {
55 name_len -= 1;
56 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080057
Christopher Ferris861c0ef2014-07-24 17:52:23 -070058 mapinfo_t* mi = reinterpret_cast<mapinfo_t*>(calloc(1, sizeof(mapinfo_t) + name_len + 1));
59 if (mi) {
60 mi->start = start;
61 mi->end = end;
62 memcpy(mi->name, name, name_len);
63 mi->name[name_len] = '\0';
64 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080065 return mi;
66}
67
Elliott Hughes35b621c2013-01-28 16:27:36 -080068__LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070069 ScopedDisableDebugCalls disable;
70
Elliott Hughes1e980b62013-01-17 18:36:06 -080071 struct mapinfo_t* milist = NULL;
72 char data[1024]; // Used to read lines as well as to construct the filename.
73 snprintf(data, sizeof(data), "/proc/%d/maps", pid);
74 FILE* fp = fopen(data, "r");
75 if (fp != NULL) {
76 while (fgets(data, sizeof(data), fp) != NULL) {
77 mapinfo_t* mi = parse_maps_line(data);
78 if (mi) {
79 mi->next = milist;
80 milist = mi;
81 }
82 }
83 fclose(fp);
84 }
85 return milist;
86}
87
88__LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070089 ScopedDisableDebugCalls disable;
90
Elliott Hughes35b621c2013-01-28 16:27:36 -080091 while (mi != NULL) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080092 mapinfo_t* del = mi;
93 mi = mi->next;
Christopher Ferris861c0ef2014-07-24 17:52:23 -070094 free(del);
Elliott Hughes1e980b62013-01-17 18:36:06 -080095 }
96}
97
98// Find the containing map info for the PC.
Elliott Hughes35b621c2013-01-28 16:27:36 -080099__LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800100 for (; mi != NULL; mi = mi->next) {
101 if ((pc >= mi->start) && (pc < mi->end)) {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800102 *rel_pc = pc - mi->start;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800103 return mi;
104 }
105 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800106 *rel_pc = pc;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800107 return NULL;
108}