debug_mapinfo.cpp revision 72bbd423579bb971dc06cdd3c06201faf3fe95e6
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>
32
33#ifdef USE_JEMALLOC
34#include "jemalloc.h"
35#define Malloc(function)  je_ ## function
36#else
37#include "dlmalloc.h"
38#define Malloc(function)  dl ## function
39#endif
40#include "debug_mapinfo.h"
41
42// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so
43// 012345678901234567890123456789012345678901234567890123456789
44// 0         1         2         3         4         5
45
46static mapinfo_t* parse_maps_line(char* line) {
47  int len = strlen(line);
48
49  if (len < 1) return 0;
50  line[--len] = 0;
51
52  if (len < 50) return 0;
53  if (line[20] != 'x') return 0;
54
55  mapinfo_t* mi = static_cast<mapinfo_t*>(Malloc(malloc)(sizeof(mapinfo_t) + (len - 47)));
56  if (mi == 0) return 0;
57
58  mi->start = strtoul(line, 0, 16);
59  mi->end = strtoul(line + 9, 0, 16);
60  mi->next = 0;
61  strcpy(mi->name, line + 49);
62
63  return mi;
64}
65
66__LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) {
67  struct mapinfo_t* milist = NULL;
68  char data[1024]; // Used to read lines as well as to construct the filename.
69  snprintf(data, sizeof(data), "/proc/%d/maps", pid);
70  FILE* fp = fopen(data, "r");
71  if (fp != NULL) {
72    while (fgets(data, sizeof(data), fp) != NULL) {
73      mapinfo_t* mi = parse_maps_line(data);
74      if (mi) {
75        mi->next = milist;
76        milist = mi;
77      }
78    }
79    fclose(fp);
80  }
81  return milist;
82}
83
84__LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) {
85  while (mi != NULL) {
86    mapinfo_t* del = mi;
87    mi = mi->next;
88    Malloc(free)(del);
89  }
90}
91
92// Find the containing map info for the PC.
93__LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) {
94  for (; mi != NULL; mi = mi->next) {
95    if ((pc >= mi->start) && (pc < mi->end)) {
96      *rel_pc = pc - mi->start;
97      return mi;
98    }
99  }
100  *rel_pc = pc;
101  return NULL;
102}
103