blob: cfcbf0f48641bf4dc075759d8baf09541d7af64b [file] [log] [blame]
Christopher Ferris7fb22872013-09-27 12:43:15 -07001/*
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
Christopher Ferrise57912d2013-11-06 17:19:53 -080020#include <stdint.h>
Christopher Ferris7fb22872013-09-27 12:43:15 -070021#include <stdbool.h>
Christopher Ferrise57912d2013-11-06 17:19:53 -080022#include <sys/types.h>
Christopher Ferris7fb22872013-09-27 12:43:15 -070023
Christopher Ferris17e91d42013-10-21 13:30:52 -070024__BEGIN_DECLS
Christopher Ferris7fb22872013-09-27 12:43:15 -070025
Christopher Ferriscbfc7302013-11-05 11:04:12 -080026// When the pid to be traced is set to this value, then trace the current
27// process. If the tid value is not BACKTRACE_NO_TID, then the specified
28// thread from the current process will be traced.
29#define BACKTRACE_CURRENT_PROCESS -1
30// When the tid to be traced is set to this value, then trace the specified
Christopher Ferrisbc12d632013-11-12 10:54:16 -080031// current thread of the specified pid.
32#define BACKTRACE_CURRENT_THREAD -1
Christopher Ferriscbfc7302013-11-05 11:04:12 -080033
Christopher Ferris7fb22872013-09-27 12:43:15 -070034#define MAX_BACKTRACE_FRAMES 64
35
36typedef struct backtrace_map_info {
37 struct backtrace_map_info* next;
38 uintptr_t start;
39 uintptr_t end;
40 bool is_readable;
41 bool is_writable;
42 bool is_executable;
43 char name[];
44} backtrace_map_info_t;
45
46typedef struct {
Christopher Ferris20303f82014-01-10 16:33:16 -080047 size_t num; /* The current fame number. */
Christopher Ferris7fb22872013-09-27 12:43:15 -070048 uintptr_t pc; /* The absolute pc. */
49 uintptr_t sp; /* The top of the stack. */
50 size_t stack_size; /* The size of the stack, zero indicate an unknown stack size. */
51 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. */
52 uintptr_t map_offset; /* pc relative to the start of the map, only valid if map_name is not NULL. */
Christopher Ferris17e91d42013-10-21 13:30:52 -070053 char* func_name; /* The function name associated with this pc, NULL if not found. */
54 uintptr_t func_offset; /* pc relative to the start of the function, only valid if func_name is not NULL. */
Christopher Ferris7fb22872013-09-27 12:43:15 -070055} backtrace_frame_data_t;
56
57typedef struct {
58 backtrace_frame_data_t frames[MAX_BACKTRACE_FRAMES];
59 size_t num_frames;
60
Christopher Ferris17e91d42013-10-21 13:30:52 -070061 pid_t pid;
Christopher Ferris7fb22872013-09-27 12:43:15 -070062 pid_t tid;
63 backtrace_map_info_t* map_info_list;
Christopher Ferris7fb22872013-09-27 12:43:15 -070064} backtrace_t;
65
Christopher Ferris17e91d42013-10-21 13:30:52 -070066typedef struct {
67 void* data;
68 const backtrace_t* backtrace;
69} backtrace_context_t;
Christopher Ferris7fb22872013-09-27 12:43:15 -070070
Christopher Ferris17e91d42013-10-21 13:30:52 -070071/* Create a context for the backtrace data and gather the backtrace.
72 * If pid < 0, then gather the backtrace for the current process.
73 */
74bool backtrace_create_context(
75 backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames);
76
Christopher Ferris98464972014-01-06 19:16:33 -080077/* The same as backtrace_create_context, except that it is assumed that
78 * the pid map has already been acquired and the caller will handle freeing
79 * the map data.
80 */
81bool backtrace_create_context_with_map(
82 backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames,
83 backtrace_map_info_t* map_info);
84
Christopher Ferris17e91d42013-10-21 13:30:52 -070085/* Gather the backtrace data for a pthread instead of a process. */
86bool backtrace_create_thread_context(
87 backtrace_context_t* context, pid_t tid, size_t num_ignore_frames);
88
89/* Free any memory allocated during the context create. */
90void backtrace_destroy_context(backtrace_context_t* context);
Christopher Ferris7fb22872013-09-27 12:43:15 -070091
92/* Read data at a specific address for a process. */
93bool backtrace_read_word(
Christopher Ferris17e91d42013-10-21 13:30:52 -070094 const backtrace_context_t* context, uintptr_t ptr, uint32_t* value);
Christopher Ferris7fb22872013-09-27 12:43:15 -070095
Christopher Ferris17e91d42013-10-21 13:30:52 -070096/* Get information about the map name associated with a pc. If NULL is
Christopher Ferris7fb22872013-09-27 12:43:15 -070097 * returned, then map_start is not set.
98 */
Christopher Ferris17e91d42013-10-21 13:30:52 -070099const char* backtrace_get_map_name(
100 const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start);
Christopher Ferris7fb22872013-09-27 12:43:15 -0700101
Christopher Ferris17e91d42013-10-21 13:30:52 -0700102/* Get the function name and offset given the pc. If NULL is returned,
103 * then func_offset is not set. The returned string is allocated using
Christopher Ferris7fb22872013-09-27 12:43:15 -0700104 * malloc and must be freed by the caller.
105 */
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106char* backtrace_get_func_name(
107 const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset);
Christopher Ferris7fb22872013-09-27 12:43:15 -0700108
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109/* Loads memory map from /proc/<pid>/maps. If pid < 0, then load the memory
Christopher Ferris7fb22872013-09-27 12:43:15 -0700110 * map for the current process.
111 */
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112backtrace_map_info_t* backtrace_create_map_info_list(pid_t pid);
Christopher Ferris7fb22872013-09-27 12:43:15 -0700113
114/* Frees memory associated with the map list. */
115void backtrace_destroy_map_info_list(backtrace_map_info_t* map_info_list);
116
117/* Finds the memory map that contains the specified pc. */
118const backtrace_map_info_t* backtrace_find_map_info(
119 const backtrace_map_info_t* map_info_list, uintptr_t pc);
120
121/* Create a formatted line of backtrace information for a single frame. */
122void backtrace_format_frame_data(
Christopher Ferris17e91d42013-10-21 13:30:52 -0700123 const backtrace_context_t* context, size_t frame_num, char* buf,
124 size_t buf_size);
Christopher Ferris7fb22872013-09-27 12:43:15 -0700125
Christopher Ferris17e91d42013-10-21 13:30:52 -0700126/* Get the backtrace data structure associated with the context. */
127const backtrace_t* backtrace_get_data(backtrace_context_t* context);
128
129__END_DECLS
Christopher Ferris7fb22872013-09-27 12:43:15 -0700130
131#endif /* _BACKTRACE_H */