Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 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 | #ifndef _BACKTRACE_H |
| 18 | #define _BACKTRACE_H |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <inttypes.h> |
| 23 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | __BEGIN_DECLS |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 25 | |
| 26 | #define MAX_BACKTRACE_FRAMES 64 |
| 27 | |
| 28 | typedef struct backtrace_map_info { |
| 29 | struct backtrace_map_info* next; |
| 30 | uintptr_t start; |
| 31 | uintptr_t end; |
| 32 | bool is_readable; |
| 33 | bool is_writable; |
| 34 | bool is_executable; |
| 35 | char name[]; |
| 36 | } backtrace_map_info_t; |
| 37 | |
| 38 | typedef struct { |
| 39 | uintptr_t pc; /* The absolute pc. */ |
| 40 | uintptr_t sp; /* The top of the stack. */ |
| 41 | size_t stack_size; /* The size of the stack, zero indicate an unknown stack size. */ |
| 42 | const char* map_name; /* The name of the map to which this pc belongs, NULL indicates the pc doesn't belong to a known map. */ |
| 43 | uintptr_t map_offset; /* pc relative to the start of the map, only valid if map_name is not NULL. */ |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 44 | char* func_name; /* The function name associated with this pc, NULL if not found. */ |
| 45 | uintptr_t func_offset; /* pc relative to the start of the function, only valid if func_name is not NULL. */ |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 46 | } backtrace_frame_data_t; |
| 47 | |
| 48 | typedef struct { |
| 49 | backtrace_frame_data_t frames[MAX_BACKTRACE_FRAMES]; |
| 50 | size_t num_frames; |
| 51 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 52 | pid_t pid; |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 53 | pid_t tid; |
| 54 | backtrace_map_info_t* map_info_list; |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 55 | } backtrace_t; |
| 56 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 | typedef struct { |
| 58 | void* data; |
| 59 | const backtrace_t* backtrace; |
| 60 | } backtrace_context_t; |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 61 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | /* Create a context for the backtrace data and gather the backtrace. |
| 63 | * If pid < 0, then gather the backtrace for the current process. |
| 64 | */ |
| 65 | bool backtrace_create_context( |
| 66 | backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames); |
| 67 | |
| 68 | /* Gather the backtrace data for a pthread instead of a process. */ |
| 69 | bool backtrace_create_thread_context( |
| 70 | backtrace_context_t* context, pid_t tid, size_t num_ignore_frames); |
| 71 | |
| 72 | /* Free any memory allocated during the context create. */ |
| 73 | void backtrace_destroy_context(backtrace_context_t* context); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 74 | |
| 75 | /* Read data at a specific address for a process. */ |
| 76 | bool backtrace_read_word( |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 77 | const backtrace_context_t* context, uintptr_t ptr, uint32_t* value); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 78 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 79 | /* Get information about the map name associated with a pc. If NULL is |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 80 | * returned, then map_start is not set. |
| 81 | */ |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 82 | const char* backtrace_get_map_name( |
| 83 | const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 84 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 85 | /* Get the function name and offset given the pc. If NULL is returned, |
| 86 | * then func_offset is not set. The returned string is allocated using |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 87 | * malloc and must be freed by the caller. |
| 88 | */ |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 89 | char* backtrace_get_func_name( |
| 90 | const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 91 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 92 | /* Loads memory map from /proc/<pid>/maps. If pid < 0, then load the memory |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 93 | * map for the current process. |
| 94 | */ |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 95 | backtrace_map_info_t* backtrace_create_map_info_list(pid_t pid); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 96 | |
| 97 | /* Frees memory associated with the map list. */ |
| 98 | void backtrace_destroy_map_info_list(backtrace_map_info_t* map_info_list); |
| 99 | |
| 100 | /* Finds the memory map that contains the specified pc. */ |
| 101 | const backtrace_map_info_t* backtrace_find_map_info( |
| 102 | const backtrace_map_info_t* map_info_list, uintptr_t pc); |
| 103 | |
| 104 | /* Create a formatted line of backtrace information for a single frame. */ |
| 105 | void backtrace_format_frame_data( |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 106 | const backtrace_context_t* context, size_t frame_num, char* buf, |
| 107 | size_t buf_size); |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 108 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 109 | /* Get the backtrace data structure associated with the context. */ |
| 110 | const backtrace_t* backtrace_get_data(backtrace_context_t* context); |
| 111 | |
| 112 | __END_DECLS |
Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 113 | |
| 114 | #endif /* _BACKTRACE_H */ |