7d33c02d |
1 | |
2 | #include <pthread.h> |
3 | #include <stdio.h> |
4 | #include <unistd.h> |
5 | #include <stdlib.h> |
6 | |
7 | #define LTT_TRACE |
141274aa |
8 | #define LTT_TRACE_FAST |
7d33c02d |
9 | #include <ltt/ltt-facility-user_generic.h> |
10 | |
11 | |
12 | void *thr1(void *arg) |
13 | { |
141274aa |
14 | ltt_thread_init(); /* This init is not required : it will be done |
15 | automatically anyways at the first tracing call site */ |
16 | printf("thread 1, thread id : %lu, pid %lu\n", pthread_self(), getpid()); |
7d33c02d |
17 | |
141274aa |
18 | while(1) { |
7d33c02d |
19 | trace_user_generic_string("Hello world! Have a nice day."); |
20 | sleep(2); |
21 | } |
141274aa |
22 | pthread_exit((void*)1); |
7d33c02d |
23 | } |
24 | |
141274aa |
25 | |
26 | /* Example of a _bad_ thread, which still works with the tracing */ |
7d33c02d |
27 | void *thr2(void *arg) |
28 | { |
141274aa |
29 | /* See ? no init */ |
30 | printf("thread 2, thread id : %lu, pid %lu\n", pthread_self(), getpid()); |
7d33c02d |
31 | sleep(1); |
141274aa |
32 | while(1) { |
7d33c02d |
33 | trace_user_generic_string("Hello world! Have a nice day."); |
141274aa |
34 | sleep(2); |
35 | } |
36 | /* This thread is a bad citizen : returning like this will cause its cancel |
37 | * routines not to be executed. This is still detected by the tracer, but only |
38 | * when the complete process dies. This is not recommended if you create a |
39 | * huge amount of threads */ |
40 | return ((void*)2); |
7d33c02d |
41 | } |
42 | |
43 | |
44 | int main() |
45 | { |
46 | int err; |
47 | pthread_t tid1, tid2; |
48 | void *tret; |
49 | |
50 | printf("Will trace the following string : Hello world! Have a nice day.\n"); |
51 | printf("Press CTRL-C to stop.\n"); |
141274aa |
52 | printf("No file is created with this example : it logs through a kernel\n"); |
53 | printf("system call. See the LTTng lttctl command to start tracing.\n\n"); |
7d33c02d |
54 | |
141274aa |
55 | printf("thread main, thread id : %lu, pid %lu\n", pthread_self(), getpid()); |
56 | err = pthread_create(&tid1, NULL, thr1, NULL); |
57 | if(err!=0) exit(1); |
7d33c02d |
58 | |
141274aa |
59 | err = pthread_create(&tid2, NULL, thr2, NULL); |
60 | if(err!=0) exit(1); |
7d33c02d |
61 | |
141274aa |
62 | err = pthread_join(tid1, &tret); |
63 | if(err!= 0) exit(1); |
7d33c02d |
64 | |
141274aa |
65 | err = pthread_join(tid2, &tret); |
66 | if(err!= 0) exit(1); |
67 | |
68 | return 0; |
7d33c02d |
69 | } |