From 1622ba2253fbbfff8cd0845fd7b543a8cd2ec9bd Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 12 Dec 2011 23:47:24 -0500 Subject: [PATCH] Rename liblttng-ust-malloc to liblttng-ust-libc, update to lttng-ust 2.0 - includes a fix to tracepoints to disable the tracepoint sites upon unregistration of the library. This is useful for cases where, like the libc wrapper, the library dependency for constructor/destructor is not quite right due to cross-dependencies (lttng-ust depends on malloc/free). This ensures the session teardown will never be in a position where it could teardown data structures still in use by probes that cannot be unregistered because their associated library would already have called its destructor. Signed-off-by: Mathieu Desnoyers --- Makefile.am | 1 + README | 7 +-- configure.ac | 2 +- liblttng-ust-libc/Makefile.am | 9 ++++ liblttng-ust-libc/README | 9 ++++ .../lttng-ust-malloc.c | 17 +++---- liblttng-ust-libc/run | 3 ++ liblttng-ust-libc/ust_libc.h | 51 +++++++++++++++++++ liblttng-ust-malloc/Makefile.am | 9 ---- liblttng-ust-malloc/README | 9 ---- liblttng-ust-malloc/run | 3 -- liblttng-ust/tracepoint.c | 34 ++++++++++++- 12 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 liblttng-ust-libc/Makefile.am create mode 100644 liblttng-ust-libc/README rename liblttng-ust-malloc/mallocwrap.c => liblttng-ust-libc/lttng-ust-malloc.c (86%) create mode 100755 liblttng-ust-libc/run create mode 100644 liblttng-ust-libc/ust_libc.h delete mode 100644 liblttng-ust-malloc/Makefile.am delete mode 100644 liblttng-ust-malloc/README delete mode 100644 liblttng-ust-malloc/run diff --git a/Makefile.am b/Makefile.am index d7d50791..01acea43 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,6 +4,7 @@ SUBDIRS = . include snprintf libringbuffer liblttng-ust-comm \ liblttng-ust \ liblttng-ust-ctl \ liblttng-ust-fork \ + liblttng-ust-libc \ tests if BUILD_JNI_INTERFACE diff --git a/README b/README index 114e3b25..71d71884 100644 --- a/README +++ b/README @@ -117,9 +117,10 @@ PACKAGE CONTENTS: - tests Various test programs - - liblttng-ust-malloc - An example library that can be LD_PRELOAD'ed to instrument calls to malloc() - in any program without need to recompile it. + - liblttng-ust-libc + An example library that can be LD_PRELOAD'ed to instrument some + calls to libc (currently malloc() and free()) in any program without + need to recompile it. - liblttng-ust-fork A library that is LD_PRELOAD'ed, and that hijacks calls to several system diff --git a/configure.ac b/configure.ac index 56cc5e46..e1542082 100644 --- a/configure.ac +++ b/configure.ac @@ -202,7 +202,6 @@ Use the --with-java-jdk=DIR flag to point to your Java include files, or disable AM_CONDITIONAL([BUILD_JNI_INTERFACE], [test "x$jni_interface" = "xyes"]) #currently disabled. - #liblttng-ust-malloc/Makefile #tests/hello2/Makefile #tests/basic/Makefile #tests/simple_include/Makefile @@ -230,6 +229,7 @@ AC_CONFIG_FILES([ liblttng-ust-ctl/Makefile liblttng-ust-fork/Makefile liblttng-ust-java/Makefile + liblttng-ust-libc/Makefile tests/Makefile tests/hello/Makefile tests/hello.cxx/Makefile diff --git a/liblttng-ust-libc/Makefile.am b/liblttng-ust-libc/Makefile.am new file mode 100644 index 00000000..15ef1573 --- /dev/null +++ b/liblttng-ust-libc/Makefile.am @@ -0,0 +1,9 @@ +AM_CPPFLAGS = -I$(top_srcdir)/include +AM_CFLAGS = -fno-strict-aliasing + +lib_LTLIBRARIES = liblttng-ust-libc.la +liblttng_ust_libc_la_SOURCES = lttng-ust-malloc.c +liblttng_ust_libc_la_LIBADD = -ldl + +noinst_SCRIPTS = run +EXTRA_DIST = run diff --git a/liblttng-ust-libc/README b/liblttng-ust-libc/README new file mode 100644 index 00000000..09e17cb1 --- /dev/null +++ b/liblttng-ust-libc/README @@ -0,0 +1,9 @@ +liblttng-ust-libc is used for instrumenting some calls to libc in a +program, without need for recompiling it. + +This library defines a malloc() function that is instrumented with a +tracepoint. It also calls the libc malloc afterwards. When loaded with +LD_PRELOAD, it replaces the libc malloc() function, in effect +instrumenting all calls to malloc(). The same is performed for free(). + +See the "run" script for a usage example. diff --git a/liblttng-ust-malloc/mallocwrap.c b/liblttng-ust-libc/lttng-ust-malloc.c similarity index 86% rename from liblttng-ust-malloc/mallocwrap.c rename to liblttng-ust-libc/lttng-ust-malloc.c index 6e668569..3212ff0b 100644 --- a/liblttng-ust-malloc/mallocwrap.c +++ b/liblttng-ust-libc/lttng-ust-malloc.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2009 Pierre-Marc Fournier + * Copyright (C) 2011 Mathieu Desnoyers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,7 +21,10 @@ #include #include #include -#include + +#define TRACEPOINT_DEFINE +#define TRACEPOINT_CREATE_PROBES +#include "ust_libc.h" void *malloc(size_t size) { @@ -34,11 +38,8 @@ void *malloc(size_t size) return NULL; } } - retval = plibc_malloc(size); - - ust_marker(malloc, "size %d ptr %p", (int)size, retval); - + tracepoint(ust_libc, malloc, size, retval); return retval; } @@ -53,10 +54,6 @@ void free(void *ptr) return; } } - - ust_marker(free, "ptr %p", ptr); - + tracepoint(ust_libc, free, ptr); plibc_free(ptr); } - -UST_MARKER_LIB diff --git a/liblttng-ust-libc/run b/liblttng-ust-libc/run new file mode 100755 index 00000000..e902cd89 --- /dev/null +++ b/liblttng-ust-libc/run @@ -0,0 +1,3 @@ +#!/bin/sh + +LD_VERBOSE=1 LD_PRELOAD=.libs/liblttng-ust-libc.so $1 diff --git a/liblttng-ust-libc/ust_libc.h b/liblttng-ust-libc/ust_libc.h new file mode 100644 index 00000000..b40548fd --- /dev/null +++ b/liblttng-ust-libc/ust_libc.h @@ -0,0 +1,51 @@ +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER ust_libc + +#if !defined(_TRACEPOINT_UST_LIBC_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _TRACEPOINT_UST_LIBC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Copyright (C) 2011 Mathieu Desnoyers + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED + * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program + * for any purpose, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + */ + +#include + +TRACEPOINT_EVENT(ust_libc, malloc, + TP_ARGS(size_t, size, void *, ptr), + TP_FIELDS( + ctf_integer(size_t, size, size) + ctf_integer_hex(unsigned long, ptr, (unsigned long) ptr) + ) +) + +TRACEPOINT_EVENT(ust_libc, free, + TP_ARGS(void *, ptr), + TP_FIELDS( + ctf_integer_hex(unsigned long, ptr, (unsigned long) ptr) + ) +) + +#endif /* _TRACEPOINT_UST_LIBC_H */ + +#undef TRACEPOINT_INCLUDE_FILE +#define TRACEPOINT_INCLUDE_FILE ./ust_libc.h + +/* This part must be outside ifdef protection */ +#include + +#ifdef __cplusplus +} +#endif diff --git a/liblttng-ust-malloc/Makefile.am b/liblttng-ust-malloc/Makefile.am deleted file mode 100644 index eb2ea753..00000000 --- a/liblttng-ust-malloc/Makefile.am +++ /dev/null @@ -1,9 +0,0 @@ -AM_CPPFLAGS = -I$(top_srcdir)/include -AM_CFLAGS = -fno-strict-aliasing - -lib_LTLIBRARIES = liblttng-ust-malloc.la -liblttng_ust_malloc_la_SOURCES = mallocwrap.c -liblttng_ust_malloc_la_LIBADD = -ldl - -noinst_SCRIPTS = run -EXTRA_DIST = run diff --git a/liblttng-ust-malloc/README b/liblttng-ust-malloc/README deleted file mode 100644 index c58a7e6a..00000000 --- a/liblttng-ust-malloc/README +++ /dev/null @@ -1,9 +0,0 @@ -libustinstr-malloc is used for instrumenting calls to malloc(3) in a program, -without need for recompiling it. - -libustinstr-malloc defines a malloc() function that is instrumented with a -marker. It also calls the libc malloc afterwards. When loaded with LD_PRELOAD, -it replaces the libc malloc() function, in effect instrumenting all calls to -malloc(). - -See the "run" script for a usage example. diff --git a/liblttng-ust-malloc/run b/liblttng-ust-malloc/run deleted file mode 100644 index ce4fd100..00000000 --- a/liblttng-ust-malloc/run +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -LD_VERBOSE=1 LD_LIBRARY_PATH=.:../libust/.libs:../../liburcu LD_PRELOAD=liburcu.so:libust.so:.libs/libmallocwrap.so $1 diff --git a/liblttng-ust/tracepoint.c b/liblttng-ust/tracepoint.c index f146d382..a71e3e6e 100644 --- a/liblttng-ust/tracepoint.c +++ b/liblttng-ust/tracepoint.c @@ -524,6 +524,20 @@ static void new_tracepoints(struct tracepoint * const *start, struct tracepoint } } +static +void lib_disable_tracepoints(struct tracepoint * const *begin, + struct tracepoint * const *end) +{ + struct tracepoint * const *iter; + + for (iter = begin; iter < end; iter++) { + if (!*iter) + continue; /* skip dummy */ + disable_tracepoint(*iter); + } + +} + int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, int tracepoints_count) { @@ -566,18 +580,34 @@ lib_added: int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start) { struct tracepoint_lib *lib; + int tracepoints_count; ust_lock(); cds_list_for_each_entry(lib, &libs, list) { if (lib->tracepoints_start == tracepoints_start) { struct tracepoint_lib *lib2free = lib; + cds_list_del(&lib->list); + tracepoints_count = lib->tracepoints_count; free(lib2free); - break; + goto found; } } + goto end; +found: + /* + * Force tracepoint disarm for all tracepoints of this lib. + * This takes care of destructor of library that would leave a + * LD_PRELOAD wrapper override function enabled for tracing, but + * the session teardown would not be able to reach the + * tracepoint anymore to disable it. + */ + lib_disable_tracepoints(tracepoints_start, + tracepoints_start + tracepoints_count); + DBG("just unregistered a tracepoints section from %p", + tracepoints_start); +end: ust_unlock(); - return 0; } -- 2.34.1