Skip to content

Commit 5012ede

Browse files
authored
fix: backtrace dump (#1057)
1 parent 67595b7 commit 5012ede

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+13307
-9727
lines changed

core/include/ten_utils/backtrace/backtrace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include <stddef.h>
1515
#include <stdint.h>
1616

17-
#ifdef __cplusplus
17+
#if defined(__cplusplus)
1818
extern "C" {
1919
#endif
2020

2121
TEN_UTILS_API void ten_backtrace_dump_global(size_t skip);
2222

23-
#ifdef __cplusplus
23+
#if defined(__cplusplus)
2424
} /* End extern "C". */
2525
#endif

core/include/ten_utils/log/log.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
__FILE__, __LINE__, __VA_ARGS__); \
5151
} while (0)
5252

53+
#define TEN_LOGM(...) \
54+
do { \
55+
ten_log_log_formatted(&ten_global_log, TEN_LOG_LEVEL_MANDATORY, __func__, \
56+
__FILE__, __LINE__, __VA_ARGS__); \
57+
} while (0)
58+
5359
#define TEN_LOGV_AUX(log, ...) \
5460
do { \
5561
ten_log_log_formatted(log, TEN_LOG_LEVEL_VERBOSE, __func__, __FILE__, \
@@ -86,6 +92,12 @@
8692
__LINE__, __VA_ARGS__); \
8793
} while (0)
8894

95+
#define TEN_LOGM_AUX(log, ...) \
96+
do { \
97+
ten_log_log_formatted(log, TEN_LOG_LEVEL_MANDATORY, __func__, __FILE__, \
98+
__LINE__, __VA_ARGS__); \
99+
} while (0)
100+
89101
typedef enum TEN_LOG_LEVEL {
90102
TEN_LOG_LEVEL_INVALID,
91103

@@ -95,6 +107,8 @@ typedef enum TEN_LOG_LEVEL {
95107
TEN_LOG_LEVEL_WARN,
96108
TEN_LOG_LEVEL_ERROR,
97109
TEN_LOG_LEVEL_FATAL,
110+
111+
TEN_LOG_LEVEL_MANDATORY
98112
} TEN_LOG_LEVEL;
99113

100114
typedef struct ten_string_t ten_string_t;

core/include_internal/ten_runtime/engine/engine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct ten_engine_t {
5151
// TODO(Wei): Perhaps this variable can be removed.
5252
bool is_ready_to_handle_msg;
5353

54+
ten_string_t graph_name;
55+
5456
// When app creates an engine, it will create a randomized graph ID for the
5557
// engine. It _must_ be a UUID4 string.
5658
ten_string_t graph_id;
@@ -112,3 +114,6 @@ TEN_RUNTIME_PRIVATE_API bool ten_engine_is_ready_to_handle_msg(
112114

113115
TEN_RUNTIME_PRIVATE_API const char *ten_engine_get_id(ten_engine_t *self,
114116
bool check_thread);
117+
118+
TEN_RUNTIME_PRIVATE_API void ten_engine_set_graph_name(ten_engine_t *self,
119+
const char *name);

core/include_internal/ten_runtime/extension_thread/extension_thread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef struct ten_extension_thread_t {
4646
ten_signature_t signature;
4747
ten_sanitizer_thread_check_t thread_check;
4848

49+
int64_t tid;
50+
4951
TEN_EXTENSION_THREAD_STATE state;
5052
bool is_close_triggered;
5153

core/include_internal/ten_utils/backtrace/backtrace.h

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
#include <stddef.h>
1515
#include <stdint.h>
1616

17-
#ifdef __cplusplus
17+
#define MAX_CAPTURED_CALL_STACK_DEPTH 128
18+
19+
#if defined(__cplusplus)
1820
extern "C" {
1921
#endif
2022

23+
// This is a virtual type, only used as the type for parameters of public
24+
// functions.
2125
typedef struct ten_backtrace_t ten_backtrace_t;
2226

2327
/**
@@ -38,7 +42,7 @@ typedef struct ten_backtrace_t ten_backtrace_t;
3842
* @note The @a filename and @a function buffers may become invalid after this
3943
* function returns.
4044
*/
41-
typedef int (*ten_backtrace_dump_file_line_func_t)(
45+
typedef int (*ten_backtrace_on_dump_file_line_func_t)(
4246
ten_backtrace_t *ten_backtrace, uintptr_t pc, const char *filename,
4347
int lineno, const char *function, void *data);
4448

@@ -55,7 +59,7 @@ typedef int (*ten_backtrace_dump_file_line_func_t)(
5559
* @note @a symname will be NULL if no error occurred but the symbol could not
5660
* be found.
5761
*/
58-
typedef void (*ten_backtrace_dump_syminfo_func_t)(
62+
typedef void (*ten_backtrace_on_dump_syminfo_func_t)(
5963
ten_backtrace_t *ten_backtrace, uintptr_t pc, const char *sym_name,
6064
uintptr_t sym_val, uintptr_t sym_size, void *data);
6165

@@ -78,52 +82,52 @@ typedef void (*ten_backtrace_dump_syminfo_func_t)(
7882
* but the function requires a symbol table (e.g., backtrace_syminfo). This may
7983
* be used as a signal that some other approach should be tried.
8084
*/
81-
typedef void (*ten_backtrace_error_func_t)(ten_backtrace_t *self,
82-
const char *msg, int errnum,
83-
void *data);
85+
typedef void (*ten_backtrace_on_error_func_t)(ten_backtrace_t *self,
86+
const char *msg, int errnum,
87+
void *data);
8488

8589
/**
8690
* @brief Given @a pc, a program counter in the current program, call the
87-
* @a dump_file_line_cb function with filename, line number, and function name
91+
* @a on_dump_file_line function with filename, line number, and function name
8892
* information. This will normally call the callback function exactly once.
8993
* However, if the @a pc happens to describe an inlined call, and the debugging
9094
* information contains the necessary information, then this may call the
9195
* callback function multiple times. This will make at least one call to either
92-
* @a dump_file_line_cb or @a error_cb.
96+
* @a on_dump_file_line or @a on_error.
9397
*
94-
* @return The first non-zero value returned by @a dump_file_line_cb or @a
95-
* error_cb, or 0.
98+
* @return The first non-zero value returned by @a on_dump_file_line or @a
99+
* on_error, or 0.
96100
*/
97-
TEN_UTILS_API int ten_backtrace_get_file_line_info(
101+
TEN_UTILS_PRIVATE_API int ten_backtrace_get_file_line_info(
98102
ten_backtrace_t *self, uintptr_t pc,
99-
ten_backtrace_dump_file_line_func_t dump_file_line_cb,
100-
ten_backtrace_error_func_t error_cb, void *data);
103+
ten_backtrace_on_dump_file_line_func_t on_dump_file_line,
104+
ten_backtrace_on_error_func_t on_error, void *data);
101105

102106
/**
103107
* @brief Given @a pc, an address or program counter in the current program,
104108
* call the callback information with the symbol name and value describing the
105109
* function or variable in which @a pc may be found.
106-
* This will call either @a dump_syminfo_cb or @a error_cb exactly once.
110+
* This will call either @a on_dump_syminfo or @a on_error exactly once.
107111
*
108112
* @return 1 on success, 0 on failure.
109113
*
110114
* @note This function requires the symbol table but does not require the debug
111115
* info. Note that if the symbol table is present but @a pc could not be found
112-
* in the table, @a dump_syminfo_cb will be called with a NULL @a sym_name
116+
* in the table, @a on_dump_syminfo will be called with a NULL @a sym_name
113117
* argument. Returns 1 on success, 0 on error.
114118
*/
115-
TEN_UTILS_API int ten_backtrace_get_syminfo(
119+
TEN_UTILS_PRIVATE_API int ten_backtrace_get_syminfo(
116120
ten_backtrace_t *self, uintptr_t pc,
117-
ten_backtrace_dump_syminfo_func_t dump_syminfo_cb,
118-
ten_backtrace_error_func_t error_cb, void *data);
121+
ten_backtrace_on_dump_syminfo_func_t on_dump_syminfo,
122+
ten_backtrace_on_error_func_t on_error, void *data);
119123

120124
TEN_UTILS_API void ten_backtrace_create_global(void);
121125

122-
TEN_UTILS_API ten_backtrace_t *ten_backtrace_create(void);
126+
TEN_UTILS_PRIVATE_API ten_backtrace_t *ten_backtrace_create(void);
123127

124128
TEN_UTILS_API void ten_backtrace_destroy_global(void);
125129

126-
TEN_UTILS_API void ten_backtrace_destroy(ten_backtrace_t *self);
130+
TEN_UTILS_PRIVATE_API void ten_backtrace_destroy(ten_backtrace_t *self);
127131

128132
/**
129133
* @brief Get a full stack backtrace.
@@ -139,8 +143,9 @@ TEN_UTILS_API void ten_backtrace_destroy(ten_backtrace_t *self);
139143
* 'dump' callback or 'error' callback.
140144
* @note This function requires debug info for the executable.
141145
*/
142-
TEN_UTILS_API void ten_backtrace_dump(ten_backtrace_t *self, size_t skip);
146+
TEN_UTILS_PRIVATE_API void ten_backtrace_dump(ten_backtrace_t *self,
147+
size_t skip);
143148

144-
#ifdef __cplusplus
149+
#if defined(__cplusplus)
145150
} /* End extern "C". */
146151
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
#pragma once
8+
9+
#include "ten_utils/ten_config.h"
10+
11+
#include <stddef.h>
12+
13+
typedef struct ten_backtrace_t ten_backtrace_t;
14+
15+
typedef struct ten_backtrace_buffer_t {
16+
char *data;
17+
size_t capacity;
18+
size_t length;
19+
int overflow;
20+
} ten_backtrace_buffer_t;
21+
22+
TEN_UTILS_PRIVATE_API void ten_backtrace_buffer_init(
23+
ten_backtrace_buffer_t *self, char *data, size_t capacity);
24+
25+
TEN_UTILS_PRIVATE_API int ten_backtrace_buffer_dump(
26+
ten_backtrace_t *self, uintptr_t pc, const char *filename, int lineno,
27+
const char *function, void *data);

core/include_internal/ten_utils/backtrace/common.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111
#include "include_internal/ten_utils/backtrace/backtrace.h"
1212

1313
typedef struct ten_backtrace_common_t {
14-
ten_backtrace_dump_file_line_func_t dump_cb;
15-
ten_backtrace_error_func_t error_cb;
14+
ten_backtrace_on_dump_file_line_func_t on_dump_file_line;
15+
ten_backtrace_on_error_func_t on_error;
1616
void *cb_data; // The user-defined argument to the above callback functions.
1717
} ten_backtrace_common_t;
1818

1919
TEN_UTILS_PRIVATE_API ten_backtrace_t *g_ten_backtrace;
2020

2121
TEN_UTILS_PRIVATE_API void ten_backtrace_common_init(
22-
ten_backtrace_common_t *self, ten_backtrace_dump_file_line_func_t dump_cb,
23-
ten_backtrace_error_func_t error_cb);
22+
ten_backtrace_common_t *self,
23+
ten_backtrace_on_dump_file_line_func_t on_dump_file_line,
24+
ten_backtrace_on_error_func_t on_error);
2425

2526
TEN_UTILS_PRIVATE_API void ten_backtrace_common_deinit(ten_backtrace_t *self);
2627

27-
TEN_UTILS_PRIVATE_API int ten_backtrace_default_dump_cb(
28+
TEN_UTILS_PRIVATE_API int ten_backtrace_default_dump(
2829
ten_backtrace_t *self, uintptr_t pc, const char *filename, int lineno,
2930
const char *function, void *data);
3031

31-
TEN_UTILS_PRIVATE_API void ten_backtrace_default_error_cb(ten_backtrace_t *self,
32-
const char *msg,
33-
int errnum,
34-
void *data);
32+
TEN_UTILS_PRIVATE_API void ten_backtrace_default_error(ten_backtrace_t *self,
33+
const char *msg,
34+
int errnum, void *data);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
#pragma once
8+
9+
#include "ten_utils/ten_config.h"
10+
11+
#include <stdbool.h>
12+
#include <stddef.h>
13+
14+
#define NORMALIZE_PATH_BUF_SIZE 4096
15+
16+
/**
17+
* @brief Normalizes a file path by resolving '..' and '.' path components.
18+
*
19+
* @param path The input path to normalize
20+
* @param normalized_path Buffer to receive the normalized path
21+
* @param buffer_size Size of the normalized_path buffer
22+
* @return true on success, false if buffer is too small or path is invalid
23+
*/
24+
TEN_UTILS_API bool ten_backtrace_normalize_path(const char *path,
25+
char *normalized_path,
26+
size_t buffer_size);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
#pragma once
8+
9+
#include "ten_utils/ten_config.h"
10+
11+
#include "include_internal/ten_utils/backtrace/common.h"
12+
13+
/**
14+
* @note On Mac, we are currently using a simple method instead of a complicated
15+
* posix method to dump backtrace. So we only need a field of
16+
* 'ten_backtrace_common_t'.
17+
*/
18+
typedef struct ten_backtrace_mac_t {
19+
ten_backtrace_common_t common;
20+
} ten_backtrace_mac_t;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
#pragma once
8+
9+
#include "ten_utils/ten_config.h"
10+
11+
#include <stddef.h>
12+
#include <stdio.h>
13+
14+
#include "include_internal/ten_utils/backtrace/backtrace.h"
15+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/abbreviation.h" // IWYU pragma: keep
16+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/attribute.h" // IWYU pragma: keep
17+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/buf.h" // IWYU pragma: keep
18+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/form.h" // IWYU pragma: keep
19+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/function.h" // IWYU pragma: keep
20+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/line.h" // IWYU pragma: keep
21+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/range_list.h" // IWYU pragma: keep
22+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/section.h" // IWYU pragma: keep
23+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/tag.h" // IWYU pragma: keep
24+
#include "include_internal/ten_utils/backtrace/platform/posix/dwarf_internal/unit.h" // IWYU pragma: keep
25+
26+
#define IS_DIR_SEPARATOR(c) ((c) == '/')
27+
#define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0]))
28+
29+
TEN_UTILS_PRIVATE_API uint64_t read_address(ten_backtrace_t *self,
30+
dwarf_buf *buf, int addrsize);
31+
32+
TEN_UTILS_PRIVATE_API bool advance(ten_backtrace_t *self, dwarf_buf *buf,
33+
size_t count);
34+
35+
TEN_UTILS_PRIVATE_API uint16_t read_uint16(ten_backtrace_t *self,
36+
dwarf_buf *buf);
37+
38+
TEN_UTILS_PRIVATE_API uint32_t read_uint24(ten_backtrace_t *self,
39+
dwarf_buf *buf);
40+
41+
TEN_UTILS_PRIVATE_API uint32_t read_uint32(ten_backtrace_t *self,
42+
dwarf_buf *buf);
43+
44+
TEN_UTILS_PRIVATE_API uint64_t read_uint64(ten_backtrace_t *self,
45+
dwarf_buf *buf);
46+
47+
TEN_UTILS_PRIVATE_API uint64_t read_offset(ten_backtrace_t *self,
48+
dwarf_buf *buf, int is_dwarf64);
49+
50+
TEN_UTILS_PRIVATE_API uint64_t read_uleb128(ten_backtrace_t *self,
51+
dwarf_buf *buf);
52+
53+
TEN_UTILS_PRIVATE_API int64_t read_sleb128(ten_backtrace_t *self,
54+
dwarf_buf *buf);
55+
56+
TEN_UTILS_PRIVATE_API const char *read_string(ten_backtrace_t *self,
57+
dwarf_buf *buf);
58+
59+
TEN_UTILS_PRIVATE_API unsigned char read_byte(ten_backtrace_t *self,
60+
dwarf_buf *buf);
61+
62+
TEN_UTILS_PRIVATE_API signed char read_sbyte(ten_backtrace_t *self,
63+
dwarf_buf *buf);
64+
65+
TEN_UTILS_PRIVATE_API void dwarf_buf_error(ten_backtrace_t *self,
66+
dwarf_buf *buf, const char *msg,
67+
int errnum);
68+
69+
TEN_UTILS_PRIVATE_API int resolve_string(
70+
ten_backtrace_t *self, const dwarf_sections *dwarf_sections, int is_dwarf64,
71+
int is_bigendian, uint64_t str_offsets_base, const attr_val *val,
72+
ten_backtrace_on_error_func_t on_error, void *data, const char **string);
73+
74+
TEN_UTILS_PRIVATE_API uint64_t read_initial_length(ten_backtrace_t *self,
75+
dwarf_buf *buf,
76+
int *is_dwarf64);
77+
78+
TEN_UTILS_PRIVATE_API int resolve_addr_index(
79+
ten_backtrace_t *self, const dwarf_sections *dwarf_sections,
80+
uint64_t addr_base, int addrsize, int is_bigendian, uint64_t addr_index,
81+
ten_backtrace_on_error_func_t on_error, void *data, uintptr_t *address);
82+
83+
TEN_UTILS_PRIVATE_API const char *read_referenced_name_from_attr(
84+
ten_backtrace_t *self, dwarf_data *ddata, unit *u, attr *attr,
85+
attr_val *val, ten_backtrace_on_error_func_t on_error, void *data);
86+
87+
TEN_UTILS_PRIVATE_API int build_address_map(
88+
ten_backtrace_t *self, uintptr_t base_address,
89+
const dwarf_sections *dwarf_sections, int is_bigendian, dwarf_data *altlink,
90+
ten_backtrace_on_error_func_t on_error, void *data,
91+
struct unit_addrs_vector *addrs, struct unit_vector *unit_vec);

0 commit comments

Comments
 (0)