Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #define LOG_TAG "Corkscrew" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <corkscrew/map_info.h> |
| 21 | |
| 22 | #include <ctype.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <limits.h> |
| 26 | #include <pthread.h> |
| 27 | #include <unistd.h> |
| 28 | #include <cutils/log.h> |
| 29 | |
| 30 | // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n |
| 31 | // 012345678901234567890123456789012345678901234567890123456789 |
| 32 | // 0 1 2 3 4 5 |
| 33 | static map_info_t* parse_maps_line(const char* line) |
| 34 | { |
| 35 | unsigned long int start; |
| 36 | unsigned long int end; |
| 37 | char permissions[5]; |
| 38 | int name_pos; |
| 39 | if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", &start, &end, |
| 40 | permissions, &name_pos) != 3) { |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | while (isspace(line[name_pos])) { |
| 45 | name_pos += 1; |
| 46 | } |
| 47 | const char* name = line + name_pos; |
| 48 | size_t name_len = strlen(name); |
| 49 | if (name_len && name[name_len - 1] == '\n') { |
| 50 | name_len -= 1; |
| 51 | } |
| 52 | |
| 53 | map_info_t* mi = calloc(1, sizeof(map_info_t) + name_len + 1); |
| 54 | if (mi) { |
| 55 | mi->start = start; |
| 56 | mi->end = end; |
| 57 | mi->is_executable = strlen(permissions) == 4 && permissions[2] == 'x'; |
| 58 | mi->data = NULL; |
| 59 | memcpy(mi->name, name, name_len); |
| 60 | mi->name[name_len] = '\0'; |
| 61 | } |
| 62 | return mi; |
| 63 | } |
| 64 | |
| 65 | map_info_t* load_map_info_list(pid_t tid) { |
| 66 | char path[PATH_MAX]; |
| 67 | char line[1024]; |
| 68 | FILE* fp; |
| 69 | map_info_t* milist = NULL; |
| 70 | |
| 71 | snprintf(path, PATH_MAX, "/proc/%d/maps", tid); |
| 72 | fp = fopen(path, "r"); |
| 73 | if (fp) { |
| 74 | while(fgets(line, sizeof(line), fp)) { |
| 75 | map_info_t* mi = parse_maps_line(line); |
| 76 | if (mi) { |
| 77 | mi->next = milist; |
| 78 | milist = mi; |
| 79 | } |
| 80 | } |
| 81 | fclose(fp); |
| 82 | } |
| 83 | return milist; |
| 84 | } |
| 85 | |
| 86 | void free_map_info_list(map_info_t* milist) { |
| 87 | while (milist) { |
| 88 | map_info_t* next = milist->next; |
| 89 | free(milist); |
| 90 | milist = next; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr) { |
| 95 | const map_info_t* mi = milist; |
| 96 | while (mi && !(addr >= mi->start && addr < mi->end)) { |
| 97 | mi = mi->next; |
| 98 | } |
| 99 | return mi; |
| 100 | } |
| 101 | |
| 102 | static pthread_once_t g_my_milist_once = PTHREAD_ONCE_INIT; |
| 103 | static map_info_t* g_my_milist = NULL; |
| 104 | |
| 105 | static void init_my_milist_once() { |
| 106 | g_my_milist = load_map_info_list(getpid()); |
| 107 | } |
| 108 | |
| 109 | const map_info_t* my_map_info_list() { |
| 110 | pthread_once(&g_my_milist_once, init_my_milist_once); |
| 111 | return g_my_milist; |
| 112 | } |