-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathtrace_utils.h
More file actions
730 lines (623 loc) · 16.3 KB
/
trace_utils.h
File metadata and controls
730 lines (623 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DF_ENTERPRISE
#define DF_ENTERPRISE 1
#endif
#ifndef TRACE_UTILS_H
#define TRACE_UTILS_H
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
#include <stdint.h>
#include "consts.h"
#if defined(__x86_64__)
#define FP 6
#endif
#if defined(__aarch64__)
#define FP 29
#endif
#if !(defined(__x86_64__) || defined(__aarch64__))
#define FP 0
#endif
#if defined(__aarch64__)
#define LR 30
#endif
#if defined(DF_ENTERPRISE)
#define LUA_RUNTIME_DETECT_METHOD_LEN 256
#endif
#if defined(DF_ENTERPRISE)
#define LUA_RUNTIME_PATH_LEN 1024
#endif
#if defined(DF_ENTERPRISE)
#define LUA_RUNTIME_VERSION_LEN 32
#endif
#if defined(__x86_64__)
#define SP 7
#endif
#if defined(__aarch64__)
#define SP 31
#endif
#if !(defined(__x86_64__) || defined(__aarch64__))
#define SP 0
#endif
#define UNWIND_ENTRIES_PER_SHARD 65535
#define UNWIND_SHARDS_PER_PROCESS 1024
enum LogLevel {
LOG_LEVEL_ERROR = 1,
LOG_LEVEL_WARN = 2,
LOG_LEVEL_INFO = 3,
LOG_LEVEL_DEBUG = 4,
LOG_LEVEL_TRACE = 5,
};
typedef uint8_t LogLevel;
enum CfaType {
CFA_TYPE_RBP_OFFSET,
CFA_TYPE_RSP_OFFSET,
CFA_TYPE_EXPRESSION,
CFA_TYPE_UNSUPPORTED,
CFA_TYPE_NO_ENTRY,
};
typedef uint8_t CfaType;
enum RegType {
REG_TYPE_UNDEFINED,
REG_TYPE_SAME_VALUE,
REG_TYPE_OFFSET,
REG_TYPE_UNSUPPORTED,
};
typedef uint8_t RegType;
/**
* Return Address recovery type for ARM64
* On x86_64, RA is always at CFA-8 (implicitly handled in BPF code)
* On ARM64, RA may be in LR register or saved on stack
*/
enum RaType {
RA_TYPE_CFA_OFFSET,
RA_TYPE_LR_REGISTER,
RA_TYPE_UNSUPPORTED,
};
typedef uint8_t RaType;
#if defined(DF_ENTERPRISE)
typedef struct lua_unwind_table_t lua_unwind_table_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct php_unwind_table_t php_unwind_table_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct python_unwind_table_t python_unwind_table_t;
#endif
typedef struct unwind_table_t unwind_table_t;
#if defined(DF_ENTERPRISE)
typedef struct v8_unwind_table_t v8_unwind_table_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint32_t kind;
uint8_t version[LUA_RUNTIME_VERSION_LEN];
uint8_t detection_method[LUA_RUNTIME_DETECT_METHOD_LEN];
uint8_t path[LUA_RUNTIME_PATH_LEN];
} lua_runtime_info_t;
#endif
typedef struct {
uint32_t id;
uint16_t entry_start;
uint16_t entry_end;
uint64_t offset;
uint64_t pc_min;
uint64_t pc_max;
} shard_info_t;
typedef struct {
uint16_t len;
shard_info_t entries[UNWIND_SHARDS_PER_PROCESS];
} process_shard_list_t;
typedef struct {
uint64_t pc;
CfaType cfa_type;
RegType rbp_type;
#if defined(__aarch64__)
RaType ra_type
#endif
;
int16_t cfa_offset;
int16_t rbp_offset;
#if defined(__aarch64__)
int16_t ra_offset
#endif
;
} unwind_entry_t;
typedef struct {
uint32_t id;
uint32_t len;
unwind_entry_t entries[UNWIND_ENTRIES_PER_SHARD];
} unwind_entry_shard_t;
/**
* Thread Specific Data info for accessing per-thread PyThreadState.
*/
typedef struct {
/**
* Offset from thread pointer base (TPBASE) to TSD storage
*/
int16_t offset;
/**
* TSD key multiplier (glibc=16, musl=8)
*/
uint8_t multiplier;
/**
* Whether indirect addressing is needed (musl=1, glibc=0)
*/
uint8_t indirect;
} tsd_info_t;
#if defined(DF_ENTERPRISE)
typedef struct {
uint64_t auto_tls_key_addr;
uint16_t version;
tsd_info_t tsd_info;
uint8_t offsets_id;
uint8_t _padding[5];
uint64_t none_struct_addr;
} python_unwind_info_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t current_frame;
} py_cframe_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t co_filename;
int64_t co_firstlineno;
int64_t co_name;
int64_t co_varnames;
} py_code_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t f_back;
int64_t f_code;
int64_t f_lineno;
int64_t f_localsplus;
} py_frame_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t owner;
} py_interpreter_frame_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t tstate_head;
} py_interpreter_state_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t ob_type;
} py_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t interp_main;
} py_runtime_state_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t data;
int64_t size;
} py_string_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t cframe;
int64_t frame;
int64_t interp;
int64_t native_thread_id;
int64_t next;
int64_t thread_id;
} py_thread_state_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t ob_item;
} py_tuple_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int64_t tp_name;
} py_type_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
py_cframe_t cframe;
py_code_object_t code_object;
py_frame_object_t frame_object;
py_interpreter_frame_t interpreter_frame;
py_interpreter_state_t interpreter_state;
py_object_t object;
py_runtime_state_t runtime_state;
py_string_t string;
py_thread_state_t thread_state;
py_tuple_object_t tuple_object;
py_type_object_t type_object;
} python_offsets_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t offsets_id;
uint8_t reserved[7];
uint64_t state_address;
} lua_unwind_info_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint32_t features;
uint32_t off_l_ci;
uint32_t off_l_base_ci;
uint32_t off_l_end_ci;
uint32_t off_ci_func;
uint32_t off_ci_top;
uint32_t off_ci_savedpc;
uint32_t off_ci_prev;
uint32_t off_tvalue_tt;
uint32_t off_tvalue_val;
uint32_t off_closure_isc;
uint32_t off_lclosure_p;
uint32_t off_cclosure_f;
uint32_t off_proto_source;
uint32_t off_proto_linedefined;
uint32_t off_proto_code;
uint32_t off_proto_sizecode;
uint32_t off_proto_lineinfo;
uint32_t off_proto_abslineinfo;
uint32_t off_tstring_len;
uint32_t sizeof_tstring;
uint32_t sizeof_callinfo;
uint32_t sizeof_tvalue;
} lua_ofs;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t fr2;
uint8_t gc64;
uint16_t pad;
uint32_t tv_sz;
uint32_t off_l_base;
uint32_t off_l_stack;
uint32_t off_gcproto_firstline;
uint32_t off_gcproto_chunkname;
uint32_t off_gcstr_data;
uint32_t off_gcfunc_cfunc;
uint32_t off_gcfunc_ffid;
uint32_t off_gcfunc_pc;
uint32_t off_gcproto_bc;
uint32_t off_gcstr_len;
uint32_t off_l_glref;
uint32_t off_global_state_dispatchmode;
} lj_ofs;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint64_t executor_globals_address;
uint64_t jit_return_address;
uint64_t execute_ex_start;
uint64_t execute_ex_end;
uint8_t offsets_id;
uint8_t has_jit;
uint8_t reserved[6];
} php_unwind_info_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t current_execute_data;
} php_executor_globals_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t opline;
uint8_t function;
uint8_t this_type_info;
uint8_t prev_execute_data;
} php_execute_data_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t common_type;
uint8_t common_funcname;
uint8_t common_scope;
uint32_t op_array_filename;
uint32_t op_array_linestart;
uint32_t sizeof_struct;
} php_function_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint64_t val;
} php_string_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t lineno;
} php_op_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint64_t name;
} php_class_entry_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
php_executor_globals_t executor_globals;
php_execute_data_t execute_data;
php_function_t function;
php_string_t string;
php_op_t op;
php_class_entry_t class_entry;
} php_offsets_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint64_t isolate_address;
uint8_t offsets_id;
uint32_t version;
} v8_unwind_info_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
int16_t marker;
int16_t function;
int16_t bytecode_offset;
} v8_frame_pointers_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t shared;
uint16_t code;
} v8_js_function_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t name_or_scope_info;
uint16_t function_data;
uint16_t script_or_debug_info;
} v8_shared_function_info_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t instruction_start;
uint16_t instruction_size;
uint16_t flags;
uint16_t deoptimization_data;
uint16_t source_position_table;
} v8_code_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t name;
uint16_t source;
} v8_script_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t source_position_table;
} v8_bytecode_array_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t scope_info;
uint16_t shared_function_info;
uint16_t js_function_first;
uint16_t js_function_last;
uint16_t string_first;
uint16_t script;
uint16_t code;
} v8_type_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t first_nonstring_type;
uint16_t string_representation_mask;
uint16_t seq_string_tag;
uint16_t cons_string_tag;
uint16_t thin_string_tag;
} v8_fixed_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t first_vars;
uint8_t n_context_locals;
} v8_scope_info_index_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t inlined_function_count;
uint8_t literal_array;
uint8_t shared_function_info;
uint8_t inlining_positions;
} v8_deoptimization_data_index_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t map;
} v8_heap_object_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint16_t instance_type;
} v8_map_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint8_t entry_frame;
uint8_t construct_entry_frame;
uint8_t exit_frame;
uint8_t wasm_frame;
uint8_t wasm_to_js_frame;
uint8_t wasm_to_js_function_frame;
uint8_t js_to_wasm_frame;
uint8_t wasm_debug_break_frame;
uint8_t stack_switch_frame;
uint8_t wasm_exit_frame;
uint8_t c_wasm_entry_frame;
uint8_t wasm_compile_lazy_frame;
uint8_t wasm_liftoff_setup_frame;
uint8_t interpreted_frame;
uint8_t baseline_frame;
uint8_t maglev_frame;
uint8_t turbofan_frame;
uint8_t stub_frame;
uint8_t turbofan_stub_with_context_frame;
uint8_t builtin_continuation_frame;
uint8_t js_builtin_continuation_frame;
uint8_t js_builtin_continuation_with_catch_frame;
uint8_t internal_frame;
uint8_t construct_frame;
uint8_t fast_construct_frame;
uint8_t builtin_frame;
uint8_t builtin_exit_frame;
uint8_t native_frame;
uint8_t api_callback_exit_frame;
uint8_t irregexp_frame;
uint8_t optimized_frame;
} v8_frame_types_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
uint32_t mask;
uint8_t shift;
uint8_t baseline;
uint8_t interpreted;
} v8_codekind_t;
#endif
#if defined(DF_ENTERPRISE)
typedef struct {
v8_frame_pointers_t frame_pointers;
v8_js_function_t js_function;
v8_shared_function_info_t shared_function_info;
v8_code_t code;
v8_script_t script;
v8_bytecode_array_t bytecode_array;
v8_type_t v8_type;
v8_fixed_t v8_fixed;
v8_scope_info_index_t scope_info_index;
v8_deoptimization_data_index_t deopt_data_index;
v8_heap_object_t heap_object;
v8_map_t map;
v8_frame_types_t frame_types;
v8_codekind_t codekind;
} v8_offsets_t;
#endif
bool frame_pointer_heuristic_check(uint32_t pid);
#if defined(DF_ENTERPRISE)
extern int32_t is_lua_process(uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern bool is_php_process(uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern bool is_python_process(uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern bool is_v8_process(uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern int32_t lua_detect(uint32_t pid, lua_runtime_info_t *out);
#endif
#if defined(DF_ENTERPRISE)
extern void lua_set_map_fds(int32_t lang_flags_fd,
int32_t unwind_info_fd,
int32_t lua_offsets_fd,
int32_t luajit_offsets_fd);
#endif
#if defined(DF_ENTERPRISE)
extern lua_unwind_table_t *lua_unwind_table_create(int32_t lang_flags_fd,
int32_t unwind_info_fd,
int32_t lua_offsets_fd,
int32_t luajit_offsets_fd);
#endif
#if defined(DF_ENTERPRISE)
extern void lua_unwind_table_destroy(lua_unwind_table_t *table);
#endif
#if defined(DF_ENTERPRISE)
extern void lua_unwind_table_load(lua_unwind_table_t *table, uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern void lua_unwind_table_unload(lua_unwind_table_t *table, uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern php_unwind_table_t *php_unwind_table_create(int32_t unwind_info_map_fd,
int32_t offsets_map_fd);
#endif
#if defined(DF_ENTERPRISE)
extern void php_unwind_table_destroy(php_unwind_table_t *table);
#endif
#if defined(DF_ENTERPRISE)
extern void php_unwind_table_load(php_unwind_table_t *table, uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern void php_unwind_table_unload(php_unwind_table_t *table, uint32_t pid);
#endif
int32_t protect_cpu_affinity_c(void);
#if defined(DF_ENTERPRISE)
extern python_unwind_table_t *python_unwind_table_create(int32_t unwind_info_map_fd,
int32_t offsets_map_fd);
#endif
#if defined(DF_ENTERPRISE)
extern void python_unwind_table_destroy(python_unwind_table_t *table);
#endif
#if defined(DF_ENTERPRISE)
extern void python_unwind_table_load(python_unwind_table_t *table, uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern void python_unwind_table_unload(python_unwind_table_t *table, uint32_t pid);
#endif
int32_t read_offset_of_stack_in_task_struct(void);
/**
* C-callable function to read TPBASE offset
*/
int64_t read_tpbase_offset(void);
#if defined(DF_ENTERPRISE)
extern char *resolve_php_frame(uint32_t pid,
uint64_t zend_function_ptr,
uint64_t lineno,
uint64_t is_jit);
#endif
#if defined(DF_ENTERPRISE)
extern char *resolve_v8_frame(uint32_t pid,
uint64_t pointer_and_type,
uint64_t delta_or_marker,
uint64_t sfi_fallback);
#endif
void rust_log_wrapper(LogLevel level,
int error_number,
const char *msg,
const char *_function_name,
const char *file_path,
int line_number);
int rustc_demangle(const char *mangled, char *out, size_t out_size);
unwind_table_t *unwind_table_create(int32_t process_shard_list_map_fd,
int32_t unwind_entry_shard_map_fd);
void unwind_table_destroy(unwind_table_t *table);
void unwind_table_load(unwind_table_t *table, uint32_t pid);
void unwind_table_unload(unwind_table_t *table, uint32_t pid);
void unwind_table_unload_all(unwind_table_t *table);
#if defined(DF_ENTERPRISE)
extern v8_unwind_table_t *v8_unwind_table_create(int32_t unwind_info_map_fd);
#endif
#if defined(DF_ENTERPRISE)
extern void v8_unwind_table_destroy(v8_unwind_table_t *table);
#endif
#if defined(DF_ENTERPRISE)
extern void v8_unwind_table_load(v8_unwind_table_t *table, uint32_t pid);
#endif
#if defined(DF_ENTERPRISE)
extern void v8_unwind_table_unload(v8_unwind_table_t *table, uint32_t pid);
#endif
#endif /* TRACE_UTILS_H */