Refactor ust-app create session
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <urcu/compiler.h>
28 #include <lttng/ust-error.h>
29
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32
33 #include "fd-limit.h"
34 #include "health.h"
35 #include "ust-app.h"
36 #include "ust-consumer.h"
37 #include "ust-ctl.h"
38
39 /*
40 * Match function for the hash table lookup.
41 *
42 * It matches an ust app event based on three attributes which are the event
43 * name, the filter bytecode and the loglevel.
44 */
45 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
46 {
47 struct ust_app_event *event;
48 const struct ust_app_ht_key *key;
49
50 assert(node);
51 assert(_key);
52
53 event = caa_container_of(node, struct ust_app_event, node.node);
54 key = _key;
55
56 /* Match the 3 elements of the key: name, filter and loglevel. */
57
58 /* Event name */
59 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
60 goto no_match;
61 }
62
63 /* Event loglevel. */
64 if (event->attr.loglevel != key->loglevel) {
65 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
66 && key->loglevel == 0 && event->attr.loglevel == -1) {
67 /*
68 * Match is accepted. This is because on event creation, the
69 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
70 * -1 are accepted for this loglevel type since 0 is the one set by
71 * the API when receiving an enable event.
72 */
73 } else {
74 goto no_match;
75 }
76 }
77
78 /* One of the filters is NULL, fail. */
79 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
80 goto no_match;
81 }
82
83 if (key->filter && event->filter) {
84 /* Both filters exists, check length followed by the bytecode. */
85 if (event->filter->len != key->filter->len ||
86 memcmp(event->filter->data, key->filter->data,
87 event->filter->len) != 0) {
88 goto no_match;
89 }
90 }
91
92 /* Match. */
93 return 1;
94
95 no_match:
96 return 0;
97 }
98
99 /*
100 * Unique add of an ust app event in the given ht. This uses the custom
101 * ht_match_ust_app_event match function and the event name as hash.
102 */
103 static void add_unique_ust_app_event(struct lttng_ht *ht,
104 struct ust_app_event *event)
105 {
106 struct cds_lfht_node *node_ptr;
107 struct ust_app_ht_key key;
108
109 assert(ht);
110 assert(ht->ht);
111 assert(event);
112
113 key.name = event->attr.name;
114 key.filter = event->filter;
115 key.loglevel = event->attr.loglevel;
116
117 node_ptr = cds_lfht_add_unique(ht->ht,
118 ht->hash_fct(event->node.key, lttng_ht_seed),
119 ht_match_ust_app_event, &key, &event->node.node);
120 assert(node_ptr == &event->node.node);
121 }
122
123 /*
124 * Delete ust context safely. RCU read lock must be held before calling
125 * this function.
126 */
127 static
128 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
129 {
130 if (ua_ctx->obj) {
131 ustctl_release_object(sock, ua_ctx->obj);
132 free(ua_ctx->obj);
133 }
134 free(ua_ctx);
135 }
136
137 /*
138 * Delete ust app event safely. RCU read lock must be held before calling
139 * this function.
140 */
141 static
142 void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
143 {
144 free(ua_event->filter);
145
146 if (ua_event->obj != NULL) {
147 ustctl_release_object(sock, ua_event->obj);
148 free(ua_event->obj);
149 }
150 free(ua_event);
151 }
152
153 /*
154 * Delete ust app stream safely. RCU read lock must be held before calling
155 * this function.
156 */
157 static
158 void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
159 {
160 if (stream->obj) {
161 ustctl_release_object(sock, stream->obj);
162 lttng_fd_put(LTTNG_FD_APPS, 2);
163 free(stream->obj);
164 }
165 free(stream);
166 }
167
168 /*
169 * Delete ust app channel safely. RCU read lock must be held before calling
170 * this function.
171 */
172 static
173 void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
174 {
175 int ret;
176 struct lttng_ht_iter iter;
177 struct ust_app_event *ua_event;
178 struct ust_app_ctx *ua_ctx;
179 struct ltt_ust_stream *stream, *stmp;
180
181 /* Wipe stream */
182 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
183 cds_list_del(&stream->list);
184 delete_ust_app_stream(sock, stream);
185 }
186
187 /* Wipe context */
188 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
189 ret = lttng_ht_del(ua_chan->ctx, &iter);
190 assert(!ret);
191 delete_ust_app_ctx(sock, ua_ctx);
192 }
193 lttng_ht_destroy(ua_chan->ctx);
194
195 /* Wipe events */
196 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
197 node.node) {
198 ret = lttng_ht_del(ua_chan->events, &iter);
199 assert(!ret);
200 delete_ust_app_event(sock, ua_event);
201 }
202 lttng_ht_destroy(ua_chan->events);
203
204 if (ua_chan->obj != NULL) {
205 ustctl_release_object(sock, ua_chan->obj);
206 lttng_fd_put(LTTNG_FD_APPS, 2);
207 free(ua_chan->obj);
208 }
209 free(ua_chan);
210 }
211
212 /*
213 * Delete ust app session safely. RCU read lock must be held before calling
214 * this function.
215 */
216 static
217 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess)
218 {
219 int ret;
220 struct lttng_ht_iter iter;
221 struct ust_app_channel *ua_chan;
222
223 if (ua_sess->metadata) {
224 if (ua_sess->metadata->stream_obj) {
225 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
226 lttng_fd_put(LTTNG_FD_APPS, 2);
227 free(ua_sess->metadata->stream_obj);
228 }
229 if (ua_sess->metadata->obj) {
230 ustctl_release_object(sock, ua_sess->metadata->obj);
231 lttng_fd_put(LTTNG_FD_APPS, 2);
232 free(ua_sess->metadata->obj);
233 }
234 trace_ust_destroy_metadata(ua_sess->metadata);
235 }
236
237 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
238 node.node) {
239 ret = lttng_ht_del(ua_sess->channels, &iter);
240 assert(!ret);
241 delete_ust_app_channel(sock, ua_chan);
242 }
243 lttng_ht_destroy(ua_sess->channels);
244
245 if (ua_sess->handle != -1) {
246 ustctl_release_handle(sock, ua_sess->handle);
247 }
248 free(ua_sess);
249 }
250
251 /*
252 * Delete a traceable application structure from the global list. Never call
253 * this function outside of a call_rcu call.
254 */
255 static
256 void delete_ust_app(struct ust_app *app)
257 {
258 int ret, sock;
259 struct ust_app_session *ua_sess, *tmp_ua_sess;
260
261 rcu_read_lock();
262
263 /* Delete ust app sessions info */
264 sock = app->sock;
265 app->sock = -1;
266
267 lttng_ht_destroy(app->sessions);
268
269 /* Wipe sessions */
270 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
271 teardown_node) {
272 /* Free every object in the session and the session. */
273 delete_ust_app_session(sock, ua_sess);
274 }
275
276 /*
277 * Wait until we have deleted the application from the sock hash table
278 * before closing this socket, otherwise an application could re-use the
279 * socket ID and race with the teardown, using the same hash table entry.
280 *
281 * It's OK to leave the close in call_rcu. We want it to stay unique for
282 * all RCU readers that could run concurrently with unregister app,
283 * therefore we _need_ to only close that socket after a grace period. So
284 * it should stay in this RCU callback.
285 *
286 * This close() is a very important step of the synchronization model so
287 * every modification to this function must be carefully reviewed.
288 */
289 ret = close(sock);
290 if (ret) {
291 PERROR("close");
292 }
293 lttng_fd_put(LTTNG_FD_APPS, 1);
294
295 DBG2("UST app pid %d deleted", app->pid);
296 free(app);
297
298 rcu_read_unlock();
299 }
300
301 /*
302 * URCU intermediate call to delete an UST app.
303 */
304 static
305 void delete_ust_app_rcu(struct rcu_head *head)
306 {
307 struct lttng_ht_node_ulong *node =
308 caa_container_of(head, struct lttng_ht_node_ulong, head);
309 struct ust_app *app =
310 caa_container_of(node, struct ust_app, pid_n);
311
312 DBG3("Call RCU deleting app PID %d", app->pid);
313 delete_ust_app(app);
314 }
315
316 /*
317 * Alloc new UST app session.
318 */
319 static
320 struct ust_app_session *alloc_ust_app_session(void)
321 {
322 struct ust_app_session *ua_sess;
323
324 /* Init most of the default value by allocating and zeroing */
325 ua_sess = zmalloc(sizeof(struct ust_app_session));
326 if (ua_sess == NULL) {
327 PERROR("malloc");
328 goto error;
329 }
330
331 ua_sess->handle = -1;
332 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
333
334 return ua_sess;
335
336 error:
337 return NULL;
338 }
339
340 /*
341 * Alloc new UST app channel.
342 */
343 static
344 struct ust_app_channel *alloc_ust_app_channel(char *name,
345 struct lttng_ust_channel *attr)
346 {
347 struct ust_app_channel *ua_chan;
348
349 /* Init most of the default value by allocating and zeroing */
350 ua_chan = zmalloc(sizeof(struct ust_app_channel));
351 if (ua_chan == NULL) {
352 PERROR("malloc");
353 goto error;
354 }
355
356 /* Setup channel name */
357 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
358 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
359
360 ua_chan->enabled = 1;
361 ua_chan->handle = -1;
362 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
363 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
364 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
365
366 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
367
368 /* Copy attributes */
369 if (attr) {
370 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
371 }
372
373 DBG3("UST app channel %s allocated", ua_chan->name);
374
375 return ua_chan;
376
377 error:
378 return NULL;
379 }
380
381 /*
382 * Alloc new UST app event.
383 */
384 static
385 struct ust_app_event *alloc_ust_app_event(char *name,
386 struct lttng_ust_event *attr)
387 {
388 struct ust_app_event *ua_event;
389
390 /* Init most of the default value by allocating and zeroing */
391 ua_event = zmalloc(sizeof(struct ust_app_event));
392 if (ua_event == NULL) {
393 PERROR("malloc");
394 goto error;
395 }
396
397 ua_event->enabled = 1;
398 strncpy(ua_event->name, name, sizeof(ua_event->name));
399 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
400 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
401
402 /* Copy attributes */
403 if (attr) {
404 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
405 }
406
407 DBG3("UST app event %s allocated", ua_event->name);
408
409 return ua_event;
410
411 error:
412 return NULL;
413 }
414
415 /*
416 * Alloc new UST app context.
417 */
418 static
419 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
420 {
421 struct ust_app_ctx *ua_ctx;
422
423 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
424 if (ua_ctx == NULL) {
425 goto error;
426 }
427
428 if (uctx) {
429 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
430 }
431
432 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
433
434 error:
435 return ua_ctx;
436 }
437
438 /*
439 * Allocate a filter and copy the given original filter.
440 *
441 * Return allocated filter or NULL on error.
442 */
443 static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
444 struct lttng_ust_filter_bytecode *orig_f)
445 {
446 struct lttng_ust_filter_bytecode *filter = NULL;
447
448 /* Copy filter bytecode */
449 filter = zmalloc(sizeof(*filter) + orig_f->len);
450 if (!filter) {
451 PERROR("zmalloc alloc ust app filter");
452 goto error;
453 }
454
455 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
456
457 error:
458 return filter;
459 }
460
461 /*
462 * Find an ust_app using the sock and return it. RCU read side lock must be
463 * held before calling this helper function.
464 */
465 static
466 struct ust_app *find_app_by_sock(int sock)
467 {
468 struct lttng_ht_node_ulong *node;
469 struct lttng_ht_iter iter;
470
471 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
472 node = lttng_ht_iter_get_node_ulong(&iter);
473 if (node == NULL) {
474 DBG2("UST app find by sock %d not found", sock);
475 goto error;
476 }
477
478 return caa_container_of(node, struct ust_app, sock_n);
479
480 error:
481 return NULL;
482 }
483
484 /*
485 * Lookup for an ust app event based on event name, filter bytecode and the
486 * event loglevel.
487 *
488 * Return an ust_app_event object or NULL on error.
489 */
490 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
491 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel)
492 {
493 struct lttng_ht_iter iter;
494 struct lttng_ht_node_str *node;
495 struct ust_app_event *event = NULL;
496 struct ust_app_ht_key key;
497
498 assert(name);
499 assert(ht);
500
501 /* Setup key for event lookup. */
502 key.name = name;
503 key.filter = filter;
504 key.loglevel = loglevel;
505
506 /* Lookup using the event name as hash and a custom match fct. */
507 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
508 ht_match_ust_app_event, &key, &iter.iter);
509 node = lttng_ht_iter_get_node_str(&iter);
510 if (node == NULL) {
511 goto end;
512 }
513
514 event = caa_container_of(node, struct ust_app_event, node);
515
516 end:
517 return event;
518 }
519
520 /*
521 * Create the channel context on the tracer.
522 */
523 static
524 int create_ust_channel_context(struct ust_app_channel *ua_chan,
525 struct ust_app_ctx *ua_ctx, struct ust_app *app)
526 {
527 int ret;
528
529 health_code_update(&health_thread_cmd);
530
531 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
532 ua_chan->obj, &ua_ctx->obj);
533 if (ret < 0) {
534 goto error;
535 }
536
537 ua_ctx->handle = ua_ctx->obj->handle;
538
539 DBG2("UST app context created successfully for channel %s", ua_chan->name);
540
541 error:
542 health_code_update(&health_thread_cmd);
543 return ret;
544 }
545
546 /*
547 * Set the filter on the tracer.
548 */
549 static
550 int set_ust_event_filter(struct ust_app_event *ua_event,
551 struct ust_app *app)
552 {
553 int ret;
554
555 health_code_update(&health_thread_cmd);
556
557 if (!ua_event->filter) {
558 ret = 0;
559 goto error;
560 }
561
562 ret = ustctl_set_filter(app->sock, ua_event->filter,
563 ua_event->obj);
564 if (ret < 0) {
565 goto error;
566 }
567
568 DBG2("UST filter set successfully for event %s", ua_event->name);
569
570 error:
571 health_code_update(&health_thread_cmd);
572 return ret;
573 }
574
575 /*
576 * Disable the specified event on to UST tracer for the UST session.
577 */
578 static int disable_ust_event(struct ust_app *app,
579 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
580 {
581 int ret;
582
583 health_code_update(&health_thread_cmd);
584
585 ret = ustctl_disable(app->sock, ua_event->obj);
586 if (ret < 0) {
587 ERR("UST app event %s disable failed for app (pid: %d) "
588 "and session handle %d with ret %d",
589 ua_event->attr.name, app->pid, ua_sess->handle, ret);
590 goto error;
591 }
592
593 DBG2("UST app event %s disabled successfully for app (pid: %d)",
594 ua_event->attr.name, app->pid);
595
596 error:
597 health_code_update(&health_thread_cmd);
598 return ret;
599 }
600
601 /*
602 * Disable the specified channel on to UST tracer for the UST session.
603 */
604 static int disable_ust_channel(struct ust_app *app,
605 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
606 {
607 int ret;
608
609 health_code_update(&health_thread_cmd);
610
611 ret = ustctl_disable(app->sock, ua_chan->obj);
612 if (ret < 0) {
613 ERR("UST app channel %s disable failed for app (pid: %d) "
614 "and session handle %d with ret %d",
615 ua_chan->name, app->pid, ua_sess->handle, ret);
616 goto error;
617 }
618
619 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
620 ua_chan->name, app->pid);
621
622 error:
623 health_code_update(&health_thread_cmd);
624 return ret;
625 }
626
627 /*
628 * Enable the specified channel on to UST tracer for the UST session.
629 */
630 static int enable_ust_channel(struct ust_app *app,
631 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
632 {
633 int ret;
634
635 health_code_update(&health_thread_cmd);
636
637 ret = ustctl_enable(app->sock, ua_chan->obj);
638 if (ret < 0) {
639 ERR("UST app channel %s enable failed for app (pid: %d) "
640 "and session handle %d with ret %d",
641 ua_chan->name, app->pid, ua_sess->handle, ret);
642 goto error;
643 }
644
645 ua_chan->enabled = 1;
646
647 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
648 ua_chan->name, app->pid);
649
650 error:
651 health_code_update(&health_thread_cmd);
652 return ret;
653 }
654
655 /*
656 * Enable the specified event on to UST tracer for the UST session.
657 */
658 static int enable_ust_event(struct ust_app *app,
659 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
660 {
661 int ret;
662
663 health_code_update(&health_thread_cmd);
664
665 ret = ustctl_enable(app->sock, ua_event->obj);
666 if (ret < 0) {
667 ERR("UST app event %s enable failed for app (pid: %d) "
668 "and session handle %d with ret %d",
669 ua_event->attr.name, app->pid, ua_sess->handle, ret);
670 goto error;
671 }
672
673 DBG2("UST app event %s enabled successfully for app (pid: %d)",
674 ua_event->attr.name, app->pid);
675
676 error:
677 health_code_update(&health_thread_cmd);
678 return ret;
679 }
680
681 /*
682 * Open metadata onto the UST tracer for a UST session.
683 */
684 static int open_ust_metadata(struct ust_app *app,
685 struct ust_app_session *ua_sess)
686 {
687 int ret;
688 struct lttng_ust_channel_attr uattr;
689
690 health_code_update(&health_thread_cmd);
691
692 uattr.overwrite = ua_sess->metadata->attr.overwrite;
693 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
694 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
695 uattr.switch_timer_interval =
696 ua_sess->metadata->attr.switch_timer_interval;
697 uattr.read_timer_interval =
698 ua_sess->metadata->attr.read_timer_interval;
699 uattr.output = ua_sess->metadata->attr.output;
700
701 /* We are going to receive 2 fds, we need to reserve them. */
702 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
703 if (ret < 0) {
704 ERR("Exhausted number of available FD upon metadata open");
705 goto error;
706 }
707 /* UST tracer metadata creation */
708 ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr,
709 &ua_sess->metadata->obj);
710 if (ret < 0) {
711 ERR("UST app open metadata failed for app pid:%d with ret %d",
712 app->pid, ret);
713 goto error;
714 }
715
716 ua_sess->metadata->handle = ua_sess->metadata->obj->handle;
717
718 error:
719 health_code_update(&health_thread_cmd);
720 return ret;
721 }
722
723 /*
724 * Create stream onto the UST tracer for a UST session.
725 */
726 static int create_ust_stream(struct ust_app *app,
727 struct ust_app_session *ua_sess)
728 {
729 int ret;
730
731 health_code_update(&health_thread_cmd);
732
733 /* We are going to receive 2 fds, we need to reserve them. */
734 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
735 if (ret < 0) {
736 ERR("Exhausted number of available FD upon metadata stream create");
737 goto error;
738 }
739 ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj,
740 &ua_sess->metadata->stream_obj);
741 if (ret < 0) {
742 ERR("UST create metadata stream failed");
743 goto error;
744 }
745
746 error:
747 health_code_update(&health_thread_cmd);
748 return ret;
749 }
750
751 /*
752 * Create the specified channel onto the UST tracer for a UST session.
753 */
754 static int create_ust_channel(struct ust_app *app,
755 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
756 {
757 int ret;
758
759 health_code_update(&health_thread_cmd);
760
761 /* TODO: remove cast and use lttng-ust-abi.h */
762
763 /* We are going to receive 2 fds, we need to reserve them. */
764 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
765 if (ret < 0) {
766 ERR("Exhausted number of available FD upon create channel");
767 goto error;
768 }
769
770 health_code_update(&health_thread_cmd);
771
772 ret = ustctl_create_channel(app->sock, ua_sess->handle,
773 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
774 if (ret < 0) {
775 ERR("Creating channel %s for app (pid: %d, sock: %d) "
776 "and session handle %d with ret %d",
777 ua_chan->name, app->pid, app->sock,
778 ua_sess->handle, ret);
779 lttng_fd_put(LTTNG_FD_APPS, 2);
780 goto error;
781 }
782
783 ua_chan->handle = ua_chan->obj->handle;
784
785 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
786 ua_chan->name, app->pid, app->sock);
787
788 health_code_update(&health_thread_cmd);
789
790 /* If channel is not enabled, disable it on the tracer */
791 if (!ua_chan->enabled) {
792 ret = disable_ust_channel(app, ua_sess, ua_chan);
793 if (ret < 0) {
794 goto error;
795 }
796 }
797
798 error:
799 health_code_update(&health_thread_cmd);
800 return ret;
801 }
802
803 /*
804 * Create the specified event onto the UST tracer for a UST session.
805 */
806 static
807 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
808 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
809 {
810 int ret = 0;
811
812 health_code_update(&health_thread_cmd);
813
814 /* Create UST event on tracer */
815 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
816 &ua_event->obj);
817 if (ret < 0) {
818 ERR("Error ustctl create event %s for app pid: %d with ret %d",
819 ua_event->attr.name, app->pid, ret);
820 goto error;
821 }
822
823 ua_event->handle = ua_event->obj->handle;
824
825 DBG2("UST app event %s created successfully for pid:%d",
826 ua_event->attr.name, app->pid);
827
828 health_code_update(&health_thread_cmd);
829
830 /* Set filter if one is present. */
831 if (ua_event->filter) {
832 ret = set_ust_event_filter(ua_event, app);
833 if (ret < 0) {
834 goto error;
835 }
836 }
837
838 /* If event not enabled, disable it on the tracer */
839 if (ua_event->enabled == 0) {
840 ret = disable_ust_event(app, ua_sess, ua_event);
841 if (ret < 0) {
842 /*
843 * If we hit an EPERM, something is wrong with our disable call. If
844 * we get an EEXIST, there is a problem on the tracer side since we
845 * just created it.
846 */
847 switch (ret) {
848 case -LTTNG_UST_ERR_PERM:
849 /* Code flow problem */
850 assert(0);
851 case -LTTNG_UST_ERR_EXIST:
852 /* It's OK for our use case. */
853 ret = 0;
854 break;
855 default:
856 break;
857 }
858 goto error;
859 }
860 }
861
862 error:
863 health_code_update(&health_thread_cmd);
864 return ret;
865 }
866
867 /*
868 * Copy data between an UST app event and a LTT event.
869 */
870 static void shadow_copy_event(struct ust_app_event *ua_event,
871 struct ltt_ust_event *uevent)
872 {
873 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
874 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
875
876 ua_event->enabled = uevent->enabled;
877
878 /* Copy event attributes */
879 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
880
881 /* Copy filter bytecode */
882 if (uevent->filter) {
883 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
884 /* Filter might be NULL here in case of ENONEM. */
885 }
886 }
887
888 /*
889 * Copy data between an UST app channel and a LTT channel.
890 */
891 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
892 struct ltt_ust_channel *uchan)
893 {
894 struct lttng_ht_iter iter;
895 struct ltt_ust_event *uevent;
896 struct ltt_ust_context *uctx;
897 struct ust_app_event *ua_event;
898 struct ust_app_ctx *ua_ctx;
899
900 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
901
902 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
903 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
904 /* Copy event attributes */
905 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
906
907 ua_chan->enabled = uchan->enabled;
908
909 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
910 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
911 if (ua_ctx == NULL) {
912 continue;
913 }
914 lttng_ht_node_init_ulong(&ua_ctx->node,
915 (unsigned long) ua_ctx->ctx.ctx);
916 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
917 }
918
919 /* Copy all events from ltt ust channel to ust app channel */
920 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
921 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
922 uevent->filter, uevent->attr.loglevel);
923 if (ua_event == NULL) {
924 DBG2("UST event %s not found on shadow copy channel",
925 uevent->attr.name);
926 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
927 if (ua_event == NULL) {
928 continue;
929 }
930 shadow_copy_event(ua_event, uevent);
931 add_unique_ust_app_event(ua_chan->events, ua_event);
932 }
933 }
934
935 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
936 }
937
938 /*
939 * Copy data between a UST app session and a regular LTT session.
940 */
941 static void shadow_copy_session(struct ust_app_session *ua_sess,
942 struct ltt_ust_session *usess, struct ust_app *app)
943 {
944 struct lttng_ht_node_str *ua_chan_node;
945 struct lttng_ht_iter iter;
946 struct ltt_ust_channel *uchan;
947 struct ust_app_channel *ua_chan;
948 time_t rawtime;
949 struct tm *timeinfo;
950 char datetime[16];
951 int ret;
952
953 /* Get date and time for unique app path */
954 time(&rawtime);
955 timeinfo = localtime(&rawtime);
956 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
957
958 DBG2("Shadow copy of session handle %d", ua_sess->handle);
959
960 ua_sess->id = usess->id;
961 ua_sess->uid = usess->uid;
962 ua_sess->gid = usess->gid;
963
964 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
965 datetime);
966 if (ret < 0) {
967 PERROR("asprintf UST shadow copy session");
968 /* TODO: We cannot return an error from here.. */
969 assert(0);
970 }
971
972 /* TODO: support all UST domain */
973
974 /* Iterate over all channels in global domain. */
975 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
976 uchan, node.node) {
977 struct lttng_ht_iter uiter;
978
979 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
980 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
981 if (ua_chan_node != NULL) {
982 /* Session exist. Contiuing. */
983 continue;
984 }
985
986 DBG2("Channel %s not found on shadow session copy, creating it",
987 uchan->name);
988 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
989 if (ua_chan == NULL) {
990 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
991 continue;
992 }
993
994 shadow_copy_channel(ua_chan, uchan);
995 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
996 }
997 }
998
999 /*
1000 * Lookup sesison wrapper.
1001 */
1002 static
1003 void __lookup_session_by_app(struct ltt_ust_session *usess,
1004 struct ust_app *app, struct lttng_ht_iter *iter)
1005 {
1006 /* Get right UST app session from app */
1007 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
1008 }
1009
1010 /*
1011 * Return ust app session from the app session hashtable using the UST session
1012 * id.
1013 */
1014 static struct ust_app_session *lookup_session_by_app(
1015 struct ltt_ust_session *usess, struct ust_app *app)
1016 {
1017 struct lttng_ht_iter iter;
1018 struct lttng_ht_node_ulong *node;
1019
1020 __lookup_session_by_app(usess, app, &iter);
1021 node = lttng_ht_iter_get_node_ulong(&iter);
1022 if (node == NULL) {
1023 goto error;
1024 }
1025
1026 return caa_container_of(node, struct ust_app_session, node);
1027
1028 error:
1029 return NULL;
1030 }
1031
1032 /*
1033 * Create a session on the tracer side for the given app.
1034 *
1035 * On success, ua_sess_ptr is populated with the session pointer or else left
1036 * untouched. If the session was created, is_created is set to 1. On error,
1037 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1038 * be NULL.
1039 *
1040 * Returns 0 on success or else a negative code which is either -ENOMEM or
1041 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1042 */
1043 static int create_ust_app_session(struct ltt_ust_session *usess,
1044 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1045 int *is_created)
1046 {
1047 int ret, created = 0;
1048 struct ust_app_session *ua_sess;
1049
1050 assert(usess);
1051 assert(app);
1052 assert(ua_sess_ptr);
1053
1054 health_code_update(&health_thread_cmd);
1055
1056 ua_sess = lookup_session_by_app(usess, app);
1057 if (ua_sess == NULL) {
1058 DBG2("UST app pid: %d session id %d not found, creating it",
1059 app->pid, usess->id);
1060 ua_sess = alloc_ust_app_session();
1061 if (ua_sess == NULL) {
1062 /* Only malloc can failed so something is really wrong */
1063 ret = -ENOMEM;
1064 goto error;
1065 }
1066 shadow_copy_session(ua_sess, usess, app);
1067 created = 1;
1068 }
1069
1070 health_code_update(&health_thread_cmd);
1071
1072 if (ua_sess->handle == -1) {
1073 ret = ustctl_create_session(app->sock);
1074 if (ret < 0) {
1075 ERR("Creating session for app pid %d", app->pid);
1076 delete_ust_app_session(-1, ua_sess);
1077 if (ret != -ENOMEM) {
1078 /*
1079 * Tracer is probably gone or got an internal error so let's
1080 * behave like it will soon unregister or not usable.
1081 */
1082 ret = -ENOTCONN;
1083 }
1084 goto error;
1085 }
1086
1087 ua_sess->handle = ret;
1088
1089 /* Add ust app session to app's HT */
1090 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
1091 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
1092
1093 DBG2("UST app session created successfully with handle %d", ret);
1094 }
1095
1096 *ua_sess_ptr = ua_sess;
1097 if (is_created) {
1098 *is_created = created;
1099 }
1100 /* Everything went well. */
1101 ret = 0;
1102
1103 error:
1104 health_code_update(&health_thread_cmd);
1105 return ret;
1106 }
1107
1108 /*
1109 * Create a context for the channel on the tracer.
1110 */
1111 static
1112 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1113 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1114 struct ust_app *app)
1115 {
1116 int ret = 0;
1117 struct lttng_ht_iter iter;
1118 struct lttng_ht_node_ulong *node;
1119 struct ust_app_ctx *ua_ctx;
1120
1121 DBG2("UST app adding context to channel %s", ua_chan->name);
1122
1123 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1124 node = lttng_ht_iter_get_node_ulong(&iter);
1125 if (node != NULL) {
1126 ret = -EEXIST;
1127 goto error;
1128 }
1129
1130 ua_ctx = alloc_ust_app_ctx(uctx);
1131 if (ua_ctx == NULL) {
1132 /* malloc failed */
1133 ret = -1;
1134 goto error;
1135 }
1136
1137 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1138 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
1139
1140 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1141 if (ret < 0) {
1142 goto error;
1143 }
1144
1145 error:
1146 return ret;
1147 }
1148
1149 /*
1150 * Enable on the tracer side a ust app event for the session and channel.
1151 */
1152 static
1153 int enable_ust_app_event(struct ust_app_session *ua_sess,
1154 struct ust_app_event *ua_event, struct ust_app *app)
1155 {
1156 int ret;
1157
1158 ret = enable_ust_event(app, ua_sess, ua_event);
1159 if (ret < 0) {
1160 goto error;
1161 }
1162
1163 ua_event->enabled = 1;
1164
1165 error:
1166 return ret;
1167 }
1168
1169 /*
1170 * Disable on the tracer side a ust app event for the session and channel.
1171 */
1172 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1173 struct ust_app_event *ua_event, struct ust_app *app)
1174 {
1175 int ret;
1176
1177 ret = disable_ust_event(app, ua_sess, ua_event);
1178 if (ret < 0) {
1179 goto error;
1180 }
1181
1182 ua_event->enabled = 0;
1183
1184 error:
1185 return ret;
1186 }
1187
1188 /*
1189 * Lookup ust app channel for session and disable it on the tracer side.
1190 */
1191 static
1192 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1193 struct ust_app_channel *ua_chan, struct ust_app *app)
1194 {
1195 int ret;
1196
1197 ret = disable_ust_channel(app, ua_sess, ua_chan);
1198 if (ret < 0) {
1199 goto error;
1200 }
1201
1202 ua_chan->enabled = 0;
1203
1204 error:
1205 return ret;
1206 }
1207
1208 /*
1209 * Lookup ust app channel for session and enable it on the tracer side.
1210 */
1211 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1212 struct ltt_ust_channel *uchan, struct ust_app *app)
1213 {
1214 int ret = 0;
1215 struct lttng_ht_iter iter;
1216 struct lttng_ht_node_str *ua_chan_node;
1217 struct ust_app_channel *ua_chan;
1218
1219 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1220 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1221 if (ua_chan_node == NULL) {
1222 DBG2("Unable to find channel %s in ust session id %u",
1223 uchan->name, ua_sess->id);
1224 goto error;
1225 }
1226
1227 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1228
1229 ret = enable_ust_channel(app, ua_sess, ua_chan);
1230 if (ret < 0) {
1231 goto error;
1232 }
1233
1234 error:
1235 return ret;
1236 }
1237
1238 /*
1239 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1240 * newly created channel if not NULL.
1241 */
1242 static int create_ust_app_channel(struct ust_app_session *ua_sess,
1243 struct ltt_ust_channel *uchan, struct ust_app *app,
1244 struct ust_app_channel **ua_chanp)
1245 {
1246 int ret = 0;
1247 struct lttng_ht_iter iter;
1248 struct lttng_ht_node_str *ua_chan_node;
1249 struct ust_app_channel *ua_chan;
1250
1251 /* Lookup channel in the ust app session */
1252 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1253 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1254 if (ua_chan_node != NULL) {
1255 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1256 goto end;
1257 }
1258
1259 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1260 if (ua_chan == NULL) {
1261 /* Only malloc can fail here */
1262 ret = -ENOMEM;
1263 goto error;
1264 }
1265 shadow_copy_channel(ua_chan, uchan);
1266
1267 ret = create_ust_channel(app, ua_sess, ua_chan);
1268 if (ret < 0) {
1269 /* Not found previously means that it does not exist on the tracer */
1270 assert(ret != -LTTNG_UST_ERR_EXIST);
1271 goto error;
1272 }
1273
1274 /* Only add the channel if successful on the tracer side. */
1275 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1276
1277 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
1278 app->pid);
1279
1280 end:
1281 if (ua_chanp) {
1282 *ua_chanp = ua_chan;
1283 }
1284
1285 /* Everything went well. */
1286 return 0;
1287
1288 error:
1289 delete_ust_app_channel(-1, ua_chan);
1290 return ret;
1291 }
1292
1293 /*
1294 * Create UST app event and create it on the tracer side.
1295 */
1296 static
1297 int create_ust_app_event(struct ust_app_session *ua_sess,
1298 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1299 struct ust_app *app)
1300 {
1301 int ret = 0;
1302 struct ust_app_event *ua_event;
1303
1304 /* Get event node */
1305 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1306 uevent->filter, uevent->attr.loglevel);
1307 if (ua_event != NULL) {
1308 ret = -EEXIST;
1309 goto end;
1310 }
1311
1312 /* Does not exist so create one */
1313 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1314 if (ua_event == NULL) {
1315 /* Only malloc can failed so something is really wrong */
1316 ret = -ENOMEM;
1317 goto end;
1318 }
1319 shadow_copy_event(ua_event, uevent);
1320
1321 /* Create it on the tracer side */
1322 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1323 if (ret < 0) {
1324 /* Not found previously means that it does not exist on the tracer */
1325 assert(ret != -LTTNG_UST_ERR_EXIST);
1326 goto error;
1327 }
1328
1329 add_unique_ust_app_event(ua_chan->events, ua_event);
1330
1331 DBG2("UST app create event %s for PID %d completed", ua_event->name,
1332 app->pid);
1333
1334 end:
1335 return ret;
1336
1337 error:
1338 /* Valid. Calling here is already in a read side lock */
1339 delete_ust_app_event(-1, ua_event);
1340 return ret;
1341 }
1342
1343 /*
1344 * Create UST metadata and open it on the tracer side.
1345 */
1346 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1347 char *pathname, struct ust_app *app)
1348 {
1349 int ret = 0;
1350
1351 if (ua_sess->metadata == NULL) {
1352 /* Allocate UST metadata */
1353 ua_sess->metadata = trace_ust_create_metadata(pathname);
1354 if (ua_sess->metadata == NULL) {
1355 /* malloc() failed */
1356 goto error;
1357 }
1358
1359 ret = open_ust_metadata(app, ua_sess);
1360 if (ret < 0) {
1361 DBG3("Opening metadata failed. Cleaning up memory");
1362
1363 /* Cleanup failed metadata struct */
1364 free(ua_sess->metadata);
1365 /*
1366 * This is very important because delete_ust_app_session check if
1367 * the pointer is null or not in order to delete the metadata.
1368 */
1369 ua_sess->metadata = NULL;
1370 goto error;
1371 }
1372
1373 DBG2("UST metadata opened for app pid %d", app->pid);
1374 }
1375
1376 /* Open UST metadata stream */
1377 if (ua_sess->metadata->stream_obj == NULL) {
1378 ret = create_ust_stream(app, ua_sess);
1379 if (ret < 0) {
1380 goto error;
1381 }
1382
1383 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1384 "%s/metadata", ua_sess->path);
1385 if (ret < 0) {
1386 PERROR("asprintf UST create stream");
1387 goto error;
1388 }
1389
1390 DBG2("UST metadata stream object created for app pid %d",
1391 app->pid);
1392 } else {
1393 ERR("Attempting to create stream without metadata opened");
1394 goto error;
1395 }
1396
1397 return 0;
1398
1399 error:
1400 return -1;
1401 }
1402
1403 /*
1404 * Return pointer to traceable apps list.
1405 */
1406 struct lttng_ht *ust_app_get_ht(void)
1407 {
1408 return ust_app_ht;
1409 }
1410
1411 /*
1412 * Return ust app pointer or NULL if not found.
1413 */
1414 struct ust_app *ust_app_find_by_pid(pid_t pid)
1415 {
1416 struct lttng_ht_node_ulong *node;
1417 struct lttng_ht_iter iter;
1418
1419 rcu_read_lock();
1420 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1421 node = lttng_ht_iter_get_node_ulong(&iter);
1422 if (node == NULL) {
1423 DBG2("UST app no found with pid %d", pid);
1424 goto error;
1425 }
1426 rcu_read_unlock();
1427
1428 DBG2("Found UST app by pid %d", pid);
1429
1430 return caa_container_of(node, struct ust_app, pid_n);
1431
1432 error:
1433 rcu_read_unlock();
1434 return NULL;
1435 }
1436
1437 /*
1438 * Using pid and uid (of the app), allocate a new ust_app struct and
1439 * add it to the global traceable app list.
1440 *
1441 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1442 * bitness is not supported.
1443 */
1444 int ust_app_register(struct ust_register_msg *msg, int sock)
1445 {
1446 struct ust_app *lta;
1447 int ret;
1448
1449 if ((msg->bits_per_long == 64 &&
1450 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1451 || (msg->bits_per_long == 32 &&
1452 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
1453 ERR("Registration failed: application \"%s\" (pid: %d) has "
1454 "%d-bit long, but no consumerd for this long size is available.\n",
1455 msg->name, msg->pid, msg->bits_per_long);
1456 ret = close(sock);
1457 if (ret) {
1458 PERROR("close");
1459 }
1460 lttng_fd_put(LTTNG_FD_APPS, 1);
1461 return -EINVAL;
1462 }
1463 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1464 ERR("Registration failed: application \"%s\" (pid: %d) has "
1465 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1466 msg->name, msg->pid, msg->major, msg->minor);
1467 ret = close(sock);
1468 if (ret) {
1469 PERROR("close");
1470 }
1471 lttng_fd_put(LTTNG_FD_APPS, 1);
1472 return -EINVAL;
1473 }
1474 lta = zmalloc(sizeof(struct ust_app));
1475 if (lta == NULL) {
1476 PERROR("malloc");
1477 return -ENOMEM;
1478 }
1479
1480 lta->ppid = msg->ppid;
1481 lta->uid = msg->uid;
1482 lta->gid = msg->gid;
1483 lta->compatible = 0; /* Not compatible until proven */
1484 lta->bits_per_long = msg->bits_per_long;
1485 lta->v_major = msg->major;
1486 lta->v_minor = msg->minor;
1487 strncpy(lta->name, msg->name, sizeof(lta->name));
1488 lta->name[16] = '\0';
1489 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1490
1491 lta->pid = msg->pid;
1492 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1493 lta->sock = sock;
1494 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
1495
1496 CDS_INIT_LIST_HEAD(&lta->teardown_head);
1497
1498 rcu_read_lock();
1499
1500 /*
1501 * On a re-registration, we want to kick out the previous registration of
1502 * that pid
1503 */
1504 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1505
1506 /*
1507 * The socket _should_ be unique until _we_ call close. So, a add_unique
1508 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1509 * already in the table.
1510 */
1511 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1512
1513 rcu_read_unlock();
1514
1515 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1516 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1517 lta->sock, lta->name, lta->v_major, lta->v_minor);
1518
1519 return 0;
1520 }
1521
1522 /*
1523 * Unregister app by removing it from the global traceable app list and freeing
1524 * the data struct.
1525 *
1526 * The socket is already closed at this point so no close to sock.
1527 */
1528 void ust_app_unregister(int sock)
1529 {
1530 struct ust_app *lta;
1531 struct lttng_ht_node_ulong *node;
1532 struct lttng_ht_iter iter;
1533 struct ust_app_session *ua_sess;
1534 int ret;
1535
1536 rcu_read_lock();
1537
1538 /* Get the node reference for a call_rcu */
1539 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1540 node = lttng_ht_iter_get_node_ulong(&iter);
1541 if (node == NULL) {
1542 ERR("Unable to find app by sock %d", sock);
1543 goto error;
1544 }
1545
1546 lta = caa_container_of(node, struct ust_app, sock_n);
1547
1548 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1549
1550 /* Remove application from PID hash table */
1551 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1552 assert(!ret);
1553
1554 /* Assign second node for deletion */
1555 iter.iter.node = &lta->pid_n.node;
1556
1557 /*
1558 * Ignore return value since the node might have been removed before by an
1559 * add replace during app registration because the PID can be reassigned by
1560 * the OS.
1561 */
1562 ret = lttng_ht_del(ust_app_ht, &iter);
1563 if (ret) {
1564 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1565 lta->pid);
1566 }
1567
1568 /* Remove sessions so they are not visible during deletion.*/
1569 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
1570 node.node) {
1571 ret = lttng_ht_del(lta->sessions, &iter);
1572 if (ret) {
1573 /* The session was already removed so scheduled for teardown. */
1574 continue;
1575 }
1576
1577 /*
1578 * Add session to list for teardown. This is safe since at this point we
1579 * are the only one using this list.
1580 */
1581 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
1582 }
1583
1584 /* Free memory */
1585 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1586
1587 error:
1588 rcu_read_unlock();
1589 return;
1590 }
1591
1592 /*
1593 * Return traceable_app_count
1594 */
1595 unsigned long ust_app_list_count(void)
1596 {
1597 unsigned long count;
1598
1599 rcu_read_lock();
1600 count = lttng_ht_get_count(ust_app_ht);
1601 rcu_read_unlock();
1602
1603 return count;
1604 }
1605
1606 /*
1607 * Fill events array with all events name of all registered apps.
1608 */
1609 int ust_app_list_events(struct lttng_event **events)
1610 {
1611 int ret, handle;
1612 size_t nbmem, count = 0;
1613 struct lttng_ht_iter iter;
1614 struct ust_app *app;
1615 struct lttng_event *tmp_event;
1616
1617 nbmem = UST_APP_EVENT_LIST_SIZE;
1618 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
1619 if (tmp_event == NULL) {
1620 PERROR("zmalloc ust app events");
1621 ret = -ENOMEM;
1622 goto error;
1623 }
1624
1625 rcu_read_lock();
1626
1627 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1628 struct lttng_ust_tracepoint_iter uiter;
1629
1630 health_code_update(&health_thread_cmd);
1631
1632 if (!app->compatible) {
1633 /*
1634 * TODO: In time, we should notice the caller of this error by
1635 * telling him that this is a version error.
1636 */
1637 continue;
1638 }
1639 handle = ustctl_tracepoint_list(app->sock);
1640 if (handle < 0) {
1641 ERR("UST app list events getting handle failed for app pid %d",
1642 app->pid);
1643 continue;
1644 }
1645
1646 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
1647 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1648 health_code_update(&health_thread_cmd);
1649 if (count >= nbmem) {
1650 /* In case the realloc fails, we free the memory */
1651 void *ptr;
1652
1653 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1654 2 * nbmem);
1655 nbmem *= 2;
1656 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event));
1657 if (ptr == NULL) {
1658 PERROR("realloc ust app events");
1659 free(tmp_event);
1660 ret = -ENOMEM;
1661 goto rcu_error;
1662 }
1663 tmp_event = ptr;
1664 }
1665 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1666 tmp_event[count].loglevel = uiter.loglevel;
1667 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1668 tmp_event[count].pid = app->pid;
1669 tmp_event[count].enabled = -1;
1670 count++;
1671 }
1672 }
1673
1674 ret = count;
1675 *events = tmp_event;
1676
1677 DBG2("UST app list events done (%zu events)", count);
1678
1679 rcu_error:
1680 rcu_read_unlock();
1681 error:
1682 health_code_update(&health_thread_cmd);
1683 return ret;
1684 }
1685
1686 /*
1687 * Fill events array with all events name of all registered apps.
1688 */
1689 int ust_app_list_event_fields(struct lttng_event_field **fields)
1690 {
1691 int ret, handle;
1692 size_t nbmem, count = 0;
1693 struct lttng_ht_iter iter;
1694 struct ust_app *app;
1695 struct lttng_event_field *tmp_event;
1696
1697 nbmem = UST_APP_EVENT_LIST_SIZE;
1698 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
1699 if (tmp_event == NULL) {
1700 PERROR("zmalloc ust app event fields");
1701 ret = -ENOMEM;
1702 goto error;
1703 }
1704
1705 rcu_read_lock();
1706
1707 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1708 struct lttng_ust_field_iter uiter;
1709
1710 health_code_update(&health_thread_cmd);
1711
1712 if (!app->compatible) {
1713 /*
1714 * TODO: In time, we should notice the caller of this error by
1715 * telling him that this is a version error.
1716 */
1717 continue;
1718 }
1719 handle = ustctl_tracepoint_field_list(app->sock);
1720 if (handle < 0) {
1721 ERR("UST app list event fields getting handle failed for app pid %d",
1722 app->pid);
1723 continue;
1724 }
1725
1726 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
1727 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1728 health_code_update(&health_thread_cmd);
1729 if (count >= nbmem) {
1730 /* In case the realloc fails, we free the memory */
1731 void *ptr;
1732
1733 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
1734 2 * nbmem);
1735 nbmem *= 2;
1736 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field));
1737 if (ptr == NULL) {
1738 PERROR("realloc ust app event fields");
1739 free(tmp_event);
1740 ret = -ENOMEM;
1741 goto rcu_error;
1742 }
1743 tmp_event = ptr;
1744 }
1745
1746 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
1747 tmp_event[count].type = uiter.type;
1748 tmp_event[count].nowrite = uiter.nowrite;
1749
1750 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
1751 tmp_event[count].event.loglevel = uiter.loglevel;
1752 tmp_event[count].event.type = LTTNG_UST_TRACEPOINT;
1753 tmp_event[count].event.pid = app->pid;
1754 tmp_event[count].event.enabled = -1;
1755 count++;
1756 }
1757 }
1758
1759 ret = count;
1760 *fields = tmp_event;
1761
1762 DBG2("UST app list event fields done (%zu events)", count);
1763
1764 rcu_error:
1765 rcu_read_unlock();
1766 error:
1767 health_code_update(&health_thread_cmd);
1768 return ret;
1769 }
1770
1771 /*
1772 * Free and clean all traceable apps of the global list.
1773 */
1774 void ust_app_clean_list(void)
1775 {
1776 int ret;
1777 struct ust_app *app;
1778 struct lttng_ht_iter iter;
1779
1780 DBG2("UST app cleaning registered apps hash table");
1781
1782 rcu_read_lock();
1783
1784 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1785 ret = lttng_ht_del(ust_app_ht, &iter);
1786 assert(!ret);
1787 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
1788 }
1789
1790 /* Cleanup socket hash table */
1791 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
1792 sock_n.node) {
1793 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1794 assert(!ret);
1795 }
1796
1797 /* Destroy is done only when the ht is empty */
1798 lttng_ht_destroy(ust_app_ht);
1799 lttng_ht_destroy(ust_app_ht_by_sock);
1800
1801 rcu_read_unlock();
1802 }
1803
1804 /*
1805 * Init UST app hash table.
1806 */
1807 void ust_app_ht_alloc(void)
1808 {
1809 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1810 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1811 }
1812
1813 /*
1814 * For a specific UST session, disable the channel for all registered apps.
1815 */
1816 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1817 struct ltt_ust_channel *uchan)
1818 {
1819 int ret = 0;
1820 struct lttng_ht_iter iter;
1821 struct lttng_ht_node_str *ua_chan_node;
1822 struct ust_app *app;
1823 struct ust_app_session *ua_sess;
1824 struct ust_app_channel *ua_chan;
1825
1826 if (usess == NULL || uchan == NULL) {
1827 ERR("Disabling UST global channel with NULL values");
1828 ret = -1;
1829 goto error;
1830 }
1831
1832 DBG2("UST app disabling channel %s from global domain for session id %d",
1833 uchan->name, usess->id);
1834
1835 rcu_read_lock();
1836
1837 /* For every registered applications */
1838 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1839 struct lttng_ht_iter uiter;
1840 if (!app->compatible) {
1841 /*
1842 * TODO: In time, we should notice the caller of this error by
1843 * telling him that this is a version error.
1844 */
1845 continue;
1846 }
1847 ua_sess = lookup_session_by_app(usess, app);
1848 if (ua_sess == NULL) {
1849 continue;
1850 }
1851
1852 /* Get channel */
1853 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1854 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1855 /* If the session if found for the app, the channel must be there */
1856 assert(ua_chan_node);
1857
1858 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1859 /* The channel must not be already disabled */
1860 assert(ua_chan->enabled == 1);
1861
1862 /* Disable channel onto application */
1863 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1864 if (ret < 0) {
1865 /* XXX: We might want to report this error at some point... */
1866 continue;
1867 }
1868 }
1869
1870 rcu_read_unlock();
1871
1872 error:
1873 return ret;
1874 }
1875
1876 /*
1877 * For a specific UST session, enable the channel for all registered apps.
1878 */
1879 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1880 struct ltt_ust_channel *uchan)
1881 {
1882 int ret = 0;
1883 struct lttng_ht_iter iter;
1884 struct ust_app *app;
1885 struct ust_app_session *ua_sess;
1886
1887 if (usess == NULL || uchan == NULL) {
1888 ERR("Adding UST global channel to NULL values");
1889 ret = -1;
1890 goto error;
1891 }
1892
1893 DBG2("UST app enabling channel %s to global domain for session id %d",
1894 uchan->name, usess->id);
1895
1896 rcu_read_lock();
1897
1898 /* For every registered applications */
1899 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1900 if (!app->compatible) {
1901 /*
1902 * TODO: In time, we should notice the caller of this error by
1903 * telling him that this is a version error.
1904 */
1905 continue;
1906 }
1907 ua_sess = lookup_session_by_app(usess, app);
1908 if (ua_sess == NULL) {
1909 continue;
1910 }
1911
1912 /* Enable channel onto application */
1913 ret = enable_ust_app_channel(ua_sess, uchan, app);
1914 if (ret < 0) {
1915 /* XXX: We might want to report this error at some point... */
1916 continue;
1917 }
1918 }
1919
1920 rcu_read_unlock();
1921
1922 error:
1923 return ret;
1924 }
1925
1926 /*
1927 * Disable an event in a channel and for a specific session.
1928 */
1929 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1930 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1931 {
1932 int ret = 0;
1933 struct lttng_ht_iter iter, uiter;
1934 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
1935 struct ust_app *app;
1936 struct ust_app_session *ua_sess;
1937 struct ust_app_channel *ua_chan;
1938 struct ust_app_event *ua_event;
1939
1940 DBG("UST app disabling event %s for all apps in channel "
1941 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
1942
1943 rcu_read_lock();
1944
1945 /* For all registered applications */
1946 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1947 if (!app->compatible) {
1948 /*
1949 * TODO: In time, we should notice the caller of this error by
1950 * telling him that this is a version error.
1951 */
1952 continue;
1953 }
1954 ua_sess = lookup_session_by_app(usess, app);
1955 if (ua_sess == NULL) {
1956 /* Next app */
1957 continue;
1958 }
1959
1960 /* Lookup channel in the ust app session */
1961 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1962 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1963 if (ua_chan_node == NULL) {
1964 DBG2("Channel %s not found in session id %d for app pid %d."
1965 "Skipping", uchan->name, usess->id, app->pid);
1966 continue;
1967 }
1968 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1969
1970 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1971 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
1972 if (ua_event_node == NULL) {
1973 DBG2("Event %s not found in channel %s for app pid %d."
1974 "Skipping", uevent->attr.name, uchan->name, app->pid);
1975 continue;
1976 }
1977 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1978
1979 ret = disable_ust_app_event(ua_sess, ua_event, app);
1980 if (ret < 0) {
1981 /* XXX: Report error someday... */
1982 continue;
1983 }
1984 }
1985
1986 rcu_read_unlock();
1987
1988 return ret;
1989 }
1990
1991 /*
1992 * For a specific UST session and UST channel, the event for all
1993 * registered apps.
1994 */
1995 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1996 struct ltt_ust_channel *uchan)
1997 {
1998 int ret = 0;
1999 struct lttng_ht_iter iter, uiter;
2000 struct lttng_ht_node_str *ua_chan_node;
2001 struct ust_app *app;
2002 struct ust_app_session *ua_sess;
2003 struct ust_app_channel *ua_chan;
2004 struct ust_app_event *ua_event;
2005
2006 DBG("UST app disabling all event for all apps in channel "
2007 "%s for session id %d", uchan->name, usess->id);
2008
2009 rcu_read_lock();
2010
2011 /* For all registered applications */
2012 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2013 if (!app->compatible) {
2014 /*
2015 * TODO: In time, we should notice the caller of this error by
2016 * telling him that this is a version error.
2017 */
2018 continue;
2019 }
2020 ua_sess = lookup_session_by_app(usess, app);
2021 if (!ua_sess) {
2022 /* The application has problem or is probably dead. */
2023 continue;
2024 }
2025
2026 /* Lookup channel in the ust app session */
2027 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2028 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2029 /* If the channel is not found, there is a code flow error */
2030 assert(ua_chan_node);
2031
2032 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2033
2034 /* Disable each events of channel */
2035 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2036 node.node) {
2037 ret = disable_ust_app_event(ua_sess, ua_event, app);
2038 if (ret < 0) {
2039 /* XXX: Report error someday... */
2040 continue;
2041 }
2042 }
2043 }
2044
2045 rcu_read_unlock();
2046
2047 return ret;
2048 }
2049
2050 /*
2051 * For a specific UST session, create the channel for all registered apps.
2052 */
2053 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
2054 struct ltt_ust_channel *uchan)
2055 {
2056 int ret = 0, created;
2057 struct lttng_ht_iter iter;
2058 struct ust_app *app;
2059 struct ust_app_session *ua_sess = NULL;
2060
2061 /* Very wrong code flow */
2062 assert(usess);
2063 assert(uchan);
2064
2065 DBG2("UST app adding channel %s to global domain for session id %d",
2066 uchan->name, usess->id);
2067
2068 rcu_read_lock();
2069
2070 /* For every registered applications */
2071 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2072 if (!app->compatible) {
2073 /*
2074 * TODO: In time, we should notice the caller of this error by
2075 * telling him that this is a version error.
2076 */
2077 continue;
2078 }
2079 /*
2080 * Create session on the tracer side and add it to app session HT. Note
2081 * that if session exist, it will simply return a pointer to the ust
2082 * app session.
2083 */
2084 ret = create_ust_app_session(usess, app, &ua_sess, &created);
2085 if (ret < 0) {
2086 switch (ret) {
2087 case -ENOTCONN:
2088 /*
2089 * The application's socket is not valid. Either a bad socket
2090 * or a timeout on it. We can't inform the caller that for a
2091 * specific app, the session failed so lets continue here.
2092 */
2093 continue;
2094 case -ENOMEM:
2095 default:
2096 goto error_rcu_unlock;
2097 }
2098 }
2099 assert(ua_sess);
2100
2101 /* Create channel onto application. We don't need the chan ref. */
2102 ret = create_ust_app_channel(ua_sess, uchan, app, NULL);
2103 if (ret < 0) {
2104 if (ret == -ENOMEM) {
2105 /* No more memory is a fatal error. Stop right now. */
2106 goto error_rcu_unlock;
2107 }
2108 /* Cleanup the created session if it's the case. */
2109 if (created) {
2110 delete_ust_app_session(app->sock, ua_sess);
2111 }
2112 }
2113 }
2114
2115 error_rcu_unlock:
2116 rcu_read_unlock();
2117 return ret;
2118 }
2119
2120 /*
2121 * Enable event for a specific session and channel on the tracer.
2122 */
2123 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
2124 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2125 {
2126 int ret = 0;
2127 struct lttng_ht_iter iter, uiter;
2128 struct lttng_ht_node_str *ua_chan_node;
2129 struct ust_app *app;
2130 struct ust_app_session *ua_sess;
2131 struct ust_app_channel *ua_chan;
2132 struct ust_app_event *ua_event;
2133
2134 DBG("UST app enabling event %s for all apps for session id %d",
2135 uevent->attr.name, usess->id);
2136
2137 /*
2138 * NOTE: At this point, this function is called only if the session and
2139 * channel passed are already created for all apps. and enabled on the
2140 * tracer also.
2141 */
2142
2143 rcu_read_lock();
2144
2145 /* For all registered applications */
2146 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2147 if (!app->compatible) {
2148 /*
2149 * TODO: In time, we should notice the caller of this error by
2150 * telling him that this is a version error.
2151 */
2152 continue;
2153 }
2154 ua_sess = lookup_session_by_app(usess, app);
2155 if (!ua_sess) {
2156 /* The application has problem or is probably dead. */
2157 continue;
2158 }
2159
2160 /* Lookup channel in the ust app session */
2161 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2162 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2163 /* If the channel is not found, there is a code flow error */
2164 assert(ua_chan_node);
2165
2166 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2167
2168 /* Get event node */
2169 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2170 uevent->filter, uevent->attr.loglevel);
2171 if (ua_event == NULL) {
2172 DBG3("UST app enable event %s not found for app PID %d."
2173 "Skipping app", uevent->attr.name, app->pid);
2174 continue;
2175 }
2176
2177 ret = enable_ust_app_event(ua_sess, ua_event, app);
2178 if (ret < 0) {
2179 goto error;
2180 }
2181 }
2182
2183 error:
2184 rcu_read_unlock();
2185 return ret;
2186 }
2187
2188 /*
2189 * For a specific existing UST session and UST channel, creates the event for
2190 * all registered apps.
2191 */
2192 int ust_app_create_event_glb(struct ltt_ust_session *usess,
2193 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2194 {
2195 int ret = 0;
2196 struct lttng_ht_iter iter, uiter;
2197 struct lttng_ht_node_str *ua_chan_node;
2198 struct ust_app *app;
2199 struct ust_app_session *ua_sess;
2200 struct ust_app_channel *ua_chan;
2201
2202 DBG("UST app creating event %s for all apps for session id %d",
2203 uevent->attr.name, usess->id);
2204
2205 rcu_read_lock();
2206
2207 /* For all registered applications */
2208 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2209 if (!app->compatible) {
2210 /*
2211 * TODO: In time, we should notice the caller of this error by
2212 * telling him that this is a version error.
2213 */
2214 continue;
2215 }
2216 ua_sess = lookup_session_by_app(usess, app);
2217 if (!ua_sess) {
2218 /* The application has problem or is probably dead. */
2219 continue;
2220 }
2221
2222 /* Lookup channel in the ust app session */
2223 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2224 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2225 /* If the channel is not found, there is a code flow error */
2226 assert(ua_chan_node);
2227
2228 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2229
2230 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2231 if (ret < 0) {
2232 if (ret != -LTTNG_UST_ERR_EXIST) {
2233 /* Possible value at this point: -ENOMEM. If so, we stop! */
2234 break;
2235 }
2236 DBG2("UST app event %s already exist on app PID %d",
2237 uevent->attr.name, app->pid);
2238 continue;
2239 }
2240 }
2241
2242 rcu_read_unlock();
2243
2244 return ret;
2245 }
2246
2247 /*
2248 * Start tracing for a specific UST session and app.
2249 */
2250 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
2251 {
2252 int ret = 0;
2253 struct lttng_ht_iter iter;
2254 struct ust_app_session *ua_sess;
2255 struct ust_app_channel *ua_chan;
2256 struct ltt_ust_stream *ustream;
2257 struct consumer_socket *socket;
2258
2259 DBG("Starting tracing for ust app pid %d", app->pid);
2260
2261 rcu_read_lock();
2262
2263 if (!app->compatible) {
2264 goto end;
2265 }
2266
2267 ua_sess = lookup_session_by_app(usess, app);
2268 if (ua_sess == NULL) {
2269 /* The session is in teardown process. Ignore and continue. */
2270 goto end;
2271 }
2272
2273 /* Upon restart, we skip the setup, already done */
2274 if (ua_sess->started) {
2275 goto skip_setup;
2276 }
2277
2278 /* Create directories if consumer is LOCAL and has a path defined. */
2279 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2280 strlen(usess->consumer->dst.trace_path) > 0) {
2281 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2282 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2283 if (ret < 0) {
2284 if (ret != -EEXIST) {
2285 ERR("Trace directory creation error");
2286 ret = -1;
2287 goto error_rcu_unlock;
2288 }
2289 }
2290 }
2291
2292 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
2293 if (ret < 0) {
2294 ret = LTTNG_ERR_UST_META_FAIL;
2295 goto error_rcu_unlock;
2296 }
2297
2298 /* For each channel */
2299 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2300 node.node) {
2301 /* Create all streams */
2302 while (1) {
2303 /* Create UST stream */
2304 ustream = zmalloc(sizeof(*ustream));
2305 if (ustream == NULL) {
2306 PERROR("zmalloc ust stream");
2307 goto error_rcu_unlock;
2308 }
2309
2310 /* We are going to receive 2 fds, we need to reserve them. */
2311 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2312 if (ret < 0) {
2313 ERR("Exhausted number of available FD upon stream create");
2314 free(ustream);
2315 goto error_rcu_unlock;
2316 }
2317
2318 health_code_update(&health_thread_cmd);
2319
2320 ret = ustctl_create_stream(app->sock, ua_chan->obj,
2321 &ustream->obj);
2322 if (ret < 0) {
2323 /* Got all streams */
2324 lttng_fd_put(LTTNG_FD_APPS, 2);
2325 free(ustream);
2326 ret = LTTNG_ERR_UST_STREAM_FAIL;
2327 break;
2328 }
2329 ustream->handle = ustream->obj->handle;
2330
2331 health_code_update(&health_thread_cmd);
2332
2333 /* Order is important */
2334 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
2335 ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u",
2336 ua_chan->name, ua_chan->streams.count);
2337 ua_chan->streams.count++;
2338 if (ret < 0) {
2339 PERROR("asprintf UST create stream");
2340 /*
2341 * XXX what should we do here with the
2342 * stream ?
2343 */
2344 continue;
2345 }
2346 DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count,
2347 ustream->handle);
2348 }
2349
2350 health_code_update(&health_thread_cmd);
2351 }
2352
2353 switch (app->bits_per_long) {
2354 case 64:
2355 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
2356 usess->consumer);
2357 if (socket == NULL) {
2358 goto skip_setup;
2359 }
2360 break;
2361 case 32:
2362 socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd),
2363 usess->consumer);
2364 if (socket == NULL) {
2365 goto skip_setup;
2366 }
2367 break;
2368 default:
2369 ret = -EINVAL;
2370 goto error_rcu_unlock;
2371 }
2372
2373 /* Setup UST consumer socket and send fds to it */
2374 ret = ust_consumer_send_session(ua_sess, usess->consumer, socket);
2375 if (ret < 0) {
2376 goto error_rcu_unlock;
2377 }
2378
2379 health_code_update(&health_thread_cmd);
2380
2381 skip_setup:
2382 /* This start the UST tracing */
2383 ret = ustctl_start_session(app->sock, ua_sess->handle);
2384 if (ret < 0) {
2385 ERR("Error starting tracing for app pid: %d (ret: %d)", app->pid, ret);
2386 goto error_rcu_unlock;
2387 }
2388
2389 /* Indicate that the session has been started once */
2390 ua_sess->started = 1;
2391
2392 health_code_update(&health_thread_cmd);
2393
2394 /* Quiescent wait after starting trace */
2395 ustctl_wait_quiescent(app->sock);
2396
2397 end:
2398 rcu_read_unlock();
2399 health_code_update(&health_thread_cmd);
2400 return 0;
2401
2402 error_rcu_unlock:
2403 rcu_read_unlock();
2404 health_code_update(&health_thread_cmd);
2405 return -1;
2406 }
2407
2408 /*
2409 * Stop tracing for a specific UST session and app.
2410 */
2411 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2412 {
2413 int ret = 0;
2414 struct lttng_ht_iter iter;
2415 struct ust_app_session *ua_sess;
2416 struct ust_app_channel *ua_chan;
2417
2418 DBG("Stopping tracing for ust app pid %d", app->pid);
2419
2420 rcu_read_lock();
2421
2422 if (!app->compatible) {
2423 goto end;
2424 }
2425
2426 ua_sess = lookup_session_by_app(usess, app);
2427 if (ua_sess == NULL) {
2428 goto end;
2429 }
2430
2431 /*
2432 * If started = 0, it means that stop trace has been called for a session
2433 * that was never started. It's possible since we can have a fail start
2434 * from either the application manager thread or the command thread. Simply
2435 * indicate that this is a stop error.
2436 */
2437 if (!ua_sess->started) {
2438 goto error_rcu_unlock;
2439 }
2440
2441 health_code_update(&health_thread_cmd);
2442
2443 /* This inhibits UST tracing */
2444 ret = ustctl_stop_session(app->sock, ua_sess->handle);
2445 if (ret < 0) {
2446 ERR("Error stopping tracing for app pid: %d (ret: %d)", app->pid, ret);
2447 goto error_rcu_unlock;
2448 }
2449
2450 health_code_update(&health_thread_cmd);
2451
2452 /* Quiescent wait after stopping trace */
2453 ustctl_wait_quiescent(app->sock);
2454
2455 health_code_update(&health_thread_cmd);
2456
2457 /* Flushing buffers */
2458 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2459 node.node) {
2460 health_code_update(&health_thread_cmd);
2461 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
2462 if (ret < 0) {
2463 ERR("UST app PID %d channel %s flush failed with ret %d",
2464 app->pid, ua_chan->name, ret);
2465 /* Continuing flushing all buffers */
2466 continue;
2467 }
2468 }
2469
2470 health_code_update(&health_thread_cmd);
2471
2472 /* Flush all buffers before stopping */
2473 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
2474 if (ret < 0) {
2475 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
2476 ret);
2477 }
2478
2479 end:
2480 rcu_read_unlock();
2481 health_code_update(&health_thread_cmd);
2482 return 0;
2483
2484 error_rcu_unlock:
2485 rcu_read_unlock();
2486 health_code_update(&health_thread_cmd);
2487 return -1;
2488 }
2489
2490 /*
2491 * Destroy a specific UST session in apps.
2492 */
2493 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2494 {
2495 struct ust_app_session *ua_sess;
2496 struct lttng_ust_object_data obj;
2497 struct lttng_ht_iter iter;
2498 struct lttng_ht_node_ulong *node;
2499 int ret;
2500
2501 DBG("Destroy tracing for ust app pid %d", app->pid);
2502
2503 rcu_read_lock();
2504
2505 if (!app->compatible) {
2506 goto end;
2507 }
2508
2509 __lookup_session_by_app(usess, app, &iter);
2510 node = lttng_ht_iter_get_node_ulong(&iter);
2511 if (node == NULL) {
2512 /* Session is being or is deleted. */
2513 goto end;
2514 }
2515 ua_sess = caa_container_of(node, struct ust_app_session, node);
2516 ret = lttng_ht_del(app->sessions, &iter);
2517 if (ret) {
2518 /* Already scheduled for teardown. */
2519 goto end;
2520 }
2521
2522 obj.handle = ua_sess->handle;
2523 obj.shm_fd = -1;
2524 obj.wait_fd = -1;
2525 obj.memory_map_size = 0;
2526 health_code_update(&health_thread_cmd);
2527 ustctl_release_object(app->sock, &obj);
2528
2529 health_code_update(&health_thread_cmd);
2530 delete_ust_app_session(app->sock, ua_sess);
2531
2532 /* Quiescent wait after stopping trace */
2533 ustctl_wait_quiescent(app->sock);
2534
2535 end:
2536 rcu_read_unlock();
2537 health_code_update(&health_thread_cmd);
2538 return 0;
2539 }
2540
2541 /*
2542 * Start tracing for the UST session.
2543 */
2544 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2545 {
2546 int ret = 0;
2547 struct lttng_ht_iter iter;
2548 struct ust_app *app;
2549
2550 DBG("Starting all UST traces");
2551
2552 rcu_read_lock();
2553
2554 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2555 ret = ust_app_start_trace(usess, app);
2556 if (ret < 0) {
2557 /* Continue to next apps even on error */
2558 continue;
2559 }
2560 }
2561
2562 rcu_read_unlock();
2563
2564 return 0;
2565 }
2566
2567 /*
2568 * Start tracing for the UST session.
2569 */
2570 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2571 {
2572 int ret = 0;
2573 struct lttng_ht_iter iter;
2574 struct ust_app *app;
2575
2576 DBG("Stopping all UST traces");
2577
2578 rcu_read_lock();
2579
2580 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2581 ret = ust_app_stop_trace(usess, app);
2582 if (ret < 0) {
2583 /* Continue to next apps even on error */
2584 continue;
2585 }
2586 }
2587
2588 rcu_read_unlock();
2589
2590 return 0;
2591 }
2592
2593 /*
2594 * Destroy app UST session.
2595 */
2596 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2597 {
2598 int ret = 0;
2599 struct lttng_ht_iter iter;
2600 struct ust_app *app;
2601
2602 DBG("Destroy all UST traces");
2603
2604 rcu_read_lock();
2605
2606 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2607 ret = destroy_trace(usess, app);
2608 if (ret < 0) {
2609 /* Continue to next apps even on error */
2610 continue;
2611 }
2612 }
2613
2614 rcu_read_unlock();
2615
2616 return 0;
2617 }
2618
2619 /*
2620 * Add channels/events from UST global domain to registered apps at sock.
2621 */
2622 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2623 {
2624 int ret = 0;
2625 struct lttng_ht_iter iter, uiter, iter_ctx;
2626 struct ust_app *app;
2627 struct ust_app_session *ua_sess = NULL;
2628 struct ust_app_channel *ua_chan;
2629 struct ust_app_event *ua_event;
2630 struct ust_app_ctx *ua_ctx;
2631
2632 assert(usess);
2633
2634 DBG2("UST app global update for app sock %d for session id %d", sock,
2635 usess->id);
2636
2637 rcu_read_lock();
2638
2639 app = find_app_by_sock(sock);
2640 if (app == NULL) {
2641 ERR("Failed to update app sock %d", sock);
2642 goto error;
2643 }
2644
2645 if (!app->compatible) {
2646 goto error;
2647 }
2648
2649 ret = create_ust_app_session(usess, app, &ua_sess, NULL);
2650 if (ret < 0) {
2651 /* Tracer is probably gone or ENOMEM. */
2652 goto error;
2653 }
2654 assert(ua_sess);
2655
2656 /*
2657 * We can iterate safely here over all UST app session sicne the create ust
2658 * app session above made a shadow copy of the UST global domain from the
2659 * ltt ust session.
2660 */
2661 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2662 node.node) {
2663 ret = create_ust_channel(app, ua_sess, ua_chan);
2664 if (ret < 0) {
2665 /* FIXME: Should we quit here or continue... */
2666 continue;
2667 }
2668
2669 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2670 node.node) {
2671 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2672 if (ret < 0) {
2673 /* FIXME: Should we quit here or continue... */
2674 continue;
2675 }
2676 }
2677
2678
2679 /* For each events */
2680 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2681 node.node) {
2682 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2683 if (ret < 0) {
2684 /* FIXME: Should we quit here or continue... */
2685 continue;
2686 }
2687 }
2688 }
2689
2690 if (usess->start_trace) {
2691 ret = ust_app_start_trace(usess, app);
2692 if (ret < 0) {
2693 goto error;
2694 }
2695
2696 DBG2("UST trace started for app pid %d", app->pid);
2697 }
2698
2699 error:
2700 rcu_read_unlock();
2701 return;
2702 }
2703
2704 /*
2705 * Add context to a specific channel for global UST domain.
2706 */
2707 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2708 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2709 {
2710 int ret = 0;
2711 struct lttng_ht_node_str *ua_chan_node;
2712 struct lttng_ht_iter iter, uiter;
2713 struct ust_app_channel *ua_chan = NULL;
2714 struct ust_app_session *ua_sess;
2715 struct ust_app *app;
2716
2717 rcu_read_lock();
2718
2719 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2720 if (!app->compatible) {
2721 /*
2722 * TODO: In time, we should notice the caller of this error by
2723 * telling him that this is a version error.
2724 */
2725 continue;
2726 }
2727 ua_sess = lookup_session_by_app(usess, app);
2728 if (ua_sess == NULL) {
2729 continue;
2730 }
2731
2732 /* Lookup channel in the ust app session */
2733 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2734 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2735 if (ua_chan_node == NULL) {
2736 continue;
2737 }
2738 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2739 node);
2740
2741 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2742 if (ret < 0) {
2743 continue;
2744 }
2745 }
2746
2747 rcu_read_unlock();
2748 return ret;
2749 }
2750
2751 /*
2752 * Enable event for a channel from a UST session for a specific PID.
2753 */
2754 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2755 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2756 {
2757 int ret = 0;
2758 struct lttng_ht_iter iter;
2759 struct lttng_ht_node_str *ua_chan_node;
2760 struct ust_app *app;
2761 struct ust_app_session *ua_sess;
2762 struct ust_app_channel *ua_chan;
2763 struct ust_app_event *ua_event;
2764
2765 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2766
2767 rcu_read_lock();
2768
2769 app = ust_app_find_by_pid(pid);
2770 if (app == NULL) {
2771 ERR("UST app enable event per PID %d not found", pid);
2772 ret = -1;
2773 goto error;
2774 }
2775
2776 if (!app->compatible) {
2777 ret = 0;
2778 goto error;
2779 }
2780
2781 ua_sess = lookup_session_by_app(usess, app);
2782 if (!ua_sess) {
2783 /* The application has problem or is probably dead. */
2784 goto error;
2785 }
2786
2787 /* Lookup channel in the ust app session */
2788 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2789 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2790 /* If the channel is not found, there is a code flow error */
2791 assert(ua_chan_node);
2792
2793 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2794
2795 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2796 uevent->filter, uevent->attr.loglevel);
2797 if (ua_event == NULL) {
2798 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2799 if (ret < 0) {
2800 goto error;
2801 }
2802 } else {
2803 ret = enable_ust_app_event(ua_sess, ua_event, app);
2804 if (ret < 0) {
2805 goto error;
2806 }
2807 }
2808
2809 error:
2810 rcu_read_unlock();
2811 return ret;
2812 }
2813
2814 /*
2815 * Disable event for a channel from a UST session for a specific PID.
2816 */
2817 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2818 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2819 {
2820 int ret = 0;
2821 struct lttng_ht_iter iter;
2822 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2823 struct ust_app *app;
2824 struct ust_app_session *ua_sess;
2825 struct ust_app_channel *ua_chan;
2826 struct ust_app_event *ua_event;
2827
2828 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2829
2830 rcu_read_lock();
2831
2832 app = ust_app_find_by_pid(pid);
2833 if (app == NULL) {
2834 ERR("UST app disable event per PID %d not found", pid);
2835 ret = -1;
2836 goto error;
2837 }
2838
2839 if (!app->compatible) {
2840 ret = 0;
2841 goto error;
2842 }
2843
2844 ua_sess = lookup_session_by_app(usess, app);
2845 if (!ua_sess) {
2846 /* The application has problem or is probably dead. */
2847 goto error;
2848 }
2849
2850 /* Lookup channel in the ust app session */
2851 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2852 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2853 if (ua_chan_node == NULL) {
2854 /* Channel does not exist, skip disabling */
2855 goto error;
2856 }
2857 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2858
2859 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2860 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2861 if (ua_event_node == NULL) {
2862 /* Event does not exist, skip disabling */
2863 goto error;
2864 }
2865 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2866
2867 ret = disable_ust_app_event(ua_sess, ua_event, app);
2868 if (ret < 0) {
2869 goto error;
2870 }
2871
2872 error:
2873 rcu_read_unlock();
2874 return ret;
2875 }
2876
2877 /*
2878 * Validate version of UST apps and set the compatible bit.
2879 */
2880 int ust_app_validate_version(int sock)
2881 {
2882 int ret;
2883 struct ust_app *app;
2884
2885 rcu_read_lock();
2886
2887 app = find_app_by_sock(sock);
2888 assert(app);
2889
2890 health_code_update(&health_thread_cmd);
2891
2892 ret = ustctl_tracer_version(sock, &app->version);
2893 if (ret < 0) {
2894 goto error;
2895 }
2896
2897 /* Validate version */
2898 if (app->version.major != UST_APP_MAJOR_VERSION) {
2899 goto error;
2900 }
2901
2902 DBG2("UST app PID %d is compatible with internal major version %d "
2903 "(supporting == %d)", app->pid, app->version.major,
2904 UST_APP_MAJOR_VERSION);
2905 app->compatible = 1;
2906 rcu_read_unlock();
2907 health_code_update(&health_thread_cmd);
2908 return 0;
2909
2910 error:
2911 DBG2("UST app PID %d is not compatible with internal major version %d "
2912 "(supporting == %d)", app->pid, app->version.major,
2913 UST_APP_MAJOR_VERSION);
2914 app->compatible = 0;
2915 rcu_read_unlock();
2916 health_code_update(&health_thread_cmd);
2917 return -1;
2918 }
2919
2920 /*
2921 * Calibrate registered applications.
2922 */
2923 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2924 {
2925 int ret = 0;
2926 struct lttng_ht_iter iter;
2927 struct ust_app *app;
2928
2929 rcu_read_lock();
2930
2931 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2932 if (!app->compatible) {
2933 /*
2934 * TODO: In time, we should notice the caller of this error by
2935 * telling him that this is a version error.
2936 */
2937 continue;
2938 }
2939
2940 health_code_update(&health_thread_cmd);
2941
2942 ret = ustctl_calibrate(app->sock, calibrate);
2943 if (ret < 0) {
2944 switch (ret) {
2945 case -ENOSYS:
2946 /* Means that it's not implemented on the tracer side. */
2947 ret = 0;
2948 break;
2949 default:
2950 /* TODO: Report error to user */
2951 DBG2("Calibrate app PID %d returned with error %d",
2952 app->pid, ret);
2953 break;
2954 }
2955 }
2956 }
2957
2958 DBG("UST app global domain calibration finished");
2959
2960 rcu_read_unlock();
2961
2962 health_code_update(&health_thread_cmd);
2963
2964 return ret;
2965 }
This page took 0.095943 seconds and 4 git commands to generate.