[lttng-dev] instrumenting shared library

Thibault, Daniel Daniel.Thibault at drdc-rddc.gc.ca
Thu Apr 3 12:05:17 EDT 2014


----------------------------------------------------------------------
Date: Thu, 3 Apr 2014 16:55:38 +0530
From: Anand Neeli <anand.neeli at gmail.com>
To: "lttng-dev at lists.lttng.org" <lttng-dev at lists.lttng.org>

> I have a library which is instrumented and tracepoints are added. And I
> have 2 binary's (or daemons) which try to include this library. (one binary
> has instrumented code with tracepoints and other binary doesn't use
> tracing)
>
> I found that i need to instrument the daemon which includes the
> instrumented library for the code to compile.
>
> Can't I have a daemon (without tracing functionality) which can include an
> instrumented library (i don't want to use tracing functionality in this
> case)
>
> I'm only able to build if both library and daemon which includes it are
> instrumented.
>
> Can anyone please give me more details on how to do this?
> This is useful for huge code bases where you can selectively add tracing
> support to few libraries or daemons.
>
> Anand Neeli

   It can be done.  An uninstrumented app can rely on an instrumented shared object; whether the latter produces lttng events then depends on whether or not the tracepoint provider shared object is loaded when the app is invoked (using LD_PRELOAD).

   Here is sample_neeli.c:
#####
#include <stdio.h> //For printf
#include <unistd.h>

#include "cobject.h"
int main(int argc, char **argv)
{
	int i = 0;
	char themessage[20]; //Can hold up to "Hello World 9999999\0"

	fprintf(stderr, "sample_neeli starting\n");
	for (i = 0; i < 10000; i++) {
		//We either prepare themessage in this module, or rely on an
		//external shared object
		cobject_message(themessage, i);
		usleep(1);
	}
	fprintf(stderr, "sample_neeli done\n");
	return 0;
}
#####
cobject.c:
#####
#include <stdio.h> //For printf
#include <unistd.h>

#include "tp.h"

void cobject_message(char *themessage, int i)
{
	tracepoint(sample_component, event, "cobject_message");
	sprintf(themessage, "CObject %u", i);
}
#####
cobject.h:
#####
extern void cobject_message(char *themessage, int i);
#####
The tracepoint provider code should be easy to guess.

The relevant Makefile fragments:
#####
CC = gcc
LIBDL = -ldl # On Linux
#LIBDL = -lc # On BSD
LOCAL_CPPFLAGS += -I. $(AM_CPPFLAGS)
LDFLAGS += -L/usr/local/lib $(AM_LDFLAGS)
SOFLAGS = -fPIC
TP_DEFINE = -D TRACEPOINT_DEFINE
TP_DEFINE_DYNAMIC = $(TP_DEFINE) -D TRACEPOINT_PROBE_DYNAMIC_LINKAGE
SOVERSION_MAJOR = 1
SOVERSION_MINOR = 0

libcobject.so: libcobject.o
	@echo "~~~~~~Packaging $@:"
	$(CC) -shared -Wl,-soname,$@.$(SOVERSION_MAJOR) -Wl,-no-as-needed \
	   -o $@.$(SOVERSION_MAJOR).$(SOVERSION_MINOR) $^
	ln -sf $@.$(SOVERSION_MAJOR).$(SOVERSION_MINOR) $@.$(SOVERSION_MAJOR)
	ln -sf $@.$(SOVERSION_MAJOR) $@

libcobject.o: cobject.c cobject.h tp.h
	@echo "~~~~~~Compiling $@:"
	$(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(SOFLAGS) $(TP_DEFINE_DYNAMIC) \
	   -c -o $@ $<

neeli_dynamic_lib:: neeli_dynamic_lib.o
	@echo "~~~~~~Linking sample_$@:"
	$(CC) -Wl,-no-as-needed -o $@ -L. -lcobject $^ $(LDFLAGS) \
	   $(LIBDL) -Wl,-rpath,'$$ORIGIN',--enable-new-dtags
	@echo "   Use '[LD_PRELOAD=./libtp.so] ./$@' to run $@"

neeli_dynamic_lib.o: sample_neeli.c cobject.h
	@echo "~~~~~~Compiling $@:"
	$(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) -c -o $@ $<
#####

   Running './neeli_dynamic_lib' yields no events.  Running 'LD_PRELOAD=./libtp.so ./neeli_dynamic_lib' yields the libcobject.so events.

Daniel U. Thibault
Protection des systèmes et contremesures (PSC) | Systems Protection & Countermeasures (SPC)
Cyber sécurité pour les missions essentielles (CME) | Mission Critical Cyber Security (MCCS)
R & D pour la défense Canada - Valcartier (RDDC Valcartier) | Defence R&D Canada - Valcartier (DRDC Valcartier)
2459 route de la Bravoure
Québec QC  G3J 1X5
CANADA
Vox : (418) 844-4000 x4245
Fax : (418) 844-4538
NAC : 918V QSDJ <http://www.travelgis.com/map.asp?addr=918V%20QSDJ>
Gouvernement du Canada | Government of Canada
<http://www.valcartier.drdc-rddc.gc.ca/>



More information about the lttng-dev mailing list