diff --git a/CMakeLists.txt b/CMakeLists.txt index c17239b2..f531bd40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,11 @@ cmake_minimum_required(VERSION 2.6) option(USE_INTERNAL_FPCONV "Use internal strtod() / g_fmt() code for performance") option(MULTIPLE_THREADS "Support multi-threaded apps with internal fpconv - recommended" ON) +option(ENABLE_DTRACE "DTrace support" OFF) + +if(ENABLE_DTRACE) + find_program(DTRACE dtrace) +endif() if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release CACHE STRING @@ -68,9 +73,46 @@ else() set(_lua_module_dir "${_lua_lib_dir}/lua/5.1") endif() -add_library(cjson MODULE lua_cjson.c strbuf.c ${FPCONV_SOURCES}) +# Init empty cjson_obj for adding dtrace.o after generation one +set(cjson_obj) + +add_library(cjson_objs OBJECT lua_cjson.c strbuf.c ${FPCONV_SOURCES}) +set_target_properties(cjson_objs PROPERTIES POSITION_INDEPENDENT_CODE ON) +set(cjson_obj ${cjson_obj} $) + +if(ENABLE_DTRACE AND DTRACE) + message(STATUS "DTrace found and enabled to build with probes") + add_definitions(-DENABLE_DTRACE) + set(D_FILE ${PROJECT_SOURCE_DIR}/cjson_dtrace) + execute_process( + COMMAND ${DTRACE} -h -s ${D_FILE}.d -o ${D_FILE}.h + ) + if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") +# Dirty hack, because isn't supported see for more details: +# http://public.kitware.com/Bug/view.php?id=14447 + set(cjson_c_o ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cjson_objs.dir/lua_cjson.c.o) + set(dtrace_obj ${CMAKE_CURRENT_BINARY_DIR}/dtrace.o) + add_custom_command(OUTPUT ${dtrace_obj} + COMMAND ${DTRACE} -G -s ${D_FILE}.d -o ${dtrace_obj} ${cjson_c_o} + DEPENDS ${cjson_c_o} + ) + set_source_files_properties(${dtrace_obj} + PROPERTIES + EXTERNAL_OBJECT true + GENERATED true + ) + set(_MODULE_LINK ${_MODULE_LINK} elf) + set(cjson_obj ${cjson_obj} ${dtrace_obj}) + unset(cjson_c_o) + unset(dtrace_obj) + unset(D_FILE) + endif() +endif() + +add_library(cjson MODULE ${cjson_obj}) set_target_properties(cjson PROPERTIES PREFIX "") target_link_libraries(cjson ${_MODULE_LINK}) + install(TARGETS cjson DESTINATION "${_lua_module_dir}") # vi:ai et sw=4 ts=4: diff --git a/cjson_dtrace.d b/cjson_dtrace.d new file mode 100644 index 00000000..8ca356a9 --- /dev/null +++ b/cjson_dtrace.d @@ -0,0 +1,4 @@ +provider lua_cjson { + probe start(); + probe end(int, char *); +}; diff --git a/external.h b/external.h new file mode 100644 index 00000000..26061a19 --- /dev/null +++ b/external.h @@ -0,0 +1,8 @@ +#ifdef ENABLE_DTRACE +#include "cjson_dtrace.h" +#else +#define LUA_CJSON_START_ENABLED() (0) +#define LUA_CJSON_START() +#define LUA_CJSON_END_ENABLED() (0) +#define LUA_CJSON_END(arg0, arg1) +#endif diff --git a/lua_cjson.c b/lua_cjson.c index c14a1c5c..63429522 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -43,6 +43,8 @@ #include #include +#include "external.h" + #include "strbuf.h" #include "fpconv.h" @@ -708,6 +710,9 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { + if (LUA_CJSON_START_ENABLED()) + LUA_CJSON_START(); + json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; strbuf_t *encode_buf; @@ -734,6 +739,9 @@ static int json_encode(lua_State *l) if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); + if (LUA_CJSON_END_ENABLED()) + LUA_CJSON_END(len, json); + return 1; } @@ -1258,6 +1266,8 @@ static void json_process_value(lua_State *l, json_parse_t *json, static int json_decode(lua_State *l) { + if (LUA_CJSON_START_ENABLED()) + LUA_CJSON_START(); json_parse_t json; json_token_t token; size_t json_len; @@ -1293,6 +1303,9 @@ static int json_decode(lua_State *l) strbuf_free(json.tmp); + if (LUA_CJSON_END_ENABLED()) + LUA_CJSON_END(json_len, (char *)json.data); + return 1; }