Skip to content

Commit 800e7a5

Browse files
committed
address comments
1 parent 4bcf021 commit 800e7a5

9 files changed

Lines changed: 47 additions & 43 deletions

File tree

interpreter/luajit/offsets_arm64.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515

1616
package luajit // import "go.opentelemetry.io/ebpf-profiler/interpreter/luajit"
1717

18-
// This is CFRAME_SIZE in src/lj_frame.h
19-
// We could dynamically get this from lj_vm_ffi_callback disassembly and look for the
20-
// add to sp register instruction but that is not available in stripped binaries.
18+
import "go.opentelemetry.io/ebpf-profiler/support"
19+
2120
const (
22-
cframeSize int32 = 208
23-
// CFRAME_SIZE_JIT in the luajit source code
24-
// claims this should be the same as CFRAME_SIZE,
25-
// but that's a bug. It's actually indeed also reserving 16 more bytes:
26-
// https://github.com/luajit/luajit/blob/659a6169/src/vm_arm64.dasc#L3949-L3949
21+
cframeSize int32 = support.LJCframeSpaceArm
2722
cframeSizeJIT int32 = cframeSize + 16
2823
)

interpreter/luajit/offsets_x86.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515

1616
package luajit // import "go.opentelemetry.io/ebpf-profiler/interpreter/luajit"
1717

18-
// This is CFRAME_SIZE in src/lj_frame.h
19-
// We could dynamically get this from lj_vm_ffi_callback disassembly and look for:
20-
// lea rax, [rsp+CFRAME_SIZE]
21-
// https://github.com/openresty/luajit2/blob/7952882d/src/vm_x64.dasc#L2725
18+
import "go.opentelemetry.io/ebpf-profiler/support"
19+
2220
const (
23-
cframeSize int32 = 80
21+
cframeSize int32 = support.LJCframeSpaceX86
2422
cframeSizeJIT int32 = cframeSize + 16
2523
)

interpreter/luajit/testdata/lua/sort.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function compare(a, b)
1212
for i=0,1000000 do
1313
local x = i * i
1414
end
15-
-- ngx.say(debug.traceback())
1615
return a[0] - b[0]
1716
end
1817

support/ebpf/luajit.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@
99

1010
// A fake "frame" that just reports the G pointer.
1111
#define LUAJIT_G_REPORT 0xff2
12+
13+
// This is CFRAME_SIZE in src/lj_frame.h
14+
// We could dynamically get this from lj_vm_ffi_callback disassembly and look for:
15+
// lea rax, [rsp+CFRAME_SIZE]
16+
// https://github.com/openresty/luajit2/blob/7952882d/src/vm_x64.dasc#L2725
17+
#define LUAJIT_CFRAME_SPACE_X86_64 80
18+
// This is CFRAME_SIZE in src/lj_frame.h
19+
// We could dynamically get this from lj_vm_ffi_callback disassembly and look for the
20+
// add to sp register instruction but that is not available in stripped binaries.
21+
#define LUAJIT_CFRAME_SPACE_AARCH64 208
22+
23+
#if defined(__x86_64__)
24+
#define LUAJIT_CFRAME_SPACE LUAJIT_CFRAME_SPACE_X86_64
25+
#elif defined(__aarch64__)
26+
#define LUAJIT_CFRAME_SPACE LUAJIT_CFRAME_SPACE_AARCH64
27+
#endif

support/ebpf/luajit_tracer.ebpf.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ struct luajit_procs_t {
3636
__val; \
3737
})
3838

39-
#define L_PART_OFFSET 0x10
40-
#define CFRAME_SIZE_JIT 0x60
39+
#define L_PART_OFFSET 0x10
4140
// (gdb) p/x sizeof(GCproto)
4241
// $4 = 0x68
43-
#define GCPROTO_SIZE 0x68
42+
#define GCPROTO_SIZE 0x68
4443

4544
// This is L offset into interpreter stack frames.
4645
#define L_STACK_OFFSET 0x10
@@ -116,7 +115,7 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
116115

117116
///////// END code copied from luajit2 sources.
118117

119-
static inline __attribute__((__always_inline__)) TValue *frame_prevl(TValue *f, TValue frame_val)
118+
static EBPF_INLINE TValue *frame_prevl(TValue *f, TValue frame_val)
120119
{
121120
// This is the EBPF version of the frame_prevl macro.
122121
// #define frame_prevl(f) ((f) - (1+LJ_FR2+bc_a(frame_pc(f)[-1])))
@@ -131,7 +130,7 @@ static inline __attribute__((__always_inline__)) TValue *frame_prevl(TValue *f,
131130
// there's a bunch of places the return address is stored depending on the frame
132131
// type.
133132
// https://github.com/openresty/luajit2/blob/7952882d/src/lj_debug.c#L53
134-
static inline __attribute__((__always_inline__)) ErrorCode
133+
static EBPF_INLINE ErrorCode
135134
lj_debug_framepc(PerCPURecord *record, void *fn, u32 *startpc, TValue *prevframe, u32 *pc)
136135
{
137136
LJFuncPart *func = &record->luajitUnwindScratch.f;
@@ -252,7 +251,7 @@ lj_debug_framepc(PerCPURecord *record, void *fn, u32 *startpc, TValue *prevframe
252251
// bytecode which we will walk backwards in userland to figure out a name for the
253252
// callee. The callee_pc is for information purposes only, so the user can see where
254253
// execution was.
255-
static inline __attribute__((__always_inline__)) ErrorCode lj_push_frame(
254+
static EBPF_INLINE ErrorCode lj_push_frame(
256255
UnwindState *state, Trace *trace, u64 callee_pt, u64 caller_pt, u32 callee_pc, u32 caller_pc)
257256
{
258257
u64 *data =
@@ -265,7 +264,7 @@ static inline __attribute__((__always_inline__)) ErrorCode lj_push_frame(
265264
return ERR_OK;
266265
}
267266

268-
static inline __attribute__((__always_inline__)) ErrorCode
267+
static EBPF_INLINE ErrorCode
269268
lj_record_frame(PerCPURecord *record, TValue *frame, TValue frame_value, TValue *prevframe)
270269
{
271270
LJScratchSpace *scr = &record->luajitUnwindScratch;
@@ -331,8 +330,7 @@ lj_record_frame(PerCPURecord *record, TValue *frame, TValue frame_value, TValue
331330

332331
// See:
333332
// https://github.com/openresty/luajit2/blob/7952882d/src/lj_frame.h#L33
334-
static inline __attribute__((__always_inline__)) ErrorCode
335-
lj_prev_frame(PerCPURecord *record, TValue frame_val)
333+
static EBPF_INLINE ErrorCode lj_prev_frame(PerCPURecord *record, TValue frame_val)
336334
{
337335
TValue *frame = record->luajitUnwindState.frame;
338336
if (frame_islua(frame_val)) {
@@ -350,18 +348,12 @@ lj_prev_frame(PerCPURecord *record, TValue frame_val)
350348
return ERR_OK;
351349
}
352350

353-
#if defined(__x86_64__)
354-
#define CFRAME_SPACE 80
355-
#elif defined(__aarch64__)
356-
#define CFRAME_SPACE 208
357-
#endif
358-
359351
// Unwind a frame of native code; for example,
360352
// a CFRAME at the C/Lua boundary.
361353
//
362354
// `is_jit`should be true if there is JITted code anywhere in the Lua code corresponding to this
363355
// cframe.
364-
static inline __attribute__((__always_inline__)) ErrorCode
356+
static EBPF_INLINE ErrorCode
365357
unwind_native_frame(const LuaJITProcInfo *info, UnwindState *state, bool is_jit)
366358
{
367359
/* Interpreter frames unwind naturally, we need to poke sp/pc for JIT frames */
@@ -375,7 +367,7 @@ unwind_native_frame(const LuaJITProcInfo *info, UnwindState *state, bool is_jit)
375367
spadjust = info->cframe_size_jit;
376368
}
377369
} else {
378-
spadjust = CFRAME_SPACE;
370+
spadjust = LUAJIT_CFRAME_SPACE;
379371
}
380372

381373
state->sp += spadjust;
@@ -404,7 +396,7 @@ unwind_native_frame(const LuaJITProcInfo *info, UnwindState *state, bool is_jit)
404396
// and finding ones that indicate a function call frame. Code inspired by
405397
// lj_debug_frame.
406398
// https://github.com/openresty/luajit2/blob/7952882d/src/lj_debug.c#L25
407-
static inline __attribute__((__always_inline__)) ErrorCode
399+
static EBPF_INLINE ErrorCode
408400
walk_luajit_stack(PerCPURecord *record, const LuaJITProcInfo *info, int *next_unwinder)
409401
{
410402
bool exitToNative = false;
@@ -434,7 +426,7 @@ walk_luajit_stack(PerCPURecord *record, const LuaJITProcInfo *info, int *next_un
434426
// BASE must always be two elements above the bottom of the stack,
435427
// even when the stack is logically empty. So whenever a new Lua state is created
436428
// (e.g. via luaL_newstate() or lua_newthread()), the interpreter
437-
// pushes two dummy values.
429+
// pushes two dummy values (see https://github.com/luajit/luajit/blob/659a6169/src/lj_state.c#L168-L180).
438430
//
439431
// Thus, when `diff` (set below) is <= 2, we've actually unwound past the logical
440432
// root of the stack, which should never happen...
@@ -554,7 +546,7 @@ walk_luajit_stack(PerCPURecord *record, const LuaJITProcInfo *info, int *next_un
554546
return ERR_OK;
555547
}
556548

557-
static inline __attribute__((__always_inline__)) ErrorCode
549+
static EBPF_INLINE ErrorCode
558550
find_context(struct pt_regs *ctx, PerCPURecord *record, const LuaJITProcInfo *info)
559551
{
560552
bool reportG = false;
@@ -662,7 +654,7 @@ find_context(struct pt_regs *ctx, PerCPURecord *record, const LuaJITProcInfo *in
662654
return ERR_OK;
663655
}
664656

665-
static inline __attribute__((__always_inline__)) int unwind_luajit(struct pt_regs *ctx)
657+
static EBPF_INLINE int unwind_luajit(struct pt_regs *ctx)
666658
{
667659
PerCPURecord *record = get_per_cpu_record();
668660
if (!record)

support/ebpf/tracer.ebpf.amd64

0 Bytes
Binary file not shown.

support/ebpf/tracer.ebpf.arm64

0 Bytes
Binary file not shown.

support/types.go

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

support/types_def.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ const (
225225
)
226226

227227
const (
228-
LJFFIFunc = C.LUAJIT_FFI_FUNC
229-
LJFileId = C.LUAJIT_JIT_FILE_ID
230-
LJNormalFrame = C.LUAJIT_NORMAL_FRAME
231-
LJGReport = C.LUAJIT_G_REPORT
228+
LJFFIFunc = C.LUAJIT_FFI_FUNC
229+
LJFileId = C.LUAJIT_JIT_FILE_ID
230+
LJNormalFrame = C.LUAJIT_NORMAL_FRAME
231+
LJGReport = C.LUAJIT_G_REPORT
232+
LJCframeSpaceX86 = C.LUAJIT_CFRAME_SPACE_X86_64
233+
LJCframeSpaceArm = C.LUAJIT_CFRAME_SPACE_AARCH64
232234
)
233235

234236
var MetricsTranslation = []metrics.MetricID{

0 commit comments

Comments
 (0)