Skip to content

Commit

Permalink
bisect
Browse files Browse the repository at this point in the history
  • Loading branch information
mkokryashkin committed Dec 4, 2023
1 parent 7817ca8 commit 2c91a7c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 74 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ else()
CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
)
)
AppendFlags(CMAKE_C_FLAGS -fno-omit-frame-pointer -fasynchronous-unwind-tables)
#AppendFlags(CMAKE_C_FLAGS -fno-omit-frame-pointer -fasynchronous-unwind-tables)
if(ENABLE_BUNDLED_LUAJIT)
AppendFlags(TARGET_C_FLAGS -DLUAJIT_EXTERNAL_SYSPROF_BACKTRACER)
else()
Expand Down
9 changes: 5 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})

include(MakeSourceList)

if(${LIBUNWIND_FOUND})
include_directories(${LIBUNWIND_INCLUDE_DIR})
list(APPEND TARGET_LIBS ${LIBUNWIND_LIBRARIES})
endif()

# --- Set OS and arch specific flags -------------------------------------------

include(SetTargetFlags)
Expand Down Expand Up @@ -284,10 +289,6 @@ add_dependencies(core_shared buildvm_output)

list(APPEND TARGET_LIBS m)

if(NOT LUAJIT_DISABLE_SYSPROF)
list(APPEND TARGET_LIBS ${LIBUNWIND_LIBRARIES})
endif()

set(LIB_OBJECTS_STATIC
$<TARGET_OBJECTS:vm_static>
$<TARGET_OBJECTS:core_static>
Expand Down
100 changes: 31 additions & 69 deletions src/lj_sysprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@

#include <pthread.h>
#include <errno.h>

#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
/*
** We only need local unwinding, then a special implementation
** can be selected which may run much faster than the generic
** implementation which supports both kinds of unwinding, local
** and remote.
*/
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#endif
#include <execinfo.h>

/*
** Number of profiler frames we need to omit during stack
Expand Down Expand Up @@ -95,56 +85,6 @@ static struct sysprof sysprof = {0};

/* --- Stream ------------------------------------------------------------- */

#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER

static ssize_t collect_stack(void **buffer, int size)
{
int frame_no = 0;
unw_context_t unw_ctx;
unw_cursor_t unw_cur;

int rc = unw_getcontext(&unw_ctx);
if (rc != 0)
return -1;

rc = unw_init_local(&unw_cur, &unw_ctx);
if (rc != 0)
return -1;

for (; frame_no < size; ++frame_no) {
unw_word_t ip;
rc = unw_get_reg(&unw_cur, UNW_REG_IP, &ip);
if (rc != 0)
return -1;

buffer[frame_no] = (void *)ip;
rc = unw_step(&unw_cur);
if (rc <= 0)
break;
}
return frame_no;
}

static void default_backtrace_host(void *(writer)(int frame_no, void *addr))
{
static void *backtrace_buf[SYSPROF_BACKTRACE_FRAME_MAX] = {};

struct sysprof *sp = &sysprof;
int max_depth = sp->opt.mode == LUAM_SYSPROF_LEAF
? SYSPROF_HANDLER_STACK_DEPTH + 1
: SYSPROF_BACKTRACE_FRAME_MAX;
const int depth = collect_stack(backtrace_buf, max_depth);
int level;

lj_assertX(depth <= max_depth, "backtrace is too deep");
lj_assertX(depth != -1, "failed to collect backtrace");
for (level = SYSPROF_HANDLER_STACK_DEPTH; level < depth; ++level) {
if (!writer(level - SYSPROF_HANDLER_STACK_DEPTH + 1, backtrace_buf[level]))
return;
}
}
#endif

static const uint8_t ljp_header[] = {'l', 'j', 'p', LJP_FORMAT_VERSION,
0x0, 0x0, 0x0};

Expand Down Expand Up @@ -265,6 +205,24 @@ static void *stream_frame_host(int frame_no, void *addr)
return addr;
}

static void default_backtrace_host(void *(writer)(int frame_no, void *addr))
{
static void *backtrace_buf[SYSPROF_BACKTRACE_FRAME_MAX] = {};

struct sysprof *sp = &sysprof;
int max_depth = sp->opt.mode == LUAM_SYSPROF_LEAF
? SYSPROF_HANDLER_STACK_DEPTH + 1
: SYSPROF_BACKTRACE_FRAME_MAX;
const int depth = backtrace(backtrace_buf, max_depth);
int level;

lj_assertX(depth <= max_depth, "depth of C stack is too big");
for (level = SYSPROF_HANDLER_STACK_DEPTH; level < depth; ++level) {
if (!writer(level - SYSPROF_HANDLER_STACK_DEPTH + 1, backtrace_buf[level]))
return;
}
}

static void stream_backtrace_host(struct sysprof *sp)
{
lj_assertX(sp->backtracer != NULL, "uninitialized sysprof backtracer");
Expand Down Expand Up @@ -469,16 +427,20 @@ int lj_sysprof_set_backtracer(luam_Sysprof_backtracer backtracer) {

if (sp->state != SPS_IDLE)
return PROFILE_ERRUSE;

if (backtracer == NULL)
#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
if (backtracer == NULL) {
sp->backtracer = default_backtrace_host;
#else
return PROFILE_ERRUSE;
#endif
else
/*
** XXX: `backtrace` is not signal-safe, according to man,
** because it is lazy loaded on the first call, which triggers
** allocations. We need to call `backtrace` before starting profiling
** to avoid lazy loading.
*/
void *dummy = NULL;
backtrace(&dummy, 1);
}
else {
sp->backtracer = backtracer;

}
if (!is_unconfigured(sp)) {
sp->state = SPS_IDLE;
}
Expand Down

0 comments on commit 2c91a7c

Please sign in to comment.