FreeRDP
backtrace.h
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 /* A stack unwinder. */
18 
19 #ifndef _CORKSCREW_BACKTRACE_H
20 #define _CORKSCREW_BACKTRACE_H
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
27 #include <sys/types.h>
28 #include <corkscrew/ptrace.h>
29 #include <corkscrew/map_info.h>
30 #include <corkscrew/symbol_table.h>
31 
32  /*
33  * Describes a single frame of a backtrace.
34  */
35  typedef struct
36  {
37  uintptr_t absolute_pc; /* absolute PC offset */
38  uintptr_t stack_top; /* top of stack for this frame */
39  size_t stack_size; /* size of this stack frame */
41 
42  /*
43  * Describes the symbols associated with a backtrace frame.
44  */
45  typedef struct
46  {
47  uintptr_t relative_pc; /* relative frame PC offset from the start of the library,
48  or the absolute PC if the library is unknown */
49  uintptr_t relative_symbol_addr; /* relative offset of the symbol from the start of the
50  library or 0 if the library is unknown */
51  char* map_name; /* executable or library name, or NULL if unknown */
52  char* symbol_name; /* symbol name, or NULL if unknown */
53  char* demangled_name; /* demangled symbol name, or NULL if unknown */
55 
56  /*
57  * Unwinds the call stack for the current thread of execution.
58  * Populates the backtrace array with the program counters from the call stack.
59  * Returns the number of frames collected, or -1 if an error occurred.
60  */
61  ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
62 
63  /*
64  * Unwinds the call stack for a thread within this process.
65  * Populates the backtrace array with the program counters from the call stack.
66  * Returns the number of frames collected, or -1 if an error occurred.
67  *
68  * The task is briefly suspended while the backtrace is being collected.
69  */
70  ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace, size_t ignore_depth,
71  size_t max_depth);
72 
73  /*
74  * Unwinds the call stack of a task within a remote process using ptrace().
75  * Populates the backtrace array with the program counters from the call stack.
76  * Returns the number of frames collected, or -1 if an error occurred.
77  */
78  ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
79  backtrace_frame_t* backtrace, size_t ignore_depth,
80  size_t max_depth);
81 
82  /*
83  * Gets the symbols for each frame of a backtrace.
84  * The symbols array must be big enough to hold one symbol record per frame.
85  * The symbols must later be freed using free_backtrace_symbols.
86  */
87  void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
88  backtrace_symbol_t* backtrace_symbols);
89 
90  /*
91  * Gets the symbols for each frame of a backtrace from a remote process.
92  * The symbols array must be big enough to hold one symbol record per frame.
93  * The symbols must later be freed using free_backtrace_symbols.
94  */
95  void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
96  const backtrace_frame_t* backtrace, size_t frames,
97  backtrace_symbol_t* backtrace_symbols);
98 
99  /*
100  * Frees the storage associated with backtrace symbols.
101  */
102  void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames);
103 
104  enum
105  {
106  // A hint for how big to make the line buffer for format_backtrace_line
107  MAX_BACKTRACE_LINE_LENGTH = 800,
108  };
109 
113  void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame,
114  const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize);
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif // _CORKSCREW_BACKTRACE_H