blob: 38a96453daaffa1f8531c8307de203791aa34759 [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
20#include <sys/types.h>
21#include <stdbool.h>
22#include <inttypes.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define MAX_BACKTRACE_FRAMES 64
29
30typedef struct backtrace_map_info {
31 struct backtrace_map_info* next;
32 uintptr_t start;
33 uintptr_t end;
34 bool is_readable;
35 bool is_writable;
36 bool is_executable;
37 char name[];
38} backtrace_map_info_t;
39
40typedef struct {
41 uintptr_t pc; /* The absolute pc. */
42 uintptr_t sp; /* The top of the stack. */
43 size_t stack_size; /* The size of the stack, zero indicate an unknown stack size. */
44 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. */
45 uintptr_t map_offset; /* pc relative to the start of the map, only valid if map_name is not NULL. */
46 char* proc_name; /* The function name associated with this pc, NULL if no not found. */
47 uintptr_t proc_offset; /* pc relative to the start of the procedure, only valid if proc_name is not NULL. */
48} backtrace_frame_data_t;
49
50typedef struct {
51 backtrace_frame_data_t frames[MAX_BACKTRACE_FRAMES];
52 size_t num_frames;
53
54 pid_t tid;
55 backtrace_map_info_t* map_info_list;
56 void* private_data;
57} backtrace_t;
58
59/* Gather the backtrace data for tid and fill in the backtrace structure.
60 * If tid < 0, then gather the backtrace for the current thread.
61 */
62bool backtrace_get_data(backtrace_t* backtrace, pid_t tid);
63
64/* Free any memory associated with the backtrace structure. */
65void backtrace_free_data(backtrace_t* backtrace);
66
67/* Read data at a specific address for a process. */
68bool backtrace_read_word(
69 const backtrace_t* backtrace, uintptr_t ptr, uint32_t* value);
70
71/* Get information about the map associated with a pc. If NULL is
72 * returned, then map_start is not set.
73 */
74const char* backtrace_get_map_info(
75 const backtrace_t* backtrace, uintptr_t pc, uintptr_t* map_start);
76
77/* Get the procedure name and offest given the pc. If NULL is returned,
78 * then proc_offset is not set. The returned string is allocated using
79 * malloc and must be freed by the caller.
80 */
81char* backtrace_get_proc_name(
82 const backtrace_t* backtrace, uintptr_t pc, uintptr_t* proc_offset);
83
84/* Loads memory map from /proc/<tid>/maps. If tid < 0, then load the memory
85 * map for the current process.
86 */
87backtrace_map_info_t* backtrace_create_map_info_list(pid_t tid);
88
89/* Frees memory associated with the map list. */
90void backtrace_destroy_map_info_list(backtrace_map_info_t* map_info_list);
91
92/* Finds the memory map that contains the specified pc. */
93const backtrace_map_info_t* backtrace_find_map_info(
94 const backtrace_map_info_t* map_info_list, uintptr_t pc);
95
96/* Create a formatted line of backtrace information for a single frame. */
97void backtrace_format_frame_data(
98 const backtrace_frame_data_t* frame, size_t frame_num, char *buf, size_t buf_size);
99
100#ifdef __cplusplus
101}
102#endif
103
104#endif /* _BACKTRACE_H */