Fix: memcpy of string is larger than source
[lttng-ust.git] / liblttng-ust / ltt-events.c
1 /*
2 * ltt-events.c
3 *
4 * Holds LTTng per-session event registry.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <endian.h>
26 #include <urcu/list.h>
27 #include <urcu/hlist.h>
28 #include <pthread.h>
29 #include <uuid/uuid.h>
30 #include <errno.h>
31 #include <sys/shm.h>
32 #include <sys/ipc.h>
33 #include <stdint.h>
34 #include <stddef.h>
35 #include <inttypes.h>
36 #include <time.h>
37 #include <sys/prctl.h>
38 #include "clock.h"
39
40 #include <urcu-bp.h>
41 #include <urcu/compiler.h>
42 #include <urcu/uatomic.h>
43 #include <urcu/arch.h>
44
45 #include <lttng/tracepoint.h>
46 #include <lttng/ust-events.h>
47
48 #include <usterr-signal-safe.h>
49 #include <helper.h>
50 #include "error.h"
51
52 #include "tracepoint-internal.h"
53 #include "ltt-tracer.h"
54 #include "ltt-tracer-core.h"
55 #include "wait.h"
56 #include "../libringbuffer/shm.h"
57 #include "jhash.h"
58
59 #define PROCNAME_LEN 17
60
61 /*
62 * The sessions mutex is the centralized mutex across UST tracing
63 * control and probe registration. All operations within this file are
64 * called by the communication thread, under ust_lock protection.
65 */
66 static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
67
68 void ust_lock(void)
69 {
70 pthread_mutex_lock(&sessions_mutex);
71 }
72
73 void ust_unlock(void)
74 {
75 pthread_mutex_unlock(&sessions_mutex);
76 }
77
78 static CDS_LIST_HEAD(sessions);
79
80 /*
81 * Wildcard list, containing the active wildcards.
82 * Protected by ust lock.
83 */
84 static CDS_LIST_HEAD(wildcard_list);
85
86 /*
87 * Pending probes hash table, containing the registered ltt events for
88 * which tracepoint probes are still missing. Protected by the sessions
89 * mutex.
90 */
91 #define PENDING_PROBE_HASH_BITS 6
92 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
93 static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE];
94
95 struct ust_pending_probe {
96 struct ltt_event *event;
97 struct cds_hlist_node node;
98 enum lttng_ust_loglevel_type loglevel_type;
99 int loglevel;
100 char name[];
101 };
102
103 static void _ltt_event_destroy(struct ltt_event *event);
104 static void _ltt_wildcard_destroy(struct session_wildcard *sw);
105 static void _ltt_channel_destroy(struct ltt_channel *chan);
106 static int _ltt_event_unregister(struct ltt_event *event);
107 static
108 int _ltt_event_metadata_statedump(struct ltt_session *session,
109 struct ltt_channel *chan,
110 struct ltt_event *event);
111 static
112 int _ltt_session_metadata_statedump(struct ltt_session *session);
113
114 int ltt_loglevel_match(const struct lttng_event_desc *desc,
115 enum lttng_ust_loglevel_type req_type,
116 int req_loglevel)
117 {
118 int ev_loglevel;
119
120 if (req_type == LTTNG_UST_LOGLEVEL_ALL)
121 return 1;
122 if (!desc->loglevel)
123 ev_loglevel = TRACE_DEFAULT;
124 else
125 ev_loglevel = *(*desc->loglevel);
126 switch (req_type) {
127 case LTTNG_UST_LOGLEVEL_RANGE:
128 if (ev_loglevel <= req_loglevel || req_loglevel == -1)
129 return 1;
130 else
131 return 0;
132 case LTTNG_UST_LOGLEVEL_SINGLE:
133 if (ev_loglevel == req_loglevel || req_loglevel == -1)
134 return 1;
135 else
136 return 0;
137 case LTTNG_UST_LOGLEVEL_ALL:
138 default:
139 return 1;
140 }
141 }
142
143 /*
144 * Return wildcard for a given event name if the event name match the
145 * one of the wildcards.
146 * Must be called with ust lock held.
147 * Returns NULL if not present.
148 */
149 static
150 struct wildcard_entry *match_wildcard(const struct lttng_event_desc *desc)
151 {
152 struct wildcard_entry *e;
153
154 cds_list_for_each_entry(e, &wildcard_list, list) {
155 /* If only contain '*' */
156 if (strlen(e->name) == 1)
157 goto possible_match;
158 /* Compare excluding final '*' */
159 if (!strncmp(desc->name, e->name, strlen(e->name) - 1))
160 goto possible_match;
161 continue; /* goto next, no match */
162 possible_match:
163 if (ltt_loglevel_match(desc,
164 e->loglevel_type,
165 e->loglevel)) {
166 return e;
167 }
168 /* no match, loop to next */
169 }
170 return NULL;
171 }
172
173 /*
174 * called at event creation if probe is missing.
175 * called with session mutex held.
176 */
177 static
178 int add_pending_probe(struct ltt_event *event, const char *name,
179 enum lttng_ust_loglevel_type loglevel_type,
180 int loglevel)
181 {
182 struct cds_hlist_head *head;
183 struct ust_pending_probe *e;
184 size_t name_len = strlen(name) + 1;
185 uint32_t hash;
186
187 if (name_len > LTTNG_UST_SYM_NAME_LEN) {
188 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN);
189 name_len = LTTNG_UST_SYM_NAME_LEN;
190 }
191 hash = jhash(name, name_len - 1, 0);
192 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
193 e = zmalloc(sizeof(struct ust_pending_probe) + name_len);
194 if (!e)
195 return -ENOMEM;
196 memcpy(&e->name[0], name, name_len);
197 e->name[name_len - 1] = '\0';
198 e->loglevel_type = loglevel_type;
199 e->loglevel = loglevel;
200 cds_hlist_add_head(&e->node, head);
201 e->event = event;
202 event->pending_probe = e;
203 return 0;
204 }
205
206 /*
207 * remove a pending probe. called when at event teardown and when an
208 * event is fixed (probe is loaded).
209 * called with session mutex held.
210 */
211 static
212 void remove_pending_probe(struct ust_pending_probe *e)
213 {
214 if (!e)
215 return;
216 cds_hlist_del(&e->node);
217 free(e);
218 }
219
220 /*
221 * Called at library load: connect the probe on the events pending on
222 * probe load.
223 * called with session mutex held.
224 */
225 int pending_probe_fix_events(const struct lttng_event_desc *desc)
226 {
227 struct cds_hlist_head *head;
228 struct cds_hlist_node *node, *p;
229 struct ust_pending_probe *e;
230 const char *name = desc->name;
231 int ret = 0;
232 struct lttng_ust_event event_param;
233 size_t name_len = strlen(name) + 1;
234 uint32_t hash;
235
236 /* Wildcard */
237 {
238 struct wildcard_entry *wildcard;
239
240 wildcard = match_wildcard(desc);
241 if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) {
242 struct session_wildcard *sw;
243
244 cds_list_for_each_entry(sw, &wildcard->session_list,
245 session_list) {
246 struct ltt_event *ev;
247 int ret;
248
249 memcpy(&event_param, &sw->event_param,
250 sizeof(event_param));
251 strncpy(event_param.name,
252 desc->name,
253 sizeof(event_param.name));
254 event_param.name[sizeof(event_param.name) - 1] = '\0';
255 /* create event */
256 ret = ltt_event_create(sw->chan,
257 &event_param, NULL,
258 &ev);
259 if (ret) {
260 DBG("Error creating event");
261 continue;
262 }
263 cds_list_add(&ev->wildcard_list,
264 &sw->events);
265 }
266 }
267 }
268
269 if (name_len > LTTNG_UST_SYM_NAME_LEN) {
270 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN);
271 name_len = LTTNG_UST_SYM_NAME_LEN;
272 }
273 hash = jhash(name, name_len - 1, 0);
274 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
275 cds_hlist_for_each_entry_safe(e, node, p, head, node) {
276 struct ltt_event *event;
277 struct ltt_channel *chan;
278
279 if (!ltt_loglevel_match(desc,
280 e->loglevel_type,
281 e->loglevel)) {
282 continue;
283 }
284 if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
285 continue;
286 }
287 event = e->event;
288 chan = event->chan;
289 assert(!event->desc);
290 event->desc = desc;
291 event->pending_probe = NULL;
292 remove_pending_probe(e);
293 ret |= __tracepoint_probe_register(name,
294 event->desc->probe_callback,
295 event, event->desc->signature);
296 if (ret)
297 continue;
298 event->id = chan->free_event_id++;
299 ret |= _ltt_event_metadata_statedump(chan->session, chan,
300 event);
301 }
302 return ret;
303 }
304
305 void synchronize_trace(void)
306 {
307 synchronize_rcu();
308 }
309
310 struct ltt_session *ltt_session_create(void)
311 {
312 struct ltt_session *session;
313
314 session = zmalloc(sizeof(struct ltt_session));
315 if (!session)
316 return NULL;
317 CDS_INIT_LIST_HEAD(&session->chan);
318 CDS_INIT_LIST_HEAD(&session->events);
319 CDS_INIT_LIST_HEAD(&session->wildcards);
320 uuid_generate(session->uuid);
321 cds_list_add(&session->list, &sessions);
322 return session;
323 }
324
325 void ltt_session_destroy(struct ltt_session *session)
326 {
327 struct ltt_channel *chan, *tmpchan;
328 struct ltt_event *event, *tmpevent;
329 struct session_wildcard *wildcard, *tmpwildcard;
330 int ret;
331
332 CMM_ACCESS_ONCE(session->active) = 0;
333 cds_list_for_each_entry(event, &session->events, list) {
334 ret = _ltt_event_unregister(event);
335 WARN_ON(ret);
336 }
337 synchronize_trace(); /* Wait for in-flight events to complete */
338 cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list)
339 _ltt_wildcard_destroy(wildcard);
340 cds_list_for_each_entry_safe(event, tmpevent, &session->events, list)
341 _ltt_event_destroy(event);
342 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
343 _ltt_channel_destroy(chan);
344 cds_list_del(&session->list);
345 free(session);
346 }
347
348 int ltt_session_enable(struct ltt_session *session)
349 {
350 int ret = 0;
351 struct ltt_channel *chan;
352
353 if (session->active) {
354 ret = -EBUSY;
355 goto end;
356 }
357
358 /*
359 * Snapshot the number of events per channel to know the type of header
360 * we need to use.
361 */
362 cds_list_for_each_entry(chan, &session->chan, list) {
363 if (chan->header_type)
364 continue; /* don't change it if session stop/restart */
365 if (chan->free_event_id < 31)
366 chan->header_type = 1; /* compact */
367 else
368 chan->header_type = 2; /* large */
369 }
370
371 CMM_ACCESS_ONCE(session->active) = 1;
372 CMM_ACCESS_ONCE(session->been_active) = 1;
373 ret = _ltt_session_metadata_statedump(session);
374 if (ret)
375 CMM_ACCESS_ONCE(session->active) = 0;
376 end:
377 return ret;
378 }
379
380 int ltt_session_disable(struct ltt_session *session)
381 {
382 int ret = 0;
383
384 if (!session->active) {
385 ret = -EBUSY;
386 goto end;
387 }
388 CMM_ACCESS_ONCE(session->active) = 0;
389 end:
390 return ret;
391 }
392
393 int ltt_channel_enable(struct ltt_channel *channel)
394 {
395 int old;
396
397 if (channel == channel->session->metadata)
398 return -EPERM;
399 old = uatomic_xchg(&channel->enabled, 1);
400 if (old)
401 return -EEXIST;
402 return 0;
403 }
404
405 int ltt_channel_disable(struct ltt_channel *channel)
406 {
407 int old;
408
409 if (channel == channel->session->metadata)
410 return -EPERM;
411 old = uatomic_xchg(&channel->enabled, 0);
412 if (!old)
413 return -EEXIST;
414 return 0;
415 }
416
417 int ltt_event_enable(struct ltt_event *event)
418 {
419 int old;
420
421 if (event->chan == event->chan->session->metadata)
422 return -EPERM;
423 old = uatomic_xchg(&event->enabled, 1);
424 if (old)
425 return -EEXIST;
426 return 0;
427 }
428
429 int ltt_event_disable(struct ltt_event *event)
430 {
431 int old;
432
433 if (event->chan == event->chan->session->metadata)
434 return -EPERM;
435 old = uatomic_xchg(&event->enabled, 0);
436 if (!old)
437 return -EEXIST;
438 return 0;
439 }
440
441 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
442 const char *transport_name,
443 void *buf_addr,
444 size_t subbuf_size, size_t num_subbuf,
445 unsigned int switch_timer_interval,
446 unsigned int read_timer_interval,
447 int **shm_fd, int **wait_fd,
448 uint64_t **memory_map_size,
449 struct ltt_channel *chan_priv_init)
450 {
451 struct ltt_channel *chan = NULL;
452 struct ltt_transport *transport;
453
454 if (session->been_active)
455 goto active; /* Refuse to add channel to active session */
456 transport = ltt_transport_find(transport_name);
457 if (!transport) {
458 DBG("LTTng transport %s not found\n",
459 transport_name);
460 goto notransport;
461 }
462 chan_priv_init->id = session->free_chan_id++;
463 chan_priv_init->session = session;
464 /*
465 * Note: the channel creation op already writes into the packet
466 * headers. Therefore the "chan" information used as input
467 * should be already accessible.
468 */
469 chan = transport->ops.channel_create(transport_name, buf_addr,
470 subbuf_size, num_subbuf, switch_timer_interval,
471 read_timer_interval, shm_fd, wait_fd,
472 memory_map_size, chan_priv_init);
473 if (!chan)
474 goto create_error;
475 chan->enabled = 1;
476 chan->ops = &transport->ops;
477 cds_list_add(&chan->list, &session->chan);
478 return chan;
479
480 create_error:
481 notransport:
482 active:
483 return NULL;
484 }
485
486 /*
487 * Only used internally at session destruction.
488 */
489 static
490 void _ltt_channel_destroy(struct ltt_channel *chan)
491 {
492 cds_list_del(&chan->list);
493 lttng_destroy_context(chan->ctx);
494 chan->ops->channel_destroy(chan);
495 }
496
497 /*
498 * Supports event creation while tracing session is active.
499 */
500 int ltt_event_create(struct ltt_channel *chan,
501 struct lttng_ust_event *event_param,
502 void (*filter)(struct ltt_event *event),
503 struct ltt_event **_event)
504 {
505 const struct lttng_event_desc *desc = NULL; /* silence gcc */
506 struct ltt_event *event;
507 int ret = 0;
508
509 if (chan->used_event_id == -1U) {
510 ret = -ENOMEM;
511 goto full;
512 }
513 /*
514 * This is O(n^2) (for each event, the loop is called at event
515 * creation). Might require a hash if we have lots of events.
516 */
517 cds_list_for_each_entry(event, &chan->session->events, list) {
518 if (event->desc && !strncmp(event->desc->name,
519 event_param->name,
520 LTTNG_UST_SYM_NAME_LEN - 1)) {
521 ret = -EEXIST;
522 goto exist;
523 }
524 }
525
526 /*
527 * Check if loglevel match. Refuse to connect event if not.
528 */
529 if (event_param->instrumentation == LTTNG_UST_TRACEPOINT) {
530 desc = ltt_event_get(event_param->name);
531 if (desc) {
532 if (!ltt_loglevel_match(desc,
533 event_param->loglevel_type,
534 event_param->loglevel)) {
535 ret = -EPERM;
536 goto no_loglevel_match;
537 }
538 }
539 /*
540 * If descriptor is not there, it will be added to
541 * pending probes.
542 */
543 }
544 event = zmalloc(sizeof(struct ltt_event));
545 if (!event) {
546 ret = -ENOMEM;
547 goto cache_error;
548 }
549 event->chan = chan;
550 event->filter = filter;
551 /*
552 * used_event_id counts the maximum number of event IDs that can
553 * register if all probes register.
554 */
555 chan->used_event_id++;
556 event->enabled = 1;
557 event->instrumentation = event_param->instrumentation;
558 /* Populate ltt_event structure before tracepoint registration. */
559 cmm_smp_wmb();
560 switch (event_param->instrumentation) {
561 case LTTNG_UST_TRACEPOINT:
562 event->desc = desc;
563 if (event->desc) {
564 ret = __tracepoint_probe_register(event_param->name,
565 event->desc->probe_callback,
566 event, event->desc->signature);
567 if (ret)
568 goto register_error;
569 event->id = chan->free_event_id++;
570 } else {
571 /*
572 * If the probe is not present, event->desc stays NULL,
573 * waiting for the probe to register, and the event->id
574 * stays unallocated.
575 */
576 ret = add_pending_probe(event, event_param->name,
577 event_param->loglevel_type,
578 event_param->loglevel);
579 if (ret)
580 goto add_pending_error;
581 }
582 break;
583 default:
584 WARN_ON_ONCE(1);
585 }
586 if (event->desc) {
587 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
588 if (ret)
589 goto statedump_error;
590 }
591 cds_list_add(&event->list, &chan->session->events);
592 *_event = event;
593 return 0;
594
595 statedump_error:
596 if (event->desc) {
597 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name,
598 event->desc->probe_callback,
599 event));
600 ltt_event_put(event->desc);
601 }
602 add_pending_error:
603 register_error:
604 free(event);
605 cache_error:
606 no_loglevel_match:
607 exist:
608 full:
609 return ret;
610 }
611
612 /*
613 * Only used internally at session destruction.
614 */
615 int _ltt_event_unregister(struct ltt_event *event)
616 {
617 int ret = -EINVAL;
618
619 switch (event->instrumentation) {
620 case LTTNG_UST_TRACEPOINT:
621 if (event->desc) {
622 ret = __tracepoint_probe_unregister(event->desc->name,
623 event->desc->probe_callback,
624 event);
625 if (ret)
626 return ret;
627 } else {
628 remove_pending_probe(event->pending_probe);
629 ret = 0;
630 }
631 break;
632 default:
633 WARN_ON_ONCE(1);
634 }
635 return ret;
636 }
637
638 /*
639 * Only used internally at session destruction.
640 */
641 static
642 void _ltt_event_destroy(struct ltt_event *event)
643 {
644 switch (event->instrumentation) {
645 case LTTNG_UST_TRACEPOINT:
646 if (event->desc) {
647 ltt_event_put(event->desc);
648 }
649 break;
650 default:
651 WARN_ON_ONCE(1);
652 }
653 cds_list_del(&event->list);
654 lttng_destroy_context(event->ctx);
655 free(event);
656 }
657
658 /*
659 * We have exclusive access to our metadata buffer (protected by the
660 * ust_lock), so we can do racy operations such as looking for
661 * remaining space left in packet and write, since mutual exclusion
662 * protects us from concurrent writes.
663 */
664 int lttng_metadata_printf(struct ltt_session *session,
665 const char *fmt, ...)
666 {
667 struct lttng_ust_lib_ring_buffer_ctx ctx;
668 struct ltt_channel *chan = session->metadata;
669 char *str = NULL;
670 int ret = 0, waitret;
671 size_t len, reserve_len, pos;
672 va_list ap;
673
674 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
675
676 va_start(ap, fmt);
677 ret = vasprintf(&str, fmt, ap);
678 va_end(ap);
679 if (ret < 0)
680 return -ENOMEM;
681
682 len = strlen(str);
683 pos = 0;
684
685 for (pos = 0; pos < len; pos += reserve_len) {
686 reserve_len = min_t(size_t,
687 chan->ops->packet_avail_size(chan->chan, chan->handle),
688 len - pos);
689 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
690 sizeof(char), -1, chan->handle);
691 /*
692 * We don't care about metadata buffer's records lost
693 * count, because we always retry here. Report error if
694 * we need to bail out after timeout or being
695 * interrupted.
696 */
697 waitret = wait_cond_interruptible_timeout(
698 ({
699 ret = chan->ops->event_reserve(&ctx, 0);
700 ret != -ENOBUFS || !ret;
701 }),
702 LTTNG_METADATA_TIMEOUT_MSEC);
703 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
704 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
705 waitret == -EINTR ? "interrupted" :
706 (ret == -ENOBUFS ? "timeout" : "I/O error"));
707 if (waitret == -EINTR)
708 ret = waitret;
709 goto end;
710 }
711 chan->ops->event_write(&ctx, &str[pos], reserve_len);
712 chan->ops->event_commit(&ctx);
713 }
714 end:
715 free(str);
716 return ret;
717 }
718
719 static
720 int _ltt_field_statedump(struct ltt_session *session,
721 const struct lttng_event_field *field)
722 {
723 int ret = 0;
724
725 switch (field->type.atype) {
726 case atype_integer:
727 ret = lttng_metadata_printf(session,
728 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
729 field->type.u.basic.integer.size,
730 field->type.u.basic.integer.alignment,
731 field->type.u.basic.integer.signedness,
732 (field->type.u.basic.integer.encoding == lttng_encode_none)
733 ? "none"
734 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
735 ? "UTF8"
736 : "ASCII",
737 field->type.u.basic.integer.base,
738 #if (BYTE_ORDER == BIG_ENDIAN)
739 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
740 #else
741 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
742 #endif
743 field->name);
744 break;
745 case atype_float:
746 ret = lttng_metadata_printf(session,
747 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
748 field->type.u.basic._float.exp_dig,
749 field->type.u.basic._float.mant_dig,
750 field->type.u.basic._float.alignment,
751 #if (BYTE_ORDER == BIG_ENDIAN)
752 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
753 #else
754 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
755 #endif
756 field->name);
757 break;
758 case atype_enum:
759 ret = lttng_metadata_printf(session,
760 " %s %s;\n",
761 field->type.u.basic.enumeration.name,
762 field->name);
763 break;
764 case atype_array:
765 {
766 const struct lttng_basic_type *elem_type;
767
768 elem_type = &field->type.u.array.elem_type;
769 ret = lttng_metadata_printf(session,
770 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
771 elem_type->u.basic.integer.size,
772 elem_type->u.basic.integer.alignment,
773 elem_type->u.basic.integer.signedness,
774 (elem_type->u.basic.integer.encoding == lttng_encode_none)
775 ? "none"
776 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
777 ? "UTF8"
778 : "ASCII",
779 elem_type->u.basic.integer.base,
780 #if (BYTE_ORDER == BIG_ENDIAN)
781 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
782 #else
783 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
784 #endif
785 field->name, field->type.u.array.length);
786 break;
787 }
788 case atype_sequence:
789 {
790 const struct lttng_basic_type *elem_type;
791 const struct lttng_basic_type *length_type;
792
793 elem_type = &field->type.u.sequence.elem_type;
794 length_type = &field->type.u.sequence.length_type;
795 ret = lttng_metadata_printf(session,
796 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
797 length_type->u.basic.integer.size,
798 (unsigned int) length_type->u.basic.integer.alignment,
799 length_type->u.basic.integer.signedness,
800 (length_type->u.basic.integer.encoding == lttng_encode_none)
801 ? "none"
802 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
803 ? "UTF8"
804 : "ASCII"),
805 length_type->u.basic.integer.base,
806 #if (BYTE_ORDER == BIG_ENDIAN)
807 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
808 #else
809 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
810 #endif
811 field->name);
812 if (ret)
813 return ret;
814
815 ret = lttng_metadata_printf(session,
816 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
817 elem_type->u.basic.integer.size,
818 (unsigned int) elem_type->u.basic.integer.alignment,
819 elem_type->u.basic.integer.signedness,
820 (elem_type->u.basic.integer.encoding == lttng_encode_none)
821 ? "none"
822 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
823 ? "UTF8"
824 : "ASCII"),
825 elem_type->u.basic.integer.base,
826 #if (BYTE_ORDER == BIG_ENDIAN)
827 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
828 #else
829 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
830 #endif
831 field->name,
832 field->name);
833 break;
834 }
835
836 case atype_string:
837 /* Default encoding is UTF8 */
838 ret = lttng_metadata_printf(session,
839 " string%s _%s;\n",
840 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
841 " { encoding = ASCII; }" : "",
842 field->name);
843 break;
844 default:
845 WARN_ON_ONCE(1);
846 return -EINVAL;
847 }
848 return ret;
849 }
850
851 static
852 int _ltt_context_metadata_statedump(struct ltt_session *session,
853 struct lttng_ctx *ctx)
854 {
855 int ret = 0;
856 int i;
857
858 if (!ctx)
859 return 0;
860 for (i = 0; i < ctx->nr_fields; i++) {
861 const struct lttng_ctx_field *field = &ctx->fields[i];
862
863 ret = _ltt_field_statedump(session, &field->event_field);
864 if (ret)
865 return ret;
866 }
867 return ret;
868 }
869
870 static
871 int _ltt_fields_metadata_statedump(struct ltt_session *session,
872 struct ltt_event *event)
873 {
874 const struct lttng_event_desc *desc = event->desc;
875 int ret = 0;
876 int i;
877
878 for (i = 0; i < desc->nr_fields; i++) {
879 const struct lttng_event_field *field = &desc->fields[i];
880
881 ret = _ltt_field_statedump(session, field);
882 if (ret)
883 return ret;
884 }
885 return ret;
886 }
887
888 static
889 int _ltt_event_metadata_statedump(struct ltt_session *session,
890 struct ltt_channel *chan,
891 struct ltt_event *event)
892 {
893 int ret = 0;
894 int loglevel = TRACE_DEFAULT;
895
896 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
897 return 0;
898 if (chan == session->metadata)
899 return 0;
900 /*
901 * Don't print events for which probe load is pending.
902 */
903 if (!event->desc)
904 return 0;
905
906 ret = lttng_metadata_printf(session,
907 "event {\n"
908 " name = \"%s\";\n"
909 " id = %u;\n"
910 " stream_id = %u;\n",
911 event->desc->name,
912 event->id,
913 event->chan->id);
914 if (ret)
915 goto end;
916
917 if (event->desc->loglevel)
918 loglevel = *(*event->desc->loglevel);
919
920 ret = lttng_metadata_printf(session,
921 " loglevel = %d;\n",
922 loglevel);
923 if (ret)
924 goto end;
925
926 if (event->ctx) {
927 ret = lttng_metadata_printf(session,
928 " context := struct {\n");
929 if (ret)
930 goto end;
931 }
932 ret = _ltt_context_metadata_statedump(session, event->ctx);
933 if (ret)
934 goto end;
935 if (event->ctx) {
936 ret = lttng_metadata_printf(session,
937 " };\n");
938 if (ret)
939 goto end;
940 }
941
942 ret = lttng_metadata_printf(session,
943 " fields := struct {\n"
944 );
945 if (ret)
946 goto end;
947
948 ret = _ltt_fields_metadata_statedump(session, event);
949 if (ret)
950 goto end;
951
952 /*
953 * LTTng space reservation can only reserve multiples of the
954 * byte size.
955 */
956 ret = lttng_metadata_printf(session,
957 " };\n"
958 "};\n\n");
959 if (ret)
960 goto end;
961
962 event->metadata_dumped = 1;
963 end:
964 return ret;
965
966 }
967
968 static
969 int _ltt_channel_metadata_statedump(struct ltt_session *session,
970 struct ltt_channel *chan)
971 {
972 int ret = 0;
973
974 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
975 return 0;
976 if (chan == session->metadata)
977 return 0;
978
979 WARN_ON_ONCE(!chan->header_type);
980 ret = lttng_metadata_printf(session,
981 "stream {\n"
982 " id = %u;\n"
983 " event.header := %s;\n"
984 " packet.context := struct packet_context;\n",
985 chan->id,
986 chan->header_type == 1 ? "struct event_header_compact" :
987 "struct event_header_large");
988 if (ret)
989 goto end;
990
991 if (chan->ctx) {
992 ret = lttng_metadata_printf(session,
993 " event.context := struct {\n");
994 if (ret)
995 goto end;
996 }
997 ret = _ltt_context_metadata_statedump(session, chan->ctx);
998 if (ret)
999 goto end;
1000 if (chan->ctx) {
1001 ret = lttng_metadata_printf(session,
1002 " };\n");
1003 if (ret)
1004 goto end;
1005 }
1006
1007 ret = lttng_metadata_printf(session,
1008 "};\n\n");
1009
1010 chan->metadata_dumped = 1;
1011 end:
1012 return ret;
1013 }
1014
1015 static
1016 int _ltt_stream_packet_context_declare(struct ltt_session *session)
1017 {
1018 return lttng_metadata_printf(session,
1019 "struct packet_context {\n"
1020 " uint64_clock_monotonic_t timestamp_begin;\n"
1021 " uint64_clock_monotonic_t timestamp_end;\n"
1022 " uint32_t events_discarded;\n"
1023 " uint32_t content_size;\n"
1024 " uint32_t packet_size;\n"
1025 " uint32_t cpu_id;\n"
1026 "};\n\n"
1027 );
1028 }
1029
1030 /*
1031 * Compact header:
1032 * id: range: 0 - 30.
1033 * id 31 is reserved to indicate an extended header.
1034 *
1035 * Large header:
1036 * id: range: 0 - 65534.
1037 * id 65535 is reserved to indicate an extended header.
1038 */
1039 static
1040 int _ltt_event_header_declare(struct ltt_session *session)
1041 {
1042 return lttng_metadata_printf(session,
1043 "struct event_header_compact {\n"
1044 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1045 " variant <id> {\n"
1046 " struct {\n"
1047 " uint27_clock_monotonic_t timestamp;\n"
1048 " } compact;\n"
1049 " struct {\n"
1050 " uint32_t id;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1052 " } extended;\n"
1053 " } v;\n"
1054 "} align(%u);\n"
1055 "\n"
1056 "struct event_header_large {\n"
1057 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1058 " variant <id> {\n"
1059 " struct {\n"
1060 " uint32_clock_monotonic_t timestamp;\n"
1061 " } compact;\n"
1062 " struct {\n"
1063 " uint32_t id;\n"
1064 " uint64_clock_monotonic_t timestamp;\n"
1065 " } extended;\n"
1066 " } v;\n"
1067 "} align(%u);\n\n",
1068 lttng_alignof(uint32_t) * CHAR_BIT,
1069 lttng_alignof(uint16_t) * CHAR_BIT
1070 );
1071 }
1072
1073 /*
1074 * Approximation of NTP time of day to clock monotonic correlation,
1075 * taken at start of trace.
1076 * Yes, this is only an approximation. Yes, we can (and will) do better
1077 * in future versions.
1078 */
1079 static
1080 uint64_t measure_clock_offset(void)
1081 {
1082 uint64_t offset, monotonic[2], realtime;
1083 struct timespec rts = { 0, 0 };
1084 int ret;
1085
1086 monotonic[0] = trace_clock_read64();
1087 ret = clock_gettime(CLOCK_REALTIME, &rts);
1088 if (ret < 0)
1089 return 0;
1090 monotonic[1] = trace_clock_read64();
1091 offset = (monotonic[0] + monotonic[1]) >> 1;
1092 realtime = (uint64_t) rts.tv_sec * 1000000000ULL;
1093 realtime += rts.tv_nsec;
1094 offset = realtime - offset;
1095 return offset;
1096 }
1097
1098 /*
1099 * Output metadata into this session's metadata buffers.
1100 */
1101 static
1102 int _ltt_session_metadata_statedump(struct ltt_session *session)
1103 {
1104 unsigned char *uuid_c = session->uuid;
1105 char uuid_s[37], clock_uuid_s[CLOCK_UUID_LEN];
1106 struct ltt_channel *chan;
1107 struct ltt_event *event;
1108 int ret = 0;
1109 char procname[PROCNAME_LEN] = "";
1110
1111 if (!CMM_ACCESS_ONCE(session->active))
1112 return 0;
1113 if (session->metadata_dumped)
1114 goto skip_session;
1115 if (!session->metadata) {
1116 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1117 return -EPERM;
1118 }
1119
1120 snprintf(uuid_s, sizeof(uuid_s),
1121 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1122 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1123 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1124 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1125 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1126
1127 ret = lttng_metadata_printf(session,
1128 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1129 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1130 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1131 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1132 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1133 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1134 "\n"
1135 "trace {\n"
1136 " major = %u;\n"
1137 " minor = %u;\n"
1138 " uuid = \"%s\";\n"
1139 " byte_order = %s;\n"
1140 " packet.header := struct {\n"
1141 " uint32_t magic;\n"
1142 " uint8_t uuid[16];\n"
1143 " uint32_t stream_id;\n"
1144 " };\n"
1145 "};\n\n",
1146 lttng_alignof(uint8_t) * CHAR_BIT,
1147 lttng_alignof(uint16_t) * CHAR_BIT,
1148 lttng_alignof(uint32_t) * CHAR_BIT,
1149 lttng_alignof(uint64_t) * CHAR_BIT,
1150 CTF_SPEC_MAJOR,
1151 CTF_SPEC_MINOR,
1152 uuid_s,
1153 #if (BYTE_ORDER == BIG_ENDIAN)
1154 "be"
1155 #else
1156 "le"
1157 #endif
1158 );
1159 if (ret)
1160 goto end;
1161
1162 /* ignore error, just use empty string if error. */
1163 (void) prctl(PR_GET_NAME, (unsigned long) procname, 0, 0, 0);
1164 procname[PROCNAME_LEN - 1] = '\0';
1165 ret = lttng_metadata_printf(session,
1166 "env {\n"
1167 " vpid = %d;\n"
1168 " procname = \"%s\";\n"
1169 " domain = \"ust\";\n"
1170 " tracer_name = \"lttng-ust\";\n"
1171 " tracer_major = %u;\n"
1172 " tracer_minor = %u;\n"
1173 " tracer_patchlevel = %u;\n"
1174 "};\n\n",
1175 (int) getpid(),
1176 procname,
1177 LTTNG_UST_MAJOR_VERSION,
1178 LTTNG_UST_MINOR_VERSION,
1179 LTTNG_UST_PATCHLEVEL_VERSION
1180 );
1181 if (ret)
1182 goto end;
1183
1184 ret = lttng_metadata_printf(session,
1185 "clock {\n"
1186 " name = %s;\n",
1187 "monotonic"
1188 );
1189 if (ret)
1190 goto end;
1191
1192 if (!trace_clock_uuid(clock_uuid_s)) {
1193 ret = lttng_metadata_printf(session,
1194 " uuid = \"%s\";\n",
1195 clock_uuid_s
1196 );
1197 if (ret)
1198 goto end;
1199 }
1200
1201 ret = lttng_metadata_printf(session,
1202 " description = \"Monotonic Clock\";\n"
1203 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1204 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1205 " offset = %" PRIu64 ";\n"
1206 "};\n\n",
1207 trace_clock_freq(),
1208 measure_clock_offset()
1209 );
1210 if (ret)
1211 goto end;
1212
1213 ret = lttng_metadata_printf(session,
1214 "typealias integer {\n"
1215 " size = 27; align = 1; signed = false;\n"
1216 " map = clock.monotonic.value;\n"
1217 "} := uint27_clock_monotonic_t;\n"
1218 "\n"
1219 "typealias integer {\n"
1220 " size = 32; align = %u; signed = false;\n"
1221 " map = clock.monotonic.value;\n"
1222 "} := uint32_clock_monotonic_t;\n"
1223 "\n"
1224 "typealias integer {\n"
1225 " size = 64; align = %u; signed = false;\n"
1226 " map = clock.monotonic.value;\n"
1227 "} := uint64_clock_monotonic_t;\n\n",
1228 lttng_alignof(uint32_t) * CHAR_BIT,
1229 lttng_alignof(uint64_t) * CHAR_BIT
1230 );
1231 if (ret)
1232 goto end;
1233
1234 ret = _ltt_stream_packet_context_declare(session);
1235 if (ret)
1236 goto end;
1237
1238 ret = _ltt_event_header_declare(session);
1239 if (ret)
1240 goto end;
1241
1242 skip_session:
1243 cds_list_for_each_entry(chan, &session->chan, list) {
1244 ret = _ltt_channel_metadata_statedump(session, chan);
1245 if (ret)
1246 goto end;
1247 }
1248
1249 cds_list_for_each_entry(event, &session->events, list) {
1250 ret = _ltt_event_metadata_statedump(session, event->chan, event);
1251 if (ret)
1252 goto end;
1253 }
1254 session->metadata_dumped = 1;
1255 end:
1256 return ret;
1257 }
1258
1259 void lttng_ust_events_exit(void)
1260 {
1261 struct ltt_session *session, *tmpsession;
1262
1263 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
1264 ltt_session_destroy(session);
1265 }
1266
1267 /* WILDCARDS */
1268
1269 static
1270 int wildcard_same_loglevel(struct wildcard_entry *e,
1271 enum lttng_ust_loglevel_type loglevel_type,
1272 int loglevel)
1273 {
1274 if (e->loglevel_type == loglevel_type && e->loglevel == loglevel)
1275 return 1;
1276 else
1277 return 0;
1278 }
1279
1280 #if 0
1281 static
1282 int wildcard_is_within(struct wildcard_entry *e,
1283 enum lttng_ust_loglevel_type loglevel_type,
1284 int loglevel)
1285 {
1286 if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL
1287 || e->loglevel == -1)
1288 return 1;
1289 switch (e->loglevel_type) {
1290 case LTTNG_UST_LOGLEVEL_RANGE:
1291 switch (loglevel_type) {
1292 case LTTNG_UST_LOGLEVEL_RANGE:
1293 if (e->loglevel >= loglevel)
1294 return 1;
1295 else
1296 return 0;
1297 case LTTNG_UST_LOGLEVEL_SINGLE:
1298 if (e->loglevel <= 0 && loglevel == 0)
1299 return 1;
1300 else
1301 return 0;
1302 }
1303 case LTTNG_UST_LOGLEVEL_SINGLE:
1304 switch (loglevel_type) {
1305 case LTTNG_UST_LOGLEVEL_RANGE:
1306 if (loglevel <= 0)
1307 return 1;
1308 else
1309 return 0;
1310 case LTTNG_UST_LOGLEVEL_SINGLE:
1311 if (e->loglevel == loglevel)
1312 return 1;
1313 else
1314 return 0;
1315 }
1316 }
1317 }
1318 #endif
1319
1320 /*
1321 * Add the wildcard to the wildcard list. Must be called with
1322 * ust lock held.
1323 */
1324 static
1325 struct session_wildcard *add_wildcard(struct ltt_channel *chan,
1326 struct lttng_ust_event *event_param)
1327 {
1328 struct wildcard_entry *e;
1329 struct session_wildcard *sw;
1330 size_t name_len = strlen(event_param->name) + 1;
1331 int found = 0;
1332
1333 /*
1334 * Try to find global wildcard entry. Given that this is shared
1335 * across all sessions, we need to check for exact loglevel
1336 * match, not just whether contained within the existing ones.
1337 */
1338 cds_list_for_each_entry(e, &wildcard_list, list) {
1339 if (!strncmp(event_param->name, e->name,
1340 LTTNG_UST_SYM_NAME_LEN - 1)) {
1341 if (wildcard_same_loglevel(e,
1342 event_param->loglevel_type,
1343 event_param->loglevel)) {
1344 found = 1;
1345 break;
1346 }
1347 }
1348 }
1349
1350 if (!found) {
1351 /*
1352 * Create global wildcard entry if not found. Using
1353 * zmalloc here to allocate a variable length element.
1354 * Could cause some memory fragmentation if overused.
1355 */
1356 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
1357 if (!e)
1358 return ERR_PTR(-ENOMEM);
1359 memcpy(&e->name[0], event_param->name, name_len);
1360 e->loglevel_type = event_param->loglevel_type;
1361 e->loglevel = event_param->loglevel;
1362 cds_list_add(&e->list, &wildcard_list);
1363 CDS_INIT_LIST_HEAD(&e->session_list);
1364 }
1365
1366 /* session wildcard */
1367 cds_list_for_each_entry(sw, &e->session_list, session_list) {
1368 if (chan == sw->chan) {
1369 DBG("wildcard %s busy for this channel",
1370 event_param->name);
1371 return ERR_PTR(-EEXIST); /* Already there */
1372 }
1373 }
1374 sw = zmalloc(sizeof(struct session_wildcard));
1375 if (!sw)
1376 return ERR_PTR(-ENOMEM);
1377 sw->chan = chan;
1378 sw->enabled = 1;
1379 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
1380 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
1381 sw->event_param.loglevel_type = event_param->loglevel_type;
1382 sw->event_param.loglevel = event_param->loglevel;
1383 CDS_INIT_LIST_HEAD(&sw->events);
1384 cds_list_add(&sw->list, &chan->session->wildcards);
1385 cds_list_add(&sw->session_list, &e->session_list);
1386 sw->entry = e;
1387 ltt_probes_create_wildcard_events(e, sw);
1388 return sw;
1389 }
1390
1391 /*
1392 * Remove the wildcard from the wildcard list. Must be called with
1393 * ust_lock held. Only called at session teardown.
1394 */
1395 static
1396 void _remove_wildcard(struct session_wildcard *wildcard)
1397 {
1398 struct ltt_event *ev, *tmp;
1399
1400 /*
1401 * Just remove the events owned (for enable/disable) by this
1402 * wildcard from the list. The session teardown will take care
1403 * of freeing the event memory.
1404 */
1405 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
1406 wildcard_list) {
1407 cds_list_del(&ev->wildcard_list);
1408 }
1409 cds_list_del(&wildcard->session_list);
1410 cds_list_del(&wildcard->list);
1411 if (cds_list_empty(&wildcard->entry->session_list)) {
1412 cds_list_del(&wildcard->entry->list);
1413 free(wildcard->entry);
1414 }
1415 free(wildcard);
1416 }
1417
1418 int ltt_wildcard_create(struct ltt_channel *chan,
1419 struct lttng_ust_event *event_param,
1420 struct session_wildcard **_sw)
1421 {
1422 struct session_wildcard *sw;
1423
1424 sw = add_wildcard(chan, event_param);
1425 if (!sw || IS_ERR(sw)) {
1426 return PTR_ERR(sw);
1427 }
1428 *_sw = sw;
1429 return 0;
1430 }
1431
1432 static
1433 void _ltt_wildcard_destroy(struct session_wildcard *sw)
1434 {
1435 _remove_wildcard(sw);
1436 }
1437
1438 int ltt_wildcard_enable(struct session_wildcard *wildcard)
1439 {
1440 struct ltt_event *ev;
1441 int ret;
1442
1443 if (wildcard->enabled)
1444 return -EEXIST;
1445 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1446 ret = ltt_event_enable(ev);
1447 if (ret) {
1448 DBG("Error: enable error.\n");
1449 return ret;
1450 }
1451 }
1452 wildcard->enabled = 1;
1453 return 0;
1454 }
1455
1456 int ltt_wildcard_disable(struct session_wildcard *wildcard)
1457 {
1458 struct ltt_event *ev;
1459 int ret;
1460
1461 if (!wildcard->enabled)
1462 return -EEXIST;
1463 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1464 ret = ltt_event_disable(ev);
1465 if (ret) {
1466 DBG("Error: disable error.\n");
1467 return ret;
1468 }
1469 }
1470 wildcard->enabled = 0;
1471 return 0;
1472 }
1473
1474 /*
1475 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1476 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1477 * taken in constructors, which are called with dynamic linker mutex
1478 * held.
1479 */
1480 void lttng_fixup_event_tls(void)
1481 {
1482 unsigned char uuid[37];
1483
1484 (void) uuid_generate(uuid);
1485 }
This page took 0.119106 seconds and 4 git commands to generate.