Debian Codegraph @

include/ctrace/ctrace.h

raw
1 #ifndef CTRACE_H
2 #define CTRACE_H
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdio.h>
7
8 #ifdef _WIN32
9 #define CPPTRACE_EXPORT_ATTR __declspec(dllexport)
10 #define CPPTRACE_IMPORT_ATTR __declspec(dllimport)
11 #else
12 #define CPPTRACE_EXPORT_ATTR __attribute__((visibility("default")))
13 #define CPPTRACE_IMPORT_ATTR __attribute__((visibility("default")))
14 #endif
15
16 #ifdef CPPTRACE_STATIC_DEFINE
17 # define CPPTRACE_EXPORT
18 # define CPPTRACE_NO_EXPORT
19 #else
20 # ifndef CPPTRACE_EXPORT
21 # ifdef cpptrace_lib_EXPORTS
22 /* We are building this library */
23 # define CPPTRACE_EXPORT CPPTRACE_EXPORT_ATTR
24 # else
25 /* We are using this library */
26 # define CPPTRACE_EXPORT CPPTRACE_IMPORT_ATTR
27 # endif
28 # endif
29 #endif
30
31 #if defined(__cplusplus)
32 #define CTRACE_BEGIN_DEFINITIONS extern "C" {
33 #define CTRACE_END_DEFINITIONS }
34 #else
35 #define CTRACE_BEGIN_DEFINITIONS
36 #define CTRACE_END_DEFINITIONS
37 #endif
38
39 #ifdef _MSC_VER
40 #define CTRACE_FORCE_NO_INLINE __declspec(noinline)
41 #else
42 #define CTRACE_FORCE_NO_INLINE __attribute__((noinline))
43 #endif
44
45 #ifdef _MSC_VER
46 #define CTRACE_FORCE_INLINE __forceinline
47 #elif defined(__clang__) || defined(__GNUC__)
48 #define CTRACE_FORCE_INLINE __attribute__((always_inline)) inline
49 #else
50 #define CTRACE_FORCE_INLINE inline
51 #endif
52
53 /* See `CPPTRACE_PATH_MAX` for more info. */
54 #define CTRACE_PATH_MAX 4096
55
56 CTRACE_BEGIN_DEFINITIONS
57
58 typedef struct ctrace_raw_trace ctrace_raw_trace;
59 typedef struct ctrace_object_trace ctrace_object_trace;
60 typedef struct ctrace_stacktrace ctrace_stacktrace;
61
62 /* Represents a boolean value, ensures a consistent ABI. */
63 typedef int8_t ctrace_bool;
64 /* A type that can represent a pointer, alias for `uintptr_t`. */
65 typedef uintptr_t ctrace_frame_ptr;
66 typedef struct ctrace_object_frame ctrace_object_frame;
67 typedef struct ctrace_stacktrace_frame ctrace_stacktrace_frame;
68 typedef struct ctrace_safe_object_frame ctrace_safe_object_frame;
69
70 /* Type-safe null-terminated string wrapper */
71 typedef struct {
72 const char* data;
73 } ctrace_owning_string;
74
75 struct ctrace_object_frame {
76 ctrace_frame_ptr raw_address;
77 ctrace_frame_ptr obj_address;
78 const char* obj_path;
79 };
80
81 struct ctrace_stacktrace_frame {
82 ctrace_frame_ptr raw_address;
83 ctrace_frame_ptr object_address;
84 uint32_t line;
85 uint32_t column;
86 const char* filename;
87 const char* symbol;
88 ctrace_bool is_inline;
89 };
90
91 struct ctrace_safe_object_frame {
92 ctrace_frame_ptr raw_address;
93 ctrace_frame_ptr relative_obj_address;
94 char object_path[CTRACE_PATH_MAX + 1];
95 };
96
97 struct ctrace_raw_trace {
98 ctrace_frame_ptr* frames;
99 size_t count;
100 };
101
102 struct ctrace_object_trace {
103 ctrace_object_frame* frames;
104 size_t count;
105 };
106
107 struct ctrace_stacktrace {
108 ctrace_stacktrace_frame* frames;
109 size_t count;
110 };
111
112 /* ctrace::string: */
113 CPPTRACE_EXPORT ctrace_owning_string ctrace_generate_owning_string(const char* raw_string);
114 CPPTRACE_EXPORT void ctrace_free_owning_string(ctrace_owning_string* string);
115
116 /* ctrace::generation: */
117 CPPTRACE_EXPORT ctrace_raw_trace ctrace_generate_raw_trace(size_t skip, size_t max_depth);
118 CPPTRACE_EXPORT ctrace_object_trace ctrace_generate_object_trace(size_t skip, size_t max_depth);
119 CPPTRACE_EXPORT ctrace_stacktrace ctrace_generate_trace(size_t skip, size_t max_depth);
120
121 /* ctrace::freeing: */
122 CPPTRACE_EXPORT void ctrace_free_raw_trace(ctrace_raw_trace* trace);
123 CPPTRACE_EXPORT void ctrace_free_object_trace(ctrace_object_trace* trace);
124 CPPTRACE_EXPORT void ctrace_free_stacktrace(ctrace_stacktrace* trace);
125
126 /* ctrace::resolve: */
127 CPPTRACE_EXPORT ctrace_stacktrace ctrace_resolve_raw_trace(const ctrace_raw_trace* trace);
128 CPPTRACE_EXPORT ctrace_object_trace ctrace_resolve_raw_trace_to_object_trace(const ctrace_raw_trace* trace);
129 CPPTRACE_EXPORT ctrace_stacktrace ctrace_resolve_object_trace(const ctrace_object_trace* trace);
130
131 /* ctrace::safe: */
132 CPPTRACE_EXPORT size_t ctrace_safe_generate_raw_trace(ctrace_frame_ptr* buffer, size_t size, size_t skip, size_t max_depth);
133 CPPTRACE_EXPORT void ctrace_get_safe_object_frame(ctrace_frame_ptr address, ctrace_safe_object_frame* out);
134 CPPTRACE_EXPORT ctrace_bool ctrace_can_signal_safe_unwind(void);
135 CPPTRACE_EXPORT ctrace_bool ctrace_can_get_safe_object_frame(void);
136
137 /* ctrace::io: */
138 CPPTRACE_EXPORT ctrace_owning_string ctrace_stacktrace_to_string(const ctrace_stacktrace* trace, ctrace_bool use_color);
139 CPPTRACE_EXPORT void ctrace_print_stacktrace(const ctrace_stacktrace* trace, FILE* to, ctrace_bool use_color);
140
141 /* ctrace::utility: */
142 CPPTRACE_EXPORT ctrace_owning_string ctrace_demangle(const char* mangled);
143 CPPTRACE_EXPORT int ctrace_stdin_fileno(void);
144 CPPTRACE_EXPORT int ctrace_stderr_fileno(void);
145 CPPTRACE_EXPORT int ctrace_stdout_fileno(void);
146 CPPTRACE_EXPORT ctrace_bool ctrace_isatty(int fd);
147
148 CPPTRACE_EXPORT ctrace_object_frame ctrace_get_object_info(const ctrace_stacktrace_frame* frame);
149
150 /* ctrace::config: */
151 typedef enum {
152 /* Only minimal lookup tables */
153 ctrace_prioritize_memory = 0,
154 /* Build lookup tables but don't keep them around between trace calls */
155 ctrace_hybrid = 1,
156 /* Build lookup tables as needed */
157 ctrace_prioritize_speed = 2
158 } ctrace_cache_mode;
159 CPPTRACE_EXPORT void ctrace_set_cache_mode(ctrace_cache_mode mode);
160 CPPTRACE_EXPORT void ctrace_enable_inlined_call_resolution(ctrace_bool enable);
161
162 CTRACE_END_DEFINITIONS
163
164 #endif