From c8ee9a836314ea16da2c178657acefbfe320b339 Mon Sep 17 00:00:00 2001 From: Joe Watkins Date: Thu, 28 Feb 2019 23:09:09 +0100 Subject: [PATCH 1/3] add cmake build --- CMakeLists.txt | 34 ++++++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9d668b0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,34 @@ +CMAKE_MINIMUM_REQUIRED (VERSION 2.6) + +SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Default Build Type") + +PROJECT (phpspy) + +ADD_CUSTOM_TARGET(submodules + COMMAND git submodule update --init --remote --recursive) + +ADD_CUSTOM_TARGET(libtermbox + COMMAND ${PROJECT_SOURCE_DIR}/vendor/termbox/waf configure --prefix=${PROJECT_BUILD_DIR} + COMMAND ${PROJECT_SOURCE_DIR}/vendor/termbox/waf --targets=termbox_static + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/termbox + DEPENDS submodules) + +INCLUDE_DIRECTORIES(.) +INCLUDE_DIRECTORIES(vendor) +INCLUDE_DIRECTORIES(vendor/termbox/src) + +ADD_EXECUTABLE (phpspy phpspy.c pgrep.c top.c addr_objdump.c event_fout.c) +ADD_DEPENDENCIES (phpspy libtermbox) + +TARGET_LINK_LIBRARIES (phpspy pthread ${PROJECT_SOURCE_DIR}/vendor/termbox/build/src/libtermbox.a) + +INSTALL (TARGETS phpspy + RUNTIME DESTINATION bin) + +INSTALL (FILES ${PROJECT_SOURCE_DIR}/phpspy.h + DESTINATION include/phpspy) + +SET_TARGET_PROPERTIES(phpspy PROPERTIES + COMPILE_FLAGS "-std=c99 -Wall -Werror" +) + diff --git a/README.md b/README.md index e473006..a688ebf 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,20 @@ All with no changes to your application and minimal overhead. $ make phpspy_dynamic # Dynamically link dependencies $ # or $ USE_ZEND=1 make ... # Use Zend structs instead of built-in structs (requires php-dev or php-devel) + +### CMake Build + +The CMake will update submodules and create a build of phpspy statically linked to libtermbox: + + $ mkdir build + $ cd build + $ cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release + $ make -j4 install + +CMake Options: + + * `CMAKE_BUILD_TYPE` may be `Release` or `Debug` + * `CMAKE_INSTALL_PREFIX` will set install prefix ### Usage $ ./phpspy -h From 18a016655a02a410c359c073575e32faf5b9966b Mon Sep 17 00:00:00 2001 From: Joe Watkins Date: Thu, 28 Feb 2019 23:09:09 +0100 Subject: [PATCH 2/3] add cmake build --- CMakeLists.txt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..35daaa0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,46 @@ +CMAKE_MINIMUM_REQUIRED (VERSION 2.6) + +SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Default Build Type") + +PROJECT (phpspy) + +ADD_CUSTOM_TARGET(submodules + COMMAND git submodule update --init --remote --recursive) + +ADD_CUSTOM_TARGET(libtermbox + COMMAND ${PROJECT_SOURCE_DIR}/vendor/termbox/waf configure --prefix=${PROJECT_BUILD_DIR} + COMMAND ${PROJECT_SOURCE_DIR}/vendor/termbox/waf --targets=termbox_static + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/termbox + DEPENDS submodules) + +INCLUDE_DIRECTORIES(.) +INCLUDE_DIRECTORIES(vendor) +INCLUDE_DIRECTORIES(vendor/termbox/src) + +ADD_EXECUTABLE (phpspy phpspy.c pgrep.c top.c addr_objdump.c event_fout.c) +ADD_DEPENDENCIES (phpspy libtermbox) + +TARGET_LINK_LIBRARIES (phpspy pthread ${PROJECT_SOURCE_DIR}/vendor/termbox/build/src/libtermbox.a) + +INSTALL (TARGETS phpspy + RUNTIME DESTINATION bin) + +INSTALL (FILES ${PROJECT_SOURCE_DIR}/phpspy.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/php_structs_70.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/php_structs_71.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/php_structs_72.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/php_structs_73.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/php_structs_74.h + DESTINATION include/phpspy) +INSTALL (FILES ${PROJECT_SOURCE_DIR}/vendor/uthash.h + DESTINATION include/phpspy) + +SET_TARGET_PROPERTIES(phpspy PROPERTIES + COMPILE_FLAGS "-std=c99 -Wall -Werror" +) + diff --git a/README.md b/README.md index e473006..a688ebf 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,20 @@ All with no changes to your application and minimal overhead. $ make phpspy_dynamic # Dynamically link dependencies $ # or $ USE_ZEND=1 make ... # Use Zend structs instead of built-in structs (requires php-dev or php-devel) + +### CMake Build + +The CMake will update submodules and create a build of phpspy statically linked to libtermbox: + + $ mkdir build + $ cd build + $ cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release + $ make -j4 install + +CMake Options: + + * `CMAKE_BUILD_TYPE` may be `Release` or `Debug` + * `CMAKE_INSTALL_PREFIX` will set install prefix ### Usage $ ./phpspy -h From 8b58ac46e0ad7573ddbe5caa834b42357e4267ac Mon Sep 17 00:00:00 2001 From: Joe Watkins Date: Fri, 1 Mar 2019 09:39:24 +0100 Subject: [PATCH 3/3] basic module thing --- module.c | 20 ++++++++++++++++++++ phpspy.c | 7 +++++-- phpspy.h | 9 ++++++++- top.c | 3 ++- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 module.c diff --git a/module.c b/module.c new file mode 100644 index 0000000..dfdee76 --- /dev/null +++ b/module.c @@ -0,0 +1,20 @@ +/* + Requires CMake build of phpspy (for headers) + + compile: gcc -shared -fPIC -Wl,-init,phpspy_module_init ../module.c -I/opt/include/phpspy -o module.so + run : sudo LD_PRELOAD=./module.so /opt/bin/phpspy -p PID + + TODO: + make cmake build for module +*/ + +#include + +int phpspy_module_handler(trace_context_t *context, int type) { + fprintf(stderr, "event: %d\n", type); + return 0; +} + +void phpspy_module_init() { + phpspy_event_handler = phpspy_module_handler; +} diff --git a/phpspy.c b/phpspy.c index 131532a..e524331 100644 --- a/phpspy.c +++ b/phpspy.c @@ -1,4 +1,5 @@ -#include "phpspy.h" +#include +#include pid_t opt_pid = -1; char *opt_pgrep_args = NULL; @@ -259,7 +260,9 @@ int main_pid(pid_t pid) { memset(&context, 0, sizeof(trace_context_t)); context.target.pid = pid; - context.event_handler = event_handler_fout; /* TODO set based on option */ + context.event_handler = phpspy_event_handler ? + phpspy_event_handler : event_handler_fout; + try(rv, find_addresses(&context.target)); try(rv, context.event_handler(&context, PHPSPY_TRACE_EVENT_INIT)); diff --git a/phpspy.h b/phpspy.h index b63c2e3..944e35b 100644 --- a/phpspy.h +++ b/phpspy.h @@ -23,7 +23,6 @@ #include #include #include -#include #include #ifdef USE_ZEND @@ -77,6 +76,12 @@ #define IS_REFERENCE 10 #endif +#if defined(__GNUC__) && __GNUC__ >= 4 +# define PHPSPY_API __attribute__ ((visibility("default"))) +#else +# define PHPSPY_API +#endif + typedef struct varpeek_var_s { char name[PHPSPY_STR_SIZE]; UT_hash_handle hh; @@ -177,4 +182,6 @@ extern void usage(FILE *fp, int exit_code); extern int get_symbol_addr(addr_memo_t *memo, pid_t pid, const char *symbol, uint64_t *raddr); extern int event_handler_fout(struct trace_context_s *context, int event_type); +PHPSPY_API int (*phpspy_event_handler)(trace_context_t *, int); + #endif diff --git a/top.c b/top.c index 12ac035..65f35e4 100644 --- a/top.c +++ b/top.c @@ -1,4 +1,5 @@ -#include "phpspy.h" +#include +#include #define FUNC_SIZE 256 #define BUF_SIZE 512