1 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #define event_list "lttng_statedump_start,lttng_statedump_end," \
11 "lttng_statedump_process_state,lttng_statedump_file_descriptor," \
12 "lttng_statedump_vm_map,lttng_statedump_network_interface," \
13 "lttng_statedump_interrupt,sched_process_free," \
14 "sched_switch,sched_process_fork"
15 #define context_list "-t pid -t procname -t tid -t ppid "
18 int check_or_start_sessiond()
23 ret
= system("pgrep -u root lttng-sessiond >/dev/null");
28 fprintf(stderr
, "Trying to start lttng-sessiond with sudo\n");
29 ret
= system("sudo -l lttng-sessiond >/dev/null");
31 fprintf(stderr
, "[error] You are not root and not "
32 "allowed by sudo to start lttng-sessiond\n");
36 ret
= system("sudo -l lttng >/dev/null");
38 fprintf(stderr
, "[error] You are not root and not "
39 "allowed by sudo to use lttng\n");
47 ret
= system("sudo lttng-sessiond -d");
49 ret
= system("lttng-sessiond -d");
52 fprintf(stderr
, "Error starting lttng-sessiond as root\n");
62 int check_or_start_relayd()
66 ret
= system("pgrep lttng-relayd >/dev/null");
70 ret
= system("lttng-relayd -d");
72 fprintf(stderr
, "Error starting lttng-relayd\n");
82 * Return 0 if in tracing group or root, 1 if sudo is needed (and working),
83 * a negative value on error.
86 int check_tracing_group()
94 ret
= system("groups|grep tracing >/dev/null");
99 ret
= system("sudo lttng --version >/dev/null");
101 fprintf(stderr
, "Error executing lttng with sudo, you need to "
102 "be root or in the \"tracing\" group to start "
115 int check_lttng_modules(int sudo
)
120 ret
= system("sudo lttng list -k | grep sched_switch >/dev/null");
122 ret
= system("lttng list -k | grep sched_switch >/dev/null");
125 fprintf(stderr
, "Error listing kernel events, "
126 "lttng-modules might not be installed\n");
135 int check_requirements(int *sudo
)
139 ret
= check_or_start_sessiond();
142 ret
= check_or_start_relayd();
145 ret
= check_tracing_group();
151 ret
= check_lttng_modules(*sudo
);
159 * Allocate a random string, must be freed by the caller.
162 char *random_session_name()
168 FILE *f
= fopen( "/dev/urandom", "r");
174 ret
= fread(&id
, 1, sizeof(uint64_t), f
);
175 if (ret
< sizeof(id
)) {
180 ret
= asprintf(&str
, "lttngtop-%" PRIu64
, id
);
182 fprintf(stderr
, "Error allocating session name");
198 int check_session_name(char *name
, int sudo
)
203 ret
= sprintf(cmd
, "%s lttng list | grep %s >/dev/null",
204 (sudo
) ? "sudo" : " ", name
);
206 fprintf(stderr
, "Allocating cmd\n");
212 fprintf(stderr
, "Error: session %s already exist, either we "
213 "are not random enough or something is "
214 "really wrong\n", name
);
224 int local_session(char *name
, int sudo
)
229 ret
= sprintf(cmd
, "%s lttng create %s >/dev/null",
230 (sudo
) ? "sudo" : " ", name
);
232 fprintf(stderr
, "Allocating cmd\n");
237 fprintf(stderr
, "Error: creating the session\n");
247 int live_local_session(char *name
, int sudo
)
252 ret
= sprintf(cmd
, "%s lttng create %s --live 1000000 -U net://localhost >/dev/null",
253 (sudo
) ? "sudo" : " ", name
);
255 fprintf(stderr
, "Allocating cmd\n");
260 fprintf(stderr
, "Error: creating the session\n");
270 int enable_events(char *name
, int sudo
)
275 ret
= sprintf(cmd
, "%s lttng enable-event -s %s -k %s >/dev/null",
276 (sudo
) ? "sudo" : " ", name
, event_list
);
278 fprintf(stderr
, "Allocating cmd\n");
284 fprintf(stderr
, "Error: enabling events\n");
294 int add_contexts(char *name
, int sudo
)
299 ret
= sprintf(cmd
, "%s lttng add-context -s %s -k %s >/dev/null",
300 (sudo
) ? "sudo" : " ", name
, context_list
);
302 fprintf(stderr
, "allocating cmd\n");
308 fprintf(stderr
, "error: adding contexts\n");
318 int start(char *name
, int sudo
, int local
, int print
)
323 ret
= sprintf(cmd
, "%s lttng start %s >/dev/null",
324 (sudo
) ? "sudo" : " ", name
);
326 fprintf(stderr
, "allocating cmd\n");
332 fprintf(stderr
, "error: starting the session %s\n", name
);
341 ret
= sprintf(cmd
, "%s lttng list|grep %s|cut -d'(' -f2|cut -d ')' -f1",
342 (sudo
) ? "sudo" : " ", name
);
344 ret
= sprintf(cmd
, "babeltrace -i lttng-live net://localhost|grep %s|cut -d' ' -f1",
348 fprintf(stderr
, "allocating cmd\n");
351 fprintf(stderr
, "%s session started : ",
352 (local
) ? "Local" : "Live");
355 fprintf(stderr
, "error: listing the sessions\n");
365 char *live_path(char *name
)
373 ret
= sprintf(cmd
, "lttngtop -r net://localhost|grep %s|cut -d' ' -f1",
376 fprintf(stderr
, "allocating cmd\n");
380 fp
= popen(cmd
, "r");
382 printf("Failed to run command\n" );
386 /* Read the output a line at a time - output it. */
387 out
= fgets(path
, sizeof(path
)-1, fp
);
399 int destroy(char *name
)
406 ret
= system("sudo -l lttng >/dev/null");
408 fprintf(stderr
, "[error] You are not root and not "
409 "allowed by sudo to use lttng\n");
416 ret
= sprintf(cmd
, "%s lttng destroy %s >/dev/null",
417 (sudo
) ? "sudo" : " ", name
);
419 fprintf(stderr
, "allocating cmd\n");
425 fprintf(stderr
, "error: destroying the session %s\n", name
);
434 int create_local_session()
440 ret
= check_requirements(&sudo
);
442 name
= random_session_name();
448 ret
= check_session_name(name
, sudo
);
453 ret
= local_session(name
, sudo
);
458 ret
= enable_events(name
, sudo
);
463 ret
= add_contexts(name
, sudo
);
468 ret
= start(name
, sudo
, 1, 1);
479 int destroy_live_local_session(char *name
)
481 return destroy(name
);
484 int create_live_local_session(char **session_path
, char **session_name
, int print
)
490 ret
= check_requirements(&sudo
);
492 name
= random_session_name();
498 ret
= check_session_name(name
, sudo
);
503 ret
= live_local_session(name
, sudo
);
508 ret
= enable_events(name
, sudo
);
513 ret
= add_contexts(name
, sudo
);
518 ret
= start(name
, sudo
, 0, print
);
524 *session_path
= live_path(name
);
526 *session_name
= name
;
This page took 0.068059 seconds and 5 git commands to generate.