blob: de72cb2912361bf493725044a42d8380229cc654 [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>
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070030#include <elf.h>
Christopher Ferris861c0ef2014-07-24 17:52:23 -070031#include <inttypes.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35
Elliott Hughes1e980b62013-01-17 18:36:06 -080036#include "debug_mapinfo.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070037#include "malloc_debug_disable.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080038
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070039#if defined(__LP64__)
40#define Elf_W(x) Elf64_##x
41#else
42#define Elf_W(x) Elf32_##x
43#endif
44
Christopher Ferris861c0ef2014-07-24 17:52:23 -070045// Format of /proc/<PID>/maps:
46// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
Elliott Hughes1e980b62013-01-17 18:36:06 -080047static mapinfo_t* parse_maps_line(char* line) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070048 uintptr_t start;
49 uintptr_t end;
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070050 uintptr_t offset;
51 char permissions[4];
Christopher Ferris861c0ef2014-07-24 17:52:23 -070052 int name_pos;
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070053 if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d%n", &start,
54 &end, permissions, &offset, &name_pos) < 2) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070055 return NULL;
56 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080057
Christopher Ferris861c0ef2014-07-24 17:52:23 -070058 while (isspace(line[name_pos])) {
59 name_pos += 1;
60 }
61 const char* name = line + name_pos;
62 size_t name_len = strlen(name);
63 if (name_len && name[name_len - 1] == '\n') {
64 name_len -= 1;
65 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080066
Christopher Ferris861c0ef2014-07-24 17:52:23 -070067 mapinfo_t* mi = reinterpret_cast<mapinfo_t*>(calloc(1, sizeof(mapinfo_t) + name_len + 1));
68 if (mi) {
69 mi->start = start;
70 mi->end = end;
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070071 mi->offset = offset;
72 if (permissions[0] != 'r') {
73 // Any unreadable map will just get a zero load base.
74 mi->load_base = 0;
75 mi->load_base_read = true;
76 } else {
77 mi->load_base_read = false;
78 }
Christopher Ferris861c0ef2014-07-24 17:52:23 -070079 memcpy(mi->name, name, name_len);
80 mi->name[name_len] = '\0';
81 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080082 return mi;
83}
84
Elliott Hughes35b621c2013-01-28 16:27:36 -080085__LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070086 ScopedDisableDebugCalls disable;
87
Elliott Hughes1e980b62013-01-17 18:36:06 -080088 struct mapinfo_t* milist = NULL;
89 char data[1024]; // Used to read lines as well as to construct the filename.
90 snprintf(data, sizeof(data), "/proc/%d/maps", pid);
Elliott Hughesc674edb2014-08-26 15:56:54 -070091 FILE* fp = fopen(data, "re");
Elliott Hughes1e980b62013-01-17 18:36:06 -080092 if (fp != NULL) {
93 while (fgets(data, sizeof(data), fp) != NULL) {
94 mapinfo_t* mi = parse_maps_line(data);
95 if (mi) {
96 mi->next = milist;
97 milist = mi;
98 }
99 }
100 fclose(fp);
101 }
102 return milist;
103}
104
105__LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700106 ScopedDisableDebugCalls disable;
107
Elliott Hughes35b621c2013-01-28 16:27:36 -0800108 while (mi != NULL) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800109 mapinfo_t* del = mi;
110 mi = mi->next;
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700111 free(del);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800112 }
113}
114
Christopher Ferris70b6e1d2015-07-16 14:49:17 -0700115template<typename T>
116static inline bool get_val(mapinfo_t* mi, uintptr_t addr, T* store) {
117 if (addr < mi->start || addr + sizeof(T) > mi->end) {
118 return false;
119 }
120 // Make sure the address is aligned properly.
121 if (addr & (sizeof(T)-1)) {
122 return false;
123 }
124 *store = *reinterpret_cast<T*>(addr);
125 return true;
126}
127
128__LIBC_HIDDEN__ void mapinfo_read_loadbase(mapinfo_t* mi) {
129 mi->load_base = 0;
130 mi->load_base_read = true;
131 uintptr_t addr = mi->start;
132 Elf_W(Ehdr) ehdr;
133 if (!get_val<Elf_W(Half)>(mi, addr + offsetof(Elf_W(Ehdr), e_phnum), &ehdr.e_phnum)) {
134 return;
135 }
136 if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Ehdr), e_phoff), &ehdr.e_phoff)) {
137 return;
138 }
139 addr += ehdr.e_phoff;
140 for (size_t i = 0; i < ehdr.e_phnum; i++) {
141 Elf_W(Phdr) phdr;
142 if (!get_val<Elf_W(Word)>(mi, addr + offsetof(Elf_W(Phdr), p_type), &phdr.p_type)) {
143 return;
144 }
145 if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Phdr), p_offset), &phdr.p_offset)) {
146 return;
147 }
148 if (phdr.p_type == PT_LOAD && phdr.p_offset == mi->offset) {
149 if (!get_val<Elf_W(Addr)>(mi, addr + offsetof(Elf_W(Phdr), p_vaddr), &phdr.p_vaddr)) {
150 return;
151 }
152 mi->load_base = phdr.p_vaddr;
153 return;
154 }
155 addr += sizeof(phdr);
156 }
157}
158
Elliott Hughes1e980b62013-01-17 18:36:06 -0800159// Find the containing map info for the PC.
Elliott Hughes35b621c2013-01-28 16:27:36 -0800160__LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800161 for (; mi != NULL; mi = mi->next) {
162 if ((pc >= mi->start) && (pc < mi->end)) {
Christopher Ferris70b6e1d2015-07-16 14:49:17 -0700163 if (!mi->load_base_read) {
164 mapinfo_read_loadbase(mi);
165 }
166 *rel_pc = pc - mi->start + mi->load_base;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800167 return mi;
168 }
169 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800170 *rel_pc = pc;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800171 return NULL;
172}