blob: 6fb8ebea316051a3151eb6cb370b4b744e1cbf40 [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;
Elliott Hughes0dec2282015-09-22 15:45:50 -070053 if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n", &start,
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070054 &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 const char* name = line + name_pos;
59 size_t name_len = strlen(name);
60 if (name_len && name[name_len - 1] == '\n') {
61 name_len -= 1;
62 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080063
Christopher Ferris861c0ef2014-07-24 17:52:23 -070064 mapinfo_t* mi = reinterpret_cast<mapinfo_t*>(calloc(1, sizeof(mapinfo_t) + name_len + 1));
65 if (mi) {
66 mi->start = start;
67 mi->end = end;
Christopher Ferris70b6e1d2015-07-16 14:49:17 -070068 mi->offset = offset;
69 if (permissions[0] != 'r') {
70 // Any unreadable map will just get a zero load base.
71 mi->load_base = 0;
72 mi->load_base_read = true;
73 } else {
74 mi->load_base_read = false;
75 }
Christopher Ferris861c0ef2014-07-24 17:52:23 -070076 memcpy(mi->name, name, name_len);
77 mi->name[name_len] = '\0';
78 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080079 return mi;
80}
81
Elliott Hughes35b621c2013-01-28 16:27:36 -080082__LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070083 ScopedDisableDebugCalls disable;
84
Elliott Hughes1e980b62013-01-17 18:36:06 -080085 struct mapinfo_t* milist = NULL;
86 char data[1024]; // Used to read lines as well as to construct the filename.
87 snprintf(data, sizeof(data), "/proc/%d/maps", pid);
Elliott Hughesc674edb2014-08-26 15:56:54 -070088 FILE* fp = fopen(data, "re");
Elliott Hughes1e980b62013-01-17 18:36:06 -080089 if (fp != NULL) {
90 while (fgets(data, sizeof(data), fp) != NULL) {
91 mapinfo_t* mi = parse_maps_line(data);
92 if (mi) {
93 mi->next = milist;
94 milist = mi;
95 }
96 }
97 fclose(fp);
98 }
99 return milist;
100}
101
102__LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700103 ScopedDisableDebugCalls disable;
104
Elliott Hughes35b621c2013-01-28 16:27:36 -0800105 while (mi != NULL) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800106 mapinfo_t* del = mi;
107 mi = mi->next;
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700108 free(del);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800109 }
110}
111
Christopher Ferris70b6e1d2015-07-16 14:49:17 -0700112template<typename T>
113static inline bool get_val(mapinfo_t* mi, uintptr_t addr, T* store) {
114 if (addr < mi->start || addr + sizeof(T) > mi->end) {
115 return false;
116 }
117 // Make sure the address is aligned properly.
118 if (addr & (sizeof(T)-1)) {
119 return false;
120 }
121 *store = *reinterpret_cast<T*>(addr);
122 return true;
123}
124
125__LIBC_HIDDEN__ void mapinfo_read_loadbase(mapinfo_t* mi) {
126 mi->load_base = 0;
127 mi->load_base_read = true;
128 uintptr_t addr = mi->start;
129 Elf_W(Ehdr) ehdr;
130 if (!get_val<Elf_W(Half)>(mi, addr + offsetof(Elf_W(Ehdr), e_phnum), &ehdr.e_phnum)) {
131 return;
132 }
133 if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Ehdr), e_phoff), &ehdr.e_phoff)) {
134 return;
135 }
136 addr += ehdr.e_phoff;
137 for (size_t i = 0; i < ehdr.e_phnum; i++) {
138 Elf_W(Phdr) phdr;
139 if (!get_val<Elf_W(Word)>(mi, addr + offsetof(Elf_W(Phdr), p_type), &phdr.p_type)) {
140 return;
141 }
142 if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Phdr), p_offset), &phdr.p_offset)) {
143 return;
144 }
145 if (phdr.p_type == PT_LOAD && phdr.p_offset == mi->offset) {
146 if (!get_val<Elf_W(Addr)>(mi, addr + offsetof(Elf_W(Phdr), p_vaddr), &phdr.p_vaddr)) {
147 return;
148 }
149 mi->load_base = phdr.p_vaddr;
150 return;
151 }
152 addr += sizeof(phdr);
153 }
154}
155
Elliott Hughes1e980b62013-01-17 18:36:06 -0800156// Find the containing map info for the PC.
Elliott Hughes35b621c2013-01-28 16:27:36 -0800157__LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800158 for (; mi != NULL; mi = mi->next) {
159 if ((pc >= mi->start) && (pc < mi->end)) {
Christopher Ferris70b6e1d2015-07-16 14:49:17 -0700160 if (!mi->load_base_read) {
161 mapinfo_read_loadbase(mi);
162 }
163 *rel_pc = pc - mi->start + mi->load_base;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800164 return mi;
165 }
166 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800167 *rel_pc = pc;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800168 return NULL;
169}