Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
mkokryashkin committed Dec 5, 2023
1 parent 7817ca8 commit 04f0fed
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 43 deletions.
16 changes: 12 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ set_property(TARGET vm_static vm_shared APPEND PROPERTY
COMPILE_FLAGS "${TARGET_VM_FLAGS}"
)

add_library(stack_collector OBJECT EXCLUDE_FROM_ALL lj_stack_collector.c)
set_property(TARGET stack_collector APPEND PROPERTY
COMPILE_FLAGS "${TARGET_C_FLAGS}"
)
set_property(TARGET stack_collector APPEND PROPERTY
POSITION_INDEPENDENT_CODE ON
)
target_link_libraries(stack_collector PRIVATE ${LIBUNWIND_LIBRARIES})

# Platform core.
add_library(core_static OBJECT EXCLUDE_FROM_ALL ${SOURCES_CORE})
add_library(core_shared OBJECT EXCLUDE_FROM_ALL ${SOURCES_CORE})
Expand All @@ -284,10 +293,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 Expand Up @@ -316,6 +321,9 @@ set_target_properties(libluajit_shared PROPERTIES
)
target_link_libraries(libluajit_shared ${TARGET_LIBS})

target_link_libraries(libluajit_shared stack_collector)
target_link_libraries(libluajit_static stack_collector)

# Compiling and linking CLIs.

# XXX: Order of the branches below matters, since when BUILDMODE
Expand Down
41 changes: 41 additions & 0 deletions src/lj_stack_collector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
** Libunwind-based stack collector.
*/
#include "lj_stack_collector.h"

/*
** 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>

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;
}
18 changes: 18 additions & 0 deletions src/lj_stack_collector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
** Libunwind-based stack collector.
**
** XXX: This module must be compiled and linked separately from
** other LuaJIT sources. Otherwise, the definitions from
** libunwind and libgcc can collide, leading to unwinding working
** improperly. Compiling this module separately ensures that the
** only place where libunwind is used is here.
*/

#ifndef _LJ_STACK_COLLECTOR_H
#define _LJ_STACK_COLLECTOR_H

#include <sys/types.h>

ssize_t collect_stack(void **buffer, int size);

#endif
43 changes: 4 additions & 39 deletions src/lj_sysprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,17 @@
#include "lj_trace.h"
#endif

#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
#include "lj_stack_collector.h"
#endif

#include "lj_wbuf.h"
#include "lj_profile_timer.h"
#include "lj_symtab.h"

#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

/*
** Number of profiler frames we need to omit during stack
** unwinding.
Expand Down Expand Up @@ -97,34 +90,6 @@ static struct sysprof sysprof = {0};

#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] = {};
Expand Down

0 comments on commit 04f0fed

Please sign in to comment.