Skip to content

Commit

Permalink
Add tests for usdt library probes
Browse files Browse the repository at this point in the history
Adding runtime tests that ensure we can list and probe tracepoints not
present in the main binary, but rather in linked shared libraries.
  • Loading branch information
xdrop committed Nov 10, 2020
1 parent 9dc91aa commit 89799f9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,14 @@ foreach(testprog ${testprogs})
if(HAVE_SYSTEMTAP_SYS_SDT_H)
target_compile_definitions(${bin_name} PRIVATE HAVE_SYSTEMTAP_SYS_SDT_H)
endif(HAVE_SYSTEMTAP_SYS_SDT_H)
set_target_properties( ${bin_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testprogs/ COMPILE_FLAGS "-g -O0" LINK_FLAGS "-no-pie")
set_target_properties(${bin_name} PROPERTIES LINK_SEARCH_START_STATIC FALSE LINK_SEARCH_END_STATIC FALSE RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testprogs/ COMPILE_FLAGS "-g -O0" LINK_FLAGS "-no-pie")
list(APPEND compiled_testprogs ${CMAKE_CURRENT_BINARY_DIR}/testprogs/${bin_name})
endforeach()
add_custom_target(testprogs DEPENDS ${compiled_testprogs})

target_include_directories(usdt_lib PUBLIC ${CMAKE_SOURCE_DIR}/tests/testlibs/)
target_link_libraries(usdt_lib -shared usdt_tp)

# Similarly compile all test libs
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testlibs/)
file(GLOB testlibs testlibs/*.c)
Expand All @@ -211,6 +214,9 @@ foreach(testlib_source ${testlibs})
get_filename_component(testlib_name ${testlib_source} NAME_WE)
add_library(${testlib_name} SHARED ${testlib_source})
set_target_properties(${testlib_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testlibs/ COMPILE_FLAGS "-g -O0")
if(HAVE_SYSTEMTAP_SYS_SDT_H)
target_compile_definitions(${testlib_name} PRIVATE HAVE_SYSTEMTAP_SYS_SDT_H)
endif(HAVE_SYSTEMTAP_SYS_SDT_H)
# clear the executable bit
add_custom_command(TARGET ${testlib_name}
POST_BUILD
Expand All @@ -219,6 +225,8 @@ foreach(testlib_source ${testlibs})
endforeach()
add_custom_target(testlibs DEPENDS ${compiled_testlibs})

target_compile_options(usdt_tp PRIVATE -fPIC)

configure_file(runtime-tests.sh runtime-tests.sh COPYONLY)
file(GLOB runtime_tests runtime/*)
list(REMOVE_ITEM runtime_tests ${CMAKE_CURRENT_SOURCE_DIR}/runtime/engine)
Expand Down
14 changes: 14 additions & 0 deletions tests/runtime/usdt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ TIMEOUT 5
BEFORE ./testprogs/usdt_test
REQUIRES ./testprogs/usdt_test should_not_skip

NAME "usdt probes - lists linked library probes by pid"
RUN bpftrace -l 'usdt:*' -p $(pidof usdt_lib)
EXPECT libusdt_tp.so:tracetestlib:lib_probe_1
TIMEOUT 5
BEFORE ./testprogs/usdt_lib
REQUIRES ./testprogs/usdt_lib should_not_skip

NAME "usdt probes - filter probes by file on provider"
RUN bpftrace -l 'usdt:./testprogs/usdt_test:tracetest2:*'
EXPECT usdt:./testprogs/usdt_test:tracetest2:testprobe2
Expand Down Expand Up @@ -62,6 +69,13 @@ BEFORE ./testprogs/usdt_test
REQUIRES ./testprogs/usdt_test should_not_skip
TIMEOUT 5

NAME "usdt probes - attach to fully specified library probe by pid"
RUN bpftrace -e 'usdt:./testlibs/libusdt_tp.so.so:tracetestlib:lib_probe_1 { printf("here\n" ); exit(); }' -p $(pidof usdt_lib)
BEFORE ./testprogs/usdt_lib
EXPECT here
TIMEOUT 5
REQUIRES ./testprogs/usdt_lib should_not_skip

NAME "usdt probes - all probes by wildcard and file"
RUN bpftrace -e 'usdt:./testprogs/usdt_test:* { printf("here\n" ); exit(); }'
EXPECT Attaching 3 probes...
Expand Down
19 changes: 19 additions & 0 deletions tests/testlibs/usdt_tp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifdef HAVE_SYSTEMTAP_SYS_SDT_H
#include <sys/sdt.h>
#else
#define DTRACE_PROBE2(a, b, c, d) (void)0
#endif
#include "usdt_tp.h"
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

long myclock()
{
struct timeval tv;
gettimeofday(&tv, NULL);
DTRACE_PROBE2(tracetestlib, lib_probe_1, tv.tv_sec, "Hello world");
DTRACE_PROBE2(tracetestlib, lib_probe_1, tv.tv_sec, "Hello world2");
DTRACE_PROBE2(tracetestlib2, lib_probe_2, tv.tv_sec, "Hello world3");
return tv.tv_sec;
}
4 changes: 4 additions & 0 deletions tests/testlibs/usdt_tp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef _USDTLIBX_H_
#define _USDTLIBX_H_
extern long myclock();
#endif
20 changes: 20 additions & 0 deletions tests/testprogs/usdt_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "usdt_tp.h"
#include <stdio.h>

int main(int argc, char **argv __attribute__((unused)))
{
if (argc > 1)
// If we don't have Systemtap headers, we should skip USDT tests. Returning 1
// can be used as validation in the REQUIRE
#ifndef HAVE_SYSTEMTAP_SYS_SDT_H
return 1;
#else
return 0;
#endif

while (1)
{
myclock();
}
return 0;
}

0 comments on commit 89799f9

Please sign in to comment.