1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <ctype.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <limits.h> 22 #include <pthread.h> 23 #include <unistd.h> 24 #include <log/log.h> 25 #include <sys/time.h> 26 27 #include <backtrace/backtrace.h> 28 29 #if defined(__APPLE__) 30 31 // Mac OS vmmap(1) output: 32 // __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n 33 // 012345678901234567890123456789012345678901234567890123456789 34 // 0 1 2 3 4 5 35 static backtrace_map_info_t* parse_vmmap_line(const char* line) { 36 unsigned long int start; 37 unsigned long int end; 38 char permissions[4]; 39 int name_pos; 40 if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n", 41 &start, &end, permissions, &name_pos) != 3) { 42 return NULL; 43 } 44 45 const char* name = line + name_pos; 46 size_t name_len = strlen(name); 47 48 backtrace_map_info_t* mi = calloc(1, sizeof(backtrace_map_info_t) + name_len); 49 if (mi != NULL) { 50 mi->start = start; 51 mi->end = end; 52 mi->is_readable = permissions[0] == 'r'; 53 mi->is_writable = permissions[1] == 'w'; 54 mi->is_executable = permissions[2] == 'x'; 55 memcpy(mi->name, name, name_len); 56 mi->name[name_len - 1] = '\0'; 57 ALOGV("Parsed map: start=0x%08x, end=0x%08x, " 58 "is_readable=%d, is_writable=%d is_executable=%d, name=%s", 59 mi->start, mi->end, 60 mi->is_readable, mi->is_writable, mi->is_executable, mi->name); 61 } 62 return mi; 63 } 64 65 backtrace_map_info_t* backtrace_create_map_info_list(pid_t pid) { 66 char cmd[1024]; 67 if (pid < 0) { 68 pid = getpid(); 69 } 70 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid); 71 FILE* fp = popen(cmd, "r"); 72 if (fp == NULL) { 73 return NULL; 74 } 75 76 char line[1024]; 77 backtrace_map_info_t* milist = NULL; 78 while (fgets(line, sizeof(line), fp) != NULL) { 79 backtrace_map_info_t* mi = parse_vmmap_line(line); 80 if (mi != NULL) { 81 mi->next = milist; 82 milist = mi; 83 } 84 } 85 pclose(fp); 86 return milist; 87 } 88 89 #else 90 91 // Linux /proc/<pid>/maps lines: 92 // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n 93 // 012345678901234567890123456789012345678901234567890123456789 94 // 0 1 2 3 4 5 95 static backtrace_map_info_t* parse_maps_line(const char* line) 96 { 97 unsigned long int start; 98 unsigned long int end; 99 char permissions[5]; 100 int name_pos; 101 if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", &start, &end, 102 permissions, &name_pos) != 3) { 103 return NULL; 104 } 105 106 while (isspace(line[name_pos])) { 107 name_pos += 1; 108 } 109 const char* name = line + name_pos; 110 size_t name_len = strlen(name); 111 if (name_len && name[name_len - 1] == '\n') { 112 name_len -= 1; 113 } 114 115 backtrace_map_info_t* mi = calloc(1, sizeof(backtrace_map_info_t) + name_len + 1); 116 if (mi) { 117 mi->start = start; 118 mi->end = end; 119 mi->is_readable = strlen(permissions) == 4 && permissions[0] == 'r'; 120 mi->is_writable = strlen(permissions) == 4 && permissions[1] == 'w'; 121 mi->is_executable = strlen(permissions) == 4 && permissions[2] == 'x'; 122 memcpy(mi->name, name, name_len); 123 mi->name[name_len] = '\0'; 124 ALOGV("Parsed map: start=0x%08x, end=0x%08x, " 125 "is_readable=%d, is_writable=%d, is_executable=%d, name=%s", 126 mi->start, mi->end, 127 mi->is_readable, mi->is_writable, mi->is_executable, mi->name); 128 } 129 return mi; 130 } 131 132 backtrace_map_info_t* backtrace_create_map_info_list(pid_t tid) { 133 char path[PATH_MAX]; 134 char line[1024]; 135 FILE* fp; 136 backtrace_map_info_t* milist = NULL; 137 138 if (tid < 0) { 139 tid = getpid(); 140 } 141 snprintf(path, PATH_MAX, "/proc/%d/maps", tid); 142 fp = fopen(path, "r"); 143 if (fp) { 144 while(fgets(line, sizeof(line), fp)) { 145 backtrace_map_info_t* mi = parse_maps_line(line); 146 if (mi) { 147 mi->next = milist; 148 milist = mi; 149 } 150 } 151 fclose(fp); 152 } 153 return milist; 154 } 155 156 #endif 157 158 void backtrace_destroy_map_info_list(backtrace_map_info_t* milist) { 159 while (milist) { 160 backtrace_map_info_t* next = milist->next; 161 free(milist); 162 milist = next; 163 } 164 } 165 166 const backtrace_map_info_t* backtrace_find_map_info( 167 const backtrace_map_info_t* milist, uintptr_t addr) { 168 const backtrace_map_info_t* mi = milist; 169 while (mi && !(addr >= mi->start && addr < mi->end)) { 170 mi = mi->next; 171 } 172 return mi; 173 } 174