Fix: Metadata stream leak when received in consumer

Between threads, when the metadata stream is received, it is allocated.
We now pass the pointer to the metadata thread thus fixing a memory leak
because the original stream was never freed.

This commit also modified some debug statements and remove a duplicate
code snippet.

Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: off-by-one in comm proto between lttng-ctl and sessiond

Mostly affects setting a filter on an event with large event name.

This fix breaks the ABI between lttng-ctl and lttng-sessiond, both
should be updated together, and sessiond must be restarted.

Fixes #357

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
New testpoint mechanism to instrument binaries for testing

This commit introduce two new macros: TESTPOINT_DECL(name) and
testpoint(name).

Here a quick example that show how to use the testpoint mechanism:

file: main.c

/* Testpoint declaration */
TESTPOINT_DECL(interesting_function)

void interesting_function(void)
{
        testpoint(interesting_function);
        /* Some processing that can fail */
        ...
}

int main(int argc, char *argv[])
{
        interesting_function();
        ...
        printf("End");
}

file: testpoint.c
void __testpoint_interesting_function(void)
{
        printf("In testpoint of interesting function!");
}

Compile:
gcc -o test main.c
gcc -fPIC -shared -o testpoint.so testpoint.c

Run:
> ./test
  End
> export LTTNG_TESTPOINT_ENABLE=1
> LD_PRELOAD=testpoint.so ./test
  In testpoint of interesting function!
  End
> export LTTNG_TESTPOINT_ENABLE=0
> LD_PRELOAD=testpoint.so ./test
  End

The testpoint mechanism is triggered via the preloading of a shared
object containing the appropriate testpoint symbols and by setting the
LTTNG_TESTPOINT_ENABLE environment variable.

The check on this environment variable is done on the application
startup with the help of a constructor (lttng_testpoint_check) which
toggle a global state variable indicating whether or not the testpoints
should be activated.

When enabled, the testpoint() macro calls an underlying wrapper specific
to the testpoint and simply try to lookup the testpoint symbol via a
dlsym() call.

When disabled, the testpoint() call will only incur an additionnal test
per testpoint on a global variable. This performance 'hit' should be
acceptable for production use.

The testpoint mechanism should be *always on*. It can be explicitly
disabled via CFLAGS="-DNTESTPOINT" in a way similar to NDEBUG and
assert().

Please refer to 0005-testpoint-mechanism.txt for more information.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Add testpoints in lttng-sessiond for each threads

This commit adds 8 new testpoints in the lttng-sessiond binary. These
testpoints rely on the testpoints infrastructure introduced recently.

Testpoints:

thread_manage_clients
thread_manage_clients_before_loop
thread_registration_apps
thread_manage_apps
thread_manage_apps_before_loop
thread_manage_kernel
thread_manage_kernel_before_loop
thread_manage_consumer

The thread_<thread_name> testpoints are placed directly at the thread
start and they can be used to trigger failure in <thread_name>.

The thread_<thread_name>_before_loop testpoints are placed directly
before the main processing loop of the thread and thus can be used to
stall the processing of the thread.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Tests: Add a health check utility program

The health_check program is a simple utility to query the health
status of the different threads of the sessiond.

Sample output:

> ./health_check
Health check cmd: 0
Health check app. manage: 0
Health check app. registration: 0
Health check kernel: 0
Health check consumer: 0

The return code is encoded to indicate which thread has failed.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Tests: Add health check thread exit test

This test trigger a failure in a specified thread using the recently
introduced testpoint mechanism. The testpoints behavior is implemented
in health_exit.c. The testpoint code simply calls pthread_exit(3) and
effectively "kill" the thread without affecting the other threads
behavior.

The test select the thread to be "killed" by enabling a specific
environment variable.

With this test we ensure that each thread can be succesfully terminated
and that the health check feature properly detect a failure.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Tests: Add health check thread stall test

This test trigger a "code stall" in a specified thread using the
testpoint mechanism. The testpoint behavior is implemented in
health_stall.c. The testpoint code stall a specific thread processing by
calling sleep(3).

The test select the thread to be stalled by enabling a specific
environment variable.

The test ensure the threads can be succesfully stalled and that the
health check feature is able to properly detect stalling in non-polling
cases.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Tests: Add health check tests to configure

Add health folder to top-level tests Makefile.am. Also add a runall
script to run all health check tests.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Build out of src tree

Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix coding style and add/change debug statements

Change all perror() to PERROR() and add some DBG() and ERR() statement
for easier verbose reading.

Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Add arbitrary wait period for kernel streaming test

Once the trace has been destroy, there is a race condition between
reading the trace and the trace being completely written on disk.

We don't have anyway of telling if the streamed trace has been completed
so we have to add an arbitrary wait period so the test can successfully
parse and validate the trace.

I never hit that problem until this morning with my Linux 3.6.0 kernel.
Maybe related or not, still the trace can occur on slower systems.

Signed-off-by: David Goulet <dgoulet@efficios.com>
Tests: Fix LD_PRELOAD library lookup path for health tests

The LD_PRELOAD library lookup was failing when the tests were not runned
in the health directory.

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Add missing call rcu and read side lock

Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Add hash table argument to helper functions

This allows these helper functions to be used more broadly across the
code base and not for a specific hash table.

Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: output number of bytes written by relayd

The print of the number of bytes written in the tracefile by the relayd
was after the call to write the padding, so the debug output was
confusing:

DEBUG3: Receiving data of size 2097133 for stream id 567 seqnum 0 [in
relay_process_data() at main.c:1394]
DEBUG2: Relay wrote 19 bytes to tracefile for stream id 567 [in
relay_process_data() at main.c:1416

Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Stream allocation and insertion consistency

This commit restores the consistency between the allocate and insertion
operations on streams. The allocate stream function was changing the
channel state by updating the refcount, cpucount and number of init
stream. We now moved these operations into the add_stream function so
that the initialization is done there and the del_stream handles the
cleanup.

So basically, any side effect done on a channel using a stream is now
done in the add/del functions. The same was done for the metadata which
is a special case that does not need to set the need update flag.

Furthermore, the consumer_del_stream now can destroy a stream even if
that stream was not successfully added to its hash table. The kernel and
UST consumers now use it on error between allocation and the add_stream
function.

This refactoring fixes memory leaks, bad refcount values and file
descriptor leaks. Also, the metadata destroy stream function was also
fixed to use the waitfd_node which is also fixed in the
consumer_del_metadata_stream that was deleting the wrong node pointer.

The waitfd_node fixes were merged with this commit in order to make the
whole patch works and not failed on make check.

Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
ABI with support for compat 32/64 bits

The current ABI does not work for compat 32/64 bits. This patch moves
the current ABI as old-abi and provides a new ABI in which all the
structures exchanged between user and kernel-space are packed. Also this
new ABI moves the "int overwrite" member of the struct
lttng_kernel_channel to remove the alignment added by the compiler.

A patch for lttng-modules has been developed in parallel to this one to
support the new ABI. These 2 patches have been tested in all possible
configurations (applied or not) on 64-bit and 32-bit kernels (with
CONFIG_COMPAT) and a user-space in 32 and 64-bit.

Here are the results of the tests :
k 64 compat     | u 32 compat     | OK
k 64 compat     | u 64 compat     | OK
k 64 compat     | u 32 non-compat | KO
k 64 compat     | u 64 non-compat | OK

k 64 non-compat | u 64 compat     | OK
k 64 non-compat | u 32 compat     | KO
k 64 non-compat | u 64 non-compat | OK
k 64 non-compat | u 32 non-compat | KO

k 32 compat     | u compat        | OK
k 32 compat     | u non-compat    | OK

k 32 non-compat | u compat        | OK
k 32 non-compat | u non-compat    | OK

The results are as expected :
- on 32-bit user-space and kernel, every configuration works.
- on 64-bit user-space and kernel, every configuration works.
- with 32-bit user-space on a 64-bit kernel the only configuration
  where it works is when the compat patch is applied everywhere.

Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Empty metadata buffer(s) on HUP|ERR

The metadata fd was not emptying all the buffers on POLLHUP | POLLERR.
This patch makes the read subbuffer operation, for hung up metadata fd,
loop until the returned len is a negative value. For user space tracing,
-EAGAIN is returned and -ENODATA for the kernel tracer.

We also fixes another issue where an error returned (not -EAGAIN) by the
read subbuffer function call was stopping the thread. Now, the fd is
removed from the poll set, the stream is freed and the thread continues.

Fixes #378

Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Missing -ENODATA handling in the consumer

Fixes #379

Signed-off-by: David Goulet <dgoulet@efficios.com>
Fix: Relayd and consumerd socket leaks

Using the set_consumer_uri call, the connection is established between
the session daemon and the relayd then sent to the relayd. However, when
start_tracing is initiated, the sessiond creates, if needed, the relayd
sockets and send them to the consumer. A validation was missing in one
of the function call to validate if the sockets were already sent to the
consumer.

Note that using lttng_create_session with a remote URL, the sockets are
created only when tracing starts thus not having this issue.

This fixes leaks on the relayd and consumerd when destroying a session.

Fixes #374 #382

Signed-off-by: David Goulet <dgoulet@efficios.com>