blob: 8cc2c99480553940205fb01c8b62a0518115f383 [file] [log] [blame]
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -07001/*
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 Hughesc4d1fec2012-08-28 14:15:04 -070040static mapinfo* parse_maps_line(char* line) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070041 int len = strlen(line);
42
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070043 if (len < 1) return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070044 line[--len] = 0;
45
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070046 if (len < 50) return 0;
47 if (line[20] != 'x') return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070048
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070049 mapinfo* mi = static_cast<mapinfo*>(dlmalloc(sizeof(mapinfo) + (len - 47)));
50 if (mi == 0) return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070051
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 Hughesc4d1fec2012-08-28 14:15:04 -070064mapinfo *init_mapinfo(int pid) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070065 struct mapinfo *milist = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070066 char data[1024]; // Used to read lines as well as to construct the filename.
67 snprintf(data, sizeof(data), "/proc/%d/maps", pid);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070068 FILE *fp = fopen(data, "r");
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070069 if (fp) {
70 while (fgets(data, sizeof(data), fp)) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070071 mapinfo *mi = parse_maps_line(data);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070072 if (mi) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070073 mi->next = milist;
74 milist = mi;
75 }
76 }
77 fclose(fp);
78 }
79
80 return milist;
81}
82
83__LIBC_HIDDEN__
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070084void deinit_mapinfo(mapinfo *mi) {
85 mapinfo *del;
86 while (mi) {
87 del = mi;
88 mi = mi->next;
89 dlfree(del);
90 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070091}
92
93/* Map a pc address to the name of the containing ELF file */
94__LIBC_HIDDEN__
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070095const char *map_to_name(mapinfo *mi, unsigned pc, const char* def) {
96 while (mi) {
97 if ((pc >= mi->start) && (pc < mi->end)) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070098 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 Hughesc4d1fec2012-08-28 14:15:04 -0700107const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700108 *rel_pc = pc;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700109 while (mi) {
110 if ((pc >= mi->start) && (pc < mi->end)) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700111 // 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}