Skip to content

Commit 4d3cb78

Browse files
committed
fixup
1 parent 7817ca8 commit 4d3cb78

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
lines changed

Diff for: src/CMakeLists.txt

+10-5
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ set_property(TARGET vm_static vm_shared APPEND PROPERTY
264264
COMPILE_FLAGS "${TARGET_VM_FLAGS}"
265265
)
266266

267+
add_library(stack_collector OBJECT EXCLUDE_FROM_ALL lj_stack_collector.c)
268+
set_property(TARGET stack_collector APPEND PROPERTY
269+
COMPILE_FLAGS "${TARGET_C_FLAGS}"
270+
)
271+
set_property(TARGET stack_collector APPEND PROPERTY
272+
POSITION_INDEPENDENT_CODE ON
273+
)
274+
target_link_libraries(stack_collector PRIVATE ${LIBUNWIND_LIBRARIES})
275+
267276
# Platform core.
268277
add_library(core_static OBJECT EXCLUDE_FROM_ALL ${SOURCES_CORE})
269278
add_library(core_shared OBJECT EXCLUDE_FROM_ALL ${SOURCES_CORE})
@@ -284,10 +293,6 @@ add_dependencies(core_shared buildvm_output)
284293

285294
list(APPEND TARGET_LIBS m)
286295

287-
if(NOT LUAJIT_DISABLE_SYSPROF)
288-
list(APPEND TARGET_LIBS ${LIBUNWIND_LIBRARIES})
289-
endif()
290-
291296
set(LIB_OBJECTS_STATIC
292297
$<TARGET_OBJECTS:vm_static>
293298
$<TARGET_OBJECTS:core_static>
@@ -344,7 +349,7 @@ set_target_properties(${LUAJIT_BIN} PROPERTIES
344349
target_include_directories(${LUAJIT_BIN} PRIVATE
345350
${CMAKE_CURRENT_BINARY_DIR}
346351
)
347-
target_link_libraries(${LUAJIT_BIN} ${LUAJIT_LIB} ${TARGET_LIBS})
352+
target_link_libraries(${LUAJIT_BIN} ${LUAJIT_LIB} ${TARGET_LIBS} stack_collector)
348353

349354
# XXX: The variable is used in testing, so PARENT_SCOPE option
350355
# is obligatory.

Diff for: src/lj_stack_collector.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
** Libunwind-based stack collector.
3+
*/
4+
#include "lj_stack_collector.h"
5+
6+
/*
7+
** We only need local unwinding, then a special implementation
8+
** can be selected which may run much faster than the generic
9+
** implementation which supports both kinds of unwinding, local
10+
** and remote.
11+
*/
12+
#define UNW_LOCAL_ONLY
13+
#include <libunwind.h>
14+
15+
ssize_t collect_stack(void **buffer, int size)
16+
{
17+
int frame_no = 0;
18+
unw_context_t unw_ctx;
19+
unw_cursor_t unw_cur;
20+
21+
int rc = unw_getcontext(&unw_ctx);
22+
if (rc != 0)
23+
return -1;
24+
25+
rc = unw_init_local(&unw_cur, &unw_ctx);
26+
if (rc != 0)
27+
return -1;
28+
29+
for (; frame_no < size; ++frame_no) {
30+
unw_word_t ip;
31+
rc = unw_get_reg(&unw_cur, UNW_REG_IP, &ip);
32+
if (rc != 0)
33+
return -1;
34+
35+
buffer[frame_no] = (void *)ip;
36+
rc = unw_step(&unw_cur);
37+
if (rc <= 0)
38+
break;
39+
}
40+
return frame_no;
41+
}

Diff for: src/lj_stack_collector.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
** Libunwind-based stack collector.
3+
**
4+
** XXX: This module must be compiled and linked separately from
5+
** other LuaJIT sources. Otherwise, the definitions from
6+
** libunwind and libgcc can collide, leading to unwinding working
7+
** improperly. Compiling this module separately ensures that the
8+
** only place where libunwind is used is here.
9+
*/
10+
11+
#ifndef _LJ_STACK_COLLECTOR_H
12+
#define _LJ_STACK_COLLECTOR_H
13+
14+
#include <sys/types.h>
15+
16+
ssize_t collect_stack(void **buffer, int size);
17+
18+
#endif

Diff for: src/lj_sysprof.c

+4-39
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,17 @@
2020
#include "lj_trace.h"
2121
#endif
2222

23+
#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
24+
#include "lj_stack_collector.h"
25+
#endif
26+
2327
#include "lj_wbuf.h"
2428
#include "lj_profile_timer.h"
2529
#include "lj_symtab.h"
2630

2731
#include <pthread.h>
2832
#include <errno.h>
2933

30-
#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
31-
/*
32-
** We only need local unwinding, then a special implementation
33-
** can be selected which may run much faster than the generic
34-
** implementation which supports both kinds of unwinding, local
35-
** and remote.
36-
*/
37-
#define UNW_LOCAL_ONLY
38-
#include <libunwind.h>
39-
#endif
40-
4134
/*
4235
** Number of profiler frames we need to omit during stack
4336
** unwinding.
@@ -97,34 +90,6 @@ static struct sysprof sysprof = {0};
9790

9891
#ifndef LUAJIT_EXTERNAL_SYSPROF_BACKTRACER
9992

100-
static ssize_t collect_stack(void **buffer, int size)
101-
{
102-
int frame_no = 0;
103-
unw_context_t unw_ctx;
104-
unw_cursor_t unw_cur;
105-
106-
int rc = unw_getcontext(&unw_ctx);
107-
if (rc != 0)
108-
return -1;
109-
110-
rc = unw_init_local(&unw_cur, &unw_ctx);
111-
if (rc != 0)
112-
return -1;
113-
114-
for (; frame_no < size; ++frame_no) {
115-
unw_word_t ip;
116-
rc = unw_get_reg(&unw_cur, UNW_REG_IP, &ip);
117-
if (rc != 0)
118-
return -1;
119-
120-
buffer[frame_no] = (void *)ip;
121-
rc = unw_step(&unw_cur);
122-
if (rc <= 0)
123-
break;
124-
}
125-
return frame_no;
126-
}
127-
12893
static void default_backtrace_host(void *(writer)(int frame_no, void *addr))
12994
{
13095
static void *backtrace_buf[SYSPROF_BACKTRACE_FRAME_MAX] = {};

0 commit comments

Comments
 (0)