Rename libustd to libustconsumer and ustd to ust-consumerd
[lttng-ust.git] / libust / marker.c
CommitLineData
68c1021b
PMF
1/*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
34e4b7db
PMF
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
68c1021b 8 *
34e4b7db 9 * This library is distributed in the hope that it will be useful,
68c1021b 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34e4b7db
PMF
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
68c1021b 13 *
34e4b7db
PMF
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
68c1021b 17 */
59b161cd 18
909bc43f
PMF
19#include <stdlib.h>
20#include <errno.h>
769d0157 21#define _LGPL_SOURCE
b7ea1a1c 22#include <urcu-bp.h>
17bb07b4 23#include <urcu/rculist.h>
10c56168 24#include <urcu/hlist.h>
769d0157 25
518d7abb 26#include <ust/core.h>
93d0f2ea 27#include <ust/marker.h>
909bc43f 28
59b161cd
PMF
29#include "usterr.h"
30#include "channels.h"
31#include "tracercore.h"
c93858f1 32#include "tracer.h"
68c1021b 33
636ca5d6
PMF
34__thread long ust_reg_stack[500];
35volatile __thread long *ust_reg_stack_ptr = (long *) 0;
36
c463904d
PMF
37extern struct marker __start___markers[] __attribute__((visibility("hidden")));
38extern struct marker __stop___markers[] __attribute__((visibility("hidden")));
defa46a7 39
68c1021b
PMF
40/* Set to 1 to enable marker debug output */
41static const int marker_debug;
42
43/*
44 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
45 * and module markers and the hash table.
46 */
47static DEFINE_MUTEX(markers_mutex);
48
0222e121 49static CDS_LIST_HEAD(libs);
772030fe
PMF
50
51
68c1021b
PMF
52void lock_markers(void)
53{
f7b16408 54 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
55}
56
57void unlock_markers(void)
58{
f7b16408 59 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
60}
61
62/*
63 * Marker hash table, containing the active markers.
64 * Protected by module_mutex.
65 */
66#define MARKER_HASH_BITS 6
67#define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
10c56168 68static struct cds_hlist_head marker_table[MARKER_TABLE_SIZE];
68c1021b
PMF
69
70/*
71 * Note about RCU :
72 * It is used to make sure every handler has finished using its private data
73 * between two consecutive operation (add or remove) on a given marker. It is
74 * also used to delay the free of multiple probes array until a quiescent state
75 * is reached.
76 * marker entries modifications are protected by the markers_mutex.
77 */
78struct marker_entry {
10c56168 79 struct cds_hlist_node hlist;
68c1021b
PMF
80 char *format;
81 char *name;
82 /* Probe wrapper */
75667d04 83 void (*call)(const struct marker *mdata, void *call_private, struct registers *regs, ...);
68c1021b
PMF
84 struct marker_probe_closure single;
85 struct marker_probe_closure *multi;
86 int refcount; /* Number of times armed. 0 if disarmed. */
87 struct rcu_head rcu;
88 void *oldptr;
89 int rcu_pending;
90 u16 channel_id;
91 u16 event_id;
92 unsigned char ptype:1;
93 unsigned char format_allocated:1;
94 char channel[0]; /* Contains channel'\0'name'\0'format'\0' */
95};
96
97#ifdef CONFIG_MARKERS_USERSPACE
98static void marker_update_processes(void);
99#else
100static void marker_update_processes(void)
101{
102}
103#endif
104
105/**
106 * __mark_empty_function - Empty probe callback
107 * @mdata: marker data
108 * @probe_private: probe private data
109 * @call_private: call site private data
110 * @fmt: format string
111 * @...: variable argument list
112 *
113 * Empty callback provided as a probe to the markers. By providing this to a
114 * disabled marker, we make sure the execution flow is always valid even
115 * though the function pointer change and the marker enabling are two distinct
116 * operations that modifies the execution flow of preemptible code.
117 */
118notrace void __mark_empty_function(const struct marker *mdata,
75667d04 119 void *probe_private, struct registers *regs, void *call_private, const char *fmt, va_list *args)
68c1021b
PMF
120{
121}
59b161cd 122//ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
68c1021b
PMF
123
124/*
125 * marker_probe_cb Callback that prepares the variable argument list for probes.
126 * @mdata: pointer of type struct marker
127 * @call_private: caller site private data
128 * @...: Variable argument list.
129 *
130 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
0222e121 131 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
68c1021b
PMF
132 * rcu_dereference() for the pointer read.
133 */
134notrace void marker_probe_cb(const struct marker *mdata,
75667d04 135 void *call_private, struct registers *regs, ...)
68c1021b
PMF
136{
137 va_list args;
138 char ptype;
139
140 /*
141 * rcu_read_lock_sched does two things : disabling preemption to make
142 * sure the teardown of the callbacks can be done correctly when they
143 * are in modules and they insure RCU read coherency.
144 */
59b161cd 145//ust// rcu_read_lock_sched_notrace();
68c1021b
PMF
146 ptype = mdata->ptype;
147 if (likely(!ptype)) {
148 marker_probe_func *func;
149 /* Must read the ptype before ptr. They are not data dependant,
0222e121
MD
150 * so we put an explicit cmm_smp_rmb() here. */
151 cmm_smp_rmb();
68c1021b
PMF
152 func = mdata->single.func;
153 /* Must read the ptr before private data. They are not data
0222e121
MD
154 * dependant, so we put an explicit cmm_smp_rmb() here. */
155 cmm_smp_rmb();
75667d04
PMF
156 va_start(args, regs);
157 func(mdata, mdata->single.probe_private, regs, call_private,
68c1021b
PMF
158 mdata->format, &args);
159 va_end(args);
160 } else {
161 struct marker_probe_closure *multi;
162 int i;
163 /*
164 * Read mdata->ptype before mdata->multi.
165 */
0222e121 166 cmm_smp_rmb();
68c1021b
PMF
167 multi = mdata->multi;
168 /*
169 * multi points to an array, therefore accessing the array
170 * depends on reading multi. However, even in this case,
171 * we must insure that the pointer is read _before_ the array
0222e121
MD
172 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
173 * in the fast path, so put the explicit cmm_barrier here.
68c1021b 174 */
0222e121 175 cmm_smp_read_barrier_depends();
68c1021b 176 for (i = 0; multi[i].func; i++) {
75667d04 177 va_start(args, regs);
68c1021b 178 multi[i].func(mdata, multi[i].probe_private,
75667d04 179 regs, call_private, mdata->format, &args);
68c1021b
PMF
180 va_end(args);
181 }
182 }
59b161cd 183//ust// rcu_read_unlock_sched_notrace();
68c1021b 184}
59b161cd 185//ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
68c1021b
PMF
186
187/*
188 * marker_probe_cb Callback that does not prepare the variable argument list.
189 * @mdata: pointer of type struct marker
190 * @call_private: caller site private data
191 * @...: Variable argument list.
192 *
193 * Should be connected to markers "MARK_NOARGS".
194 */
195static notrace void marker_probe_cb_noarg(const struct marker *mdata,
75667d04 196 void *call_private, struct registers *regs, ...)
68c1021b
PMF
197{
198 va_list args; /* not initialized */
199 char ptype;
200
59b161cd 201//ust// rcu_read_lock_sched_notrace();
68c1021b
PMF
202 ptype = mdata->ptype;
203 if (likely(!ptype)) {
204 marker_probe_func *func;
205 /* Must read the ptype before ptr. They are not data dependant,
0222e121
MD
206 * so we put an explicit cmm_smp_rmb() here. */
207 cmm_smp_rmb();
68c1021b
PMF
208 func = mdata->single.func;
209 /* Must read the ptr before private data. They are not data
0222e121
MD
210 * dependant, so we put an explicit cmm_smp_rmb() here. */
211 cmm_smp_rmb();
75667d04 212 func(mdata, mdata->single.probe_private, regs, call_private,
68c1021b
PMF
213 mdata->format, &args);
214 } else {
215 struct marker_probe_closure *multi;
216 int i;
217 /*
218 * Read mdata->ptype before mdata->multi.
219 */
0222e121 220 cmm_smp_rmb();
68c1021b
PMF
221 multi = mdata->multi;
222 /*
223 * multi points to an array, therefore accessing the array
224 * depends on reading multi. However, even in this case,
225 * we must insure that the pointer is read _before_ the array
0222e121
MD
226 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
227 * in the fast path, so put the explicit cmm_barrier here.
68c1021b 228 */
0222e121 229 cmm_smp_read_barrier_depends();
68c1021b 230 for (i = 0; multi[i].func; i++)
75667d04 231 multi[i].func(mdata, multi[i].probe_private, regs,
68c1021b
PMF
232 call_private, mdata->format, &args);
233 }
59b161cd 234//ust// rcu_read_unlock_sched_notrace();
68c1021b
PMF
235}
236
237static void free_old_closure(struct rcu_head *head)
238{
1e4b909b 239 struct marker_entry *entry = _ust_container_of(head,
68c1021b 240 struct marker_entry, rcu);
909bc43f 241 free(entry->oldptr);
68c1021b 242 /* Make sure we free the data before setting the pending flag to 0 */
0222e121 243 cmm_smp_wmb();
68c1021b
PMF
244 entry->rcu_pending = 0;
245}
246
247static void debug_print_probes(struct marker_entry *entry)
248{
249 int i;
250
251 if (!marker_debug)
252 return;
253
254 if (!entry->ptype) {
c1f20530 255 DBG("Single probe : %p %p",
68c1021b
PMF
256 entry->single.func,
257 entry->single.probe_private);
258 } else {
259 for (i = 0; entry->multi[i].func; i++)
c1f20530 260 DBG("Multi probe %d : %p %p", i,
68c1021b
PMF
261 entry->multi[i].func,
262 entry->multi[i].probe_private);
263 }
264}
265
266static struct marker_probe_closure *
267marker_entry_add_probe(struct marker_entry *entry,
268 marker_probe_func *probe, void *probe_private)
269{
270 int nr_probes = 0;
271 struct marker_probe_closure *old, *new;
272
273 WARN_ON(!probe);
274
275 debug_print_probes(entry);
276 old = entry->multi;
277 if (!entry->ptype) {
278 if (entry->single.func == probe &&
279 entry->single.probe_private == probe_private)
280 return ERR_PTR(-EBUSY);
281 if (entry->single.func == __mark_empty_function) {
282 /* 0 -> 1 probes */
283 entry->single.func = probe;
284 entry->single.probe_private = probe_private;
285 entry->refcount = 1;
286 entry->ptype = 0;
287 debug_print_probes(entry);
288 return NULL;
289 } else {
290 /* 1 -> 2 probes */
291 nr_probes = 1;
292 old = NULL;
293 }
294 } else {
295 /* (N -> N+1), (N != 0, 1) probes */
296 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
297 if (old[nr_probes].func == probe
298 && old[nr_probes].probe_private
299 == probe_private)
300 return ERR_PTR(-EBUSY);
301 }
302 /* + 2 : one for new probe, one for NULL func */
909bc43f 303 new = zmalloc((nr_probes + 2) * sizeof(struct marker_probe_closure));
68c1021b
PMF
304 if (new == NULL)
305 return ERR_PTR(-ENOMEM);
306 if (!old)
307 new[0] = entry->single;
308 else
309 memcpy(new, old,
310 nr_probes * sizeof(struct marker_probe_closure));
311 new[nr_probes].func = probe;
312 new[nr_probes].probe_private = probe_private;
313 entry->refcount = nr_probes + 1;
314 entry->multi = new;
315 entry->ptype = 1;
316 debug_print_probes(entry);
317 return old;
318}
319
320static struct marker_probe_closure *
321marker_entry_remove_probe(struct marker_entry *entry,
322 marker_probe_func *probe, void *probe_private)
323{
324 int nr_probes = 0, nr_del = 0, i;
325 struct marker_probe_closure *old, *new;
326
327 old = entry->multi;
328
329 debug_print_probes(entry);
330 if (!entry->ptype) {
331 /* 0 -> N is an error */
332 WARN_ON(entry->single.func == __mark_empty_function);
333 /* 1 -> 0 probes */
334 WARN_ON(probe && entry->single.func != probe);
335 WARN_ON(entry->single.probe_private != probe_private);
336 entry->single.func = __mark_empty_function;
337 entry->refcount = 0;
338 entry->ptype = 0;
339 debug_print_probes(entry);
340 return NULL;
341 } else {
342 /* (N -> M), (N > 1, M >= 0) probes */
343 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
344 if ((!probe || old[nr_probes].func == probe)
345 && old[nr_probes].probe_private
346 == probe_private)
347 nr_del++;
348 }
349 }
350
351 if (nr_probes - nr_del == 0) {
352 /* N -> 0, (N > 1) */
353 entry->single.func = __mark_empty_function;
354 entry->refcount = 0;
355 entry->ptype = 0;
356 } else if (nr_probes - nr_del == 1) {
357 /* N -> 1, (N > 1) */
358 for (i = 0; old[i].func; i++)
359 if ((probe && old[i].func != probe) ||
360 old[i].probe_private != probe_private)
361 entry->single = old[i];
362 entry->refcount = 1;
363 entry->ptype = 0;
364 } else {
365 int j = 0;
366 /* N -> M, (N > 1, M > 1) */
367 /* + 1 for NULL */
909bc43f 368 new = zmalloc((nr_probes - nr_del + 1) * sizeof(struct marker_probe_closure));
68c1021b
PMF
369 if (new == NULL)
370 return ERR_PTR(-ENOMEM);
371 for (i = 0; old[i].func; i++)
372 if ((probe && old[i].func != probe) ||
373 old[i].probe_private != probe_private)
374 new[j++] = old[i];
375 entry->refcount = nr_probes - nr_del;
376 entry->ptype = 1;
377 entry->multi = new;
378 }
379 debug_print_probes(entry);
380 return old;
381}
382
383/*
384 * Get marker if the marker is present in the marker hash table.
385 * Must be called with markers_mutex held.
386 * Returns NULL if not present.
387 */
388static struct marker_entry *get_marker(const char *channel, const char *name)
389{
10c56168
DG
390 struct cds_hlist_head *head;
391 struct cds_hlist_node *node;
68c1021b
PMF
392 struct marker_entry *e;
393 size_t channel_len = strlen(channel) + 1;
394 size_t name_len = strlen(name) + 1;
395 u32 hash;
396
397 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
398 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
10c56168 399 cds_hlist_for_each_entry(e, node, head, hlist) {
68c1021b
PMF
400 if (!strcmp(channel, e->channel) && !strcmp(name, e->name))
401 return e;
402 }
403 return NULL;
404}
405
406/*
407 * Add the marker to the marker hash table. Must be called with markers_mutex
408 * held.
409 */
410static struct marker_entry *add_marker(const char *channel, const char *name,
411 const char *format)
412{
10c56168
DG
413 struct cds_hlist_head *head;
414 struct cds_hlist_node *node;
68c1021b
PMF
415 struct marker_entry *e;
416 size_t channel_len = strlen(channel) + 1;
417 size_t name_len = strlen(name) + 1;
418 size_t format_len = 0;
419 u32 hash;
420
421 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
422 if (format)
423 format_len = strlen(format) + 1;
424 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
10c56168 425 cds_hlist_for_each_entry(e, node, head, hlist) {
68c1021b 426 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
c1f20530 427 DBG("Marker %s.%s busy", channel, name);
68c1021b
PMF
428 return ERR_PTR(-EBUSY); /* Already there */
429 }
430 }
431 /*
1dba3e6c 432 * Using zmalloc here to allocate a variable length element. Could
68c1021b
PMF
433 * cause some memory fragmentation if overused.
434 */
1dba3e6c 435 e = zmalloc(sizeof(struct marker_entry)
909bc43f 436 + channel_len + name_len + format_len);
68c1021b
PMF
437 if (!e)
438 return ERR_PTR(-ENOMEM);
439 memcpy(e->channel, channel, channel_len);
440 e->name = &e->channel[channel_len];
441 memcpy(e->name, name, name_len);
442 if (format) {
eddb0f66 443 e->format = &e->name[name_len];
68c1021b
PMF
444 memcpy(e->format, format, format_len);
445 if (strcmp(e->format, MARK_NOARGS) == 0)
446 e->call = marker_probe_cb_noarg;
447 else
448 e->call = marker_probe_cb;
449 trace_mark(metadata, core_marker_format,
450 "channel %s name %s format %s",
451 e->channel, e->name, e->format);
452 } else {
453 e->format = NULL;
454 e->call = marker_probe_cb;
455 }
456 e->single.func = __mark_empty_function;
457 e->single.probe_private = NULL;
458 e->multi = NULL;
459 e->ptype = 0;
460 e->format_allocated = 0;
461 e->refcount = 0;
462 e->rcu_pending = 0;
10c56168 463 cds_hlist_add_head(&e->hlist, head);
68c1021b
PMF
464 return e;
465}
466
467/*
468 * Remove the marker from the marker hash table. Must be called with mutex_lock
469 * held.
470 */
471static int remove_marker(const char *channel, const char *name)
472{
10c56168
DG
473 struct cds_hlist_head *head;
474 struct cds_hlist_node *node;
68c1021b
PMF
475 struct marker_entry *e;
476 int found = 0;
477 size_t channel_len = strlen(channel) + 1;
478 size_t name_len = strlen(name) + 1;
479 u32 hash;
480 int ret;
481
482 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
483 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
10c56168 484 cds_hlist_for_each_entry(e, node, head, hlist) {
68c1021b
PMF
485 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
486 found = 1;
487 break;
488 }
489 }
490 if (!found)
491 return -ENOENT;
492 if (e->single.func != __mark_empty_function)
493 return -EBUSY;
10c56168 494 cds_hlist_del(&e->hlist);
68c1021b 495 if (e->format_allocated)
909bc43f 496 free(e->format);
68c1021b
PMF
497 ret = ltt_channels_unregister(e->channel);
498 WARN_ON(ret);
499 /* Make sure the call_rcu has been executed */
6cb88bc0 500//ust// if (e->rcu_pending)
0222e121 501//ust// rcu_cmm_barrier_sched();
909bc43f 502 free(e);
68c1021b
PMF
503 return 0;
504}
505
506/*
507 * Set the mark_entry format to the format found in the element.
508 */
509static int marker_set_format(struct marker_entry *entry, const char *format)
510{
909bc43f 511 entry->format = strdup(format);
68c1021b
PMF
512 if (!entry->format)
513 return -ENOMEM;
514 entry->format_allocated = 1;
515
516 trace_mark(metadata, core_marker_format,
517 "channel %s name %s format %s",
518 entry->channel, entry->name, entry->format);
519 return 0;
520}
521
522/*
523 * Sets the probe callback corresponding to one marker.
524 */
525static int set_marker(struct marker_entry *entry, struct marker *elem,
526 int active)
527{
528 int ret = 0;
529 WARN_ON(strcmp(entry->name, elem->name) != 0);
530
531 if (entry->format) {
532 if (strcmp(entry->format, elem->format) != 0) {
5c2b2d70 533 ERR("Format mismatch for probe %s (%s), marker (%s)",
68c1021b
PMF
534 entry->name,
535 entry->format,
536 elem->format);
537 return -EPERM;
538 }
539 } else {
540 ret = marker_set_format(entry, elem->format);
541 if (ret)
542 return ret;
543 }
544
545 /*
546 * probe_cb setup (statically known) is done here. It is
547 * asynchronous with the rest of execution, therefore we only
548 * pass from a "safe" callback (with argument) to an "unsafe"
549 * callback (does not set arguments).
550 */
551 elem->call = entry->call;
552 elem->channel_id = entry->channel_id;
553 elem->event_id = entry->event_id;
554 /*
555 * Sanity check :
556 * We only update the single probe private data when the ptr is
557 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
558 */
559 WARN_ON(elem->single.func != __mark_empty_function
560 && elem->single.probe_private != entry->single.probe_private
561 && !elem->ptype);
562 elem->single.probe_private = entry->single.probe_private;
563 /*
564 * Make sure the private data is valid when we update the
565 * single probe ptr.
566 */
0222e121 567 cmm_smp_wmb();
68c1021b
PMF
568 elem->single.func = entry->single.func;
569 /*
570 * We also make sure that the new probe callbacks array is consistent
571 * before setting a pointer to it.
572 */
573 rcu_assign_pointer(elem->multi, entry->multi);
574 /*
575 * Update the function or multi probe array pointer before setting the
576 * ptype.
577 */
0222e121 578 cmm_smp_wmb();
68c1021b
PMF
579 elem->ptype = entry->ptype;
580
12e81b07
PMF
581 if (elem->tp_name && (active ^ _imv_read(elem->state))) {
582 WARN_ON(!elem->tp_cb);
583 /*
584 * It is ok to directly call the probe registration because type
585 * checking has been done in the __trace_mark_tp() macro.
586 */
587
588 if (active) {
589 /*
590 * try_module_get should always succeed because we hold
591 * markers_mutex to get the tp_cb address.
592 */
59b161cd
PMF
593//ust// ret = try_module_get(__module_text_address(
594//ust// (unsigned long)elem->tp_cb));
595//ust// BUG_ON(!ret);
12e81b07
PMF
596 ret = tracepoint_probe_register_noupdate(
597 elem->tp_name,
9b9e13aa 598 elem->tp_cb, NULL);
12e81b07
PMF
599 } else {
600 ret = tracepoint_probe_unregister_noupdate(
601 elem->tp_name,
9b9e13aa 602 elem->tp_cb, NULL);
12e81b07
PMF
603 /*
604 * tracepoint_probe_update_all() must be called
605 * before the module containing tp_cb is unloaded.
606 */
59b161cd
PMF
607//ust// module_put(__module_text_address(
608//ust// (unsigned long)elem->tp_cb));
12e81b07
PMF
609 }
610 }
68c1021b
PMF
611 elem->state__imv = active;
612
613 return ret;
614}
615
616/*
617 * Disable a marker and its probe callback.
618 * Note: only waiting an RCU period after setting elem->call to the empty
619 * function insures that the original callback is not used anymore. This insured
620 * by rcu_read_lock_sched around the call site.
621 */
622static void disable_marker(struct marker *elem)
623{
12e81b07
PMF
624 int ret;
625
626 /* leave "call" as is. It is known statically. */
627 if (elem->tp_name && _imv_read(elem->state)) {
628 WARN_ON(!elem->tp_cb);
629 /*
630 * It is ok to directly call the probe registration because type
631 * checking has been done in the __trace_mark_tp() macro.
632 */
633 ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
9b9e13aa 634 elem->tp_cb, NULL);
12e81b07
PMF
635 WARN_ON(ret);
636 /*
637 * tracepoint_probe_update_all() must be called
638 * before the module containing tp_cb is unloaded.
639 */
59b161cd 640//ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
12e81b07 641 }
68c1021b
PMF
642 elem->state__imv = 0;
643 elem->single.func = __mark_empty_function;
644 /* Update the function before setting the ptype */
0222e121 645 cmm_smp_wmb();
68c1021b
PMF
646 elem->ptype = 0; /* single probe */
647 /*
648 * Leave the private data and channel_id/event_id there, because removal
649 * is racy and should be done only after an RCU period. These are never
650 * used until the next initialization anyway.
651 */
652}
653
a5311005
PMF
654/*
655 * is_marker_enabled - Check if a marker is enabled
656 * @channel: channel name
657 * @name: marker name
658 *
659 * Returns 1 if the marker is enabled, 0 if disabled.
660 */
661int is_marker_enabled(const char *channel, const char *name)
662{
663 struct marker_entry *entry;
664
f7b16408 665 pthread_mutex_lock(&markers_mutex);
a5311005 666 entry = get_marker(channel, name);
f7b16408 667 pthread_mutex_unlock(&markers_mutex);
a5311005
PMF
668
669 return entry && !!entry->refcount;
670}
671
68c1021b
PMF
672/**
673 * marker_update_probe_range - Update a probe range
674 * @begin: beginning of the range
675 * @end: end of the range
676 *
677 * Updates the probe callback corresponding to a range of markers.
678 */
679void marker_update_probe_range(struct marker *begin,
680 struct marker *end)
681{
682 struct marker *iter;
683 struct marker_entry *mark_entry;
684
f7b16408 685 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
686 for (iter = begin; iter < end; iter++) {
687 mark_entry = get_marker(iter->channel, iter->name);
688 if (mark_entry) {
689 set_marker(mark_entry, iter, !!mark_entry->refcount);
690 /*
691 * ignore error, continue
692 */
4db647c5
PMF
693
694 /* This is added for UST. We emit a core_marker_id event
695 * for markers that are already registered to a probe
696 * upon library load. Otherwise, no core_marker_id will
697 * be generated for these markers. Is this the right thing
698 * to do?
699 */
700 trace_mark(metadata, core_marker_id,
701 "channel %s name %s event_id %hu "
702 "int #1u%zu long #1u%zu pointer #1u%zu "
703 "size_t #1u%zu alignment #1u%u",
704 iter->channel, iter->name, mark_entry->event_id,
705 sizeof(int), sizeof(long), sizeof(void *),
706 sizeof(size_t), ltt_get_alignment());
68c1021b
PMF
707 } else {
708 disable_marker(iter);
709 }
710 }
f7b16408 711 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
712}
713
772030fe
PMF
714static void lib_update_markers(void)
715{
716 struct lib *lib;
717
718 /* FIXME: we should probably take a mutex here on libs */
f7b16408 719//ust// pthread_mutex_lock(&module_mutex);
0222e121 720 cds_list_for_each_entry(lib, &libs, list)
772030fe
PMF
721 marker_update_probe_range(lib->markers_start,
722 lib->markers_start + lib->markers_count);
f7b16408 723//ust// pthread_mutex_unlock(&module_mutex);
772030fe
PMF
724}
725
68c1021b
PMF
726/*
727 * Update probes, removing the faulty probes.
728 *
729 * Internal callback only changed before the first probe is connected to it.
730 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
731 * transitions. All other transitions will leave the old private data valid.
732 * This makes the non-atomicity of the callback/private data updates valid.
733 *
734 * "special case" updates :
735 * 0 -> 1 callback
736 * 1 -> 0 callback
737 * 1 -> 2 callbacks
738 * 2 -> 1 callbacks
739 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
740 * Site effect : marker_set_format may delete the marker entry (creating a
741 * replacement).
742 */
743static void marker_update_probes(void)
744{
745 /* Core kernel markers */
c463904d 746//ust// marker_update_probe_range(__start___markers, __stop___markers);
68c1021b 747 /* Markers in modules. */
59b161cd 748//ust// module_update_markers();
c463904d 749 lib_update_markers();
12e81b07 750 tracepoint_probe_update_all();
68c1021b
PMF
751 /* Update immediate values */
752 core_imv_update();
474d745f 753//ust// module_imv_update(); /* FIXME: need to port for libs? */
68c1021b
PMF
754 marker_update_processes();
755}
756
757/**
758 * marker_probe_register - Connect a probe to a marker
759 * @channel: marker channel
760 * @name: marker name
761 * @format: format string
762 * @probe: probe handler
763 * @probe_private: probe private data
764 *
765 * private data must be a valid allocated memory address, or NULL.
766 * Returns 0 if ok, error value on error.
767 * The probe address must at least be aligned on the architecture pointer size.
768 */
769int marker_probe_register(const char *channel, const char *name,
770 const char *format, marker_probe_func *probe,
771 void *probe_private)
772{
773 struct marker_entry *entry;
774 int ret = 0, ret_err;
775 struct marker_probe_closure *old;
776 int first_probe = 0;
777
f7b16408 778 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
779 entry = get_marker(channel, name);
780 if (!entry) {
781 first_probe = 1;
782 entry = add_marker(channel, name, format);
783 if (IS_ERR(entry))
784 ret = PTR_ERR(entry);
785 if (ret)
786 goto end;
787 ret = ltt_channels_register(channel);
788 if (ret)
789 goto error_remove_marker;
790 ret = ltt_channels_get_index_from_name(channel);
791 if (ret < 0)
792 goto error_unregister_channel;
793 entry->channel_id = ret;
794 ret = ltt_channels_get_event_id(channel, name);
795 if (ret < 0)
796 goto error_unregister_channel;
797 entry->event_id = ret;
798 ret = 0;
799 trace_mark(metadata, core_marker_id,
800 "channel %s name %s event_id %hu "
801 "int #1u%zu long #1u%zu pointer #1u%zu "
802 "size_t #1u%zu alignment #1u%u",
803 channel, name, entry->event_id,
804 sizeof(int), sizeof(long), sizeof(void *),
805 sizeof(size_t), ltt_get_alignment());
806 } else if (format) {
807 if (!entry->format)
808 ret = marker_set_format(entry, format);
809 else if (strcmp(entry->format, format))
810 ret = -EPERM;
811 if (ret)
812 goto end;
813 }
814
815 /*
816 * If we detect that a call_rcu is pending for this marker,
817 * make sure it's executed now.
818 */
6cb88bc0 819//ust// if (entry->rcu_pending)
0222e121 820//ust// rcu_cmm_barrier_sched();
68c1021b
PMF
821 old = marker_entry_add_probe(entry, probe, probe_private);
822 if (IS_ERR(old)) {
823 ret = PTR_ERR(old);
824 if (first_probe)
825 goto error_unregister_channel;
826 else
827 goto end;
828 }
f7b16408 829 pthread_mutex_unlock(&markers_mutex);
68c1021b 830
79e36890 831 /* Activate marker if necessary */
68c1021b
PMF
832 marker_update_probes();
833
f7b16408 834 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
835 entry = get_marker(channel, name);
836 if (!entry)
837 goto end;
6cb88bc0 838//ust// if (entry->rcu_pending)
0222e121 839//ust// rcu_cmm_barrier_sched();
68c1021b
PMF
840 entry->oldptr = old;
841 entry->rcu_pending = 1;
842 /* write rcu_pending before calling the RCU callback */
0222e121 843 cmm_smp_wmb();
6cb88bc0
PMF
844//ust// call_rcu_sched(&entry->rcu, free_old_closure);
845 synchronize_rcu(); free_old_closure(&entry->rcu);
68c1021b
PMF
846 goto end;
847
848error_unregister_channel:
849 ret_err = ltt_channels_unregister(channel);
850 WARN_ON(ret_err);
851error_remove_marker:
852 ret_err = remove_marker(channel, name);
853 WARN_ON(ret_err);
854end:
f7b16408 855 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
856 return ret;
857}
59b161cd 858//ust// EXPORT_SYMBOL_GPL(marker_probe_register);
68c1021b
PMF
859
860/**
861 * marker_probe_unregister - Disconnect a probe from a marker
862 * @channel: marker channel
863 * @name: marker name
864 * @probe: probe function pointer
865 * @probe_private: probe private data
866 *
867 * Returns the private data given to marker_probe_register, or an ERR_PTR().
868 * We do not need to call a synchronize_sched to make sure the probes have
869 * finished running before doing a module unload, because the module unload
870 * itself uses stop_machine(), which insures that every preempt disabled section
871 * have finished.
872 */
873int marker_probe_unregister(const char *channel, const char *name,
874 marker_probe_func *probe, void *probe_private)
875{
876 struct marker_entry *entry;
877 struct marker_probe_closure *old;
878 int ret = -ENOENT;
879
f7b16408 880 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
881 entry = get_marker(channel, name);
882 if (!entry)
883 goto end;
6cb88bc0 884//ust// if (entry->rcu_pending)
0222e121 885//ust// rcu_cmm_barrier_sched();
68c1021b 886 old = marker_entry_remove_probe(entry, probe, probe_private);
f7b16408 887 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
888
889 marker_update_probes();
890
f7b16408 891 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
892 entry = get_marker(channel, name);
893 if (!entry)
894 goto end;
6cb88bc0 895//ust// if (entry->rcu_pending)
0222e121 896//ust// rcu_cmm_barrier_sched();
68c1021b
PMF
897 entry->oldptr = old;
898 entry->rcu_pending = 1;
899 /* write rcu_pending before calling the RCU callback */
0222e121 900 cmm_smp_wmb();
6cb88bc0
PMF
901//ust// call_rcu_sched(&entry->rcu, free_old_closure);
902 synchronize_rcu(); free_old_closure(&entry->rcu);
68c1021b
PMF
903 remove_marker(channel, name); /* Ignore busy error message */
904 ret = 0;
905end:
f7b16408 906 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
907 return ret;
908}
59b161cd 909//ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
68c1021b
PMF
910
911static struct marker_entry *
912get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
913{
914 struct marker_entry *entry;
915 unsigned int i;
10c56168
DG
916 struct cds_hlist_head *head;
917 struct cds_hlist_node *node;
68c1021b
PMF
918
919 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
920 head = &marker_table[i];
10c56168 921 cds_hlist_for_each_entry(entry, node, head, hlist) {
68c1021b
PMF
922 if (!entry->ptype) {
923 if (entry->single.func == probe
924 && entry->single.probe_private
925 == probe_private)
926 return entry;
927 } else {
928 struct marker_probe_closure *closure;
929 closure = entry->multi;
930 for (i = 0; closure[i].func; i++) {
931 if (closure[i].func == probe &&
932 closure[i].probe_private
933 == probe_private)
934 return entry;
935 }
936 }
937 }
938 }
939 return NULL;
940}
941
942/**
943 * marker_probe_unregister_private_data - Disconnect a probe from a marker
944 * @probe: probe function
945 * @probe_private: probe private data
946 *
947 * Unregister a probe by providing the registered private data.
948 * Only removes the first marker found in hash table.
949 * Return 0 on success or error value.
950 * We do not need to call a synchronize_sched to make sure the probes have
951 * finished running before doing a module unload, because the module unload
952 * itself uses stop_machine(), which insures that every preempt disabled section
953 * have finished.
954 */
955int marker_probe_unregister_private_data(marker_probe_func *probe,
956 void *probe_private)
957{
958 struct marker_entry *entry;
959 int ret = 0;
960 struct marker_probe_closure *old;
909bc43f 961 char *channel = NULL, *name = NULL;
68c1021b 962
f7b16408 963 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
964 entry = get_marker_from_private_data(probe, probe_private);
965 if (!entry) {
966 ret = -ENOENT;
967 goto end;
968 }
6cb88bc0 969//ust// if (entry->rcu_pending)
0222e121 970//ust// rcu_cmm_barrier_sched();
68c1021b 971 old = marker_entry_remove_probe(entry, NULL, probe_private);
909bc43f
PMF
972 channel = strdup(entry->channel);
973 name = strdup(entry->name);
f7b16408 974 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
975
976 marker_update_probes();
977
f7b16408 978 pthread_mutex_lock(&markers_mutex);
68c1021b
PMF
979 entry = get_marker(channel, name);
980 if (!entry)
981 goto end;
6cb88bc0 982//ust// if (entry->rcu_pending)
0222e121 983//ust// rcu_cmm_barrier_sched();
68c1021b
PMF
984 entry->oldptr = old;
985 entry->rcu_pending = 1;
986 /* write rcu_pending before calling the RCU callback */
0222e121 987 cmm_smp_wmb();
6cb88bc0
PMF
988//ust// call_rcu_sched(&entry->rcu, free_old_closure);
989 synchronize_rcu(); free_old_closure(&entry->rcu);
68c1021b
PMF
990 /* Ignore busy error message */
991 remove_marker(channel, name);
992end:
f7b16408 993 pthread_mutex_unlock(&markers_mutex);
909bc43f
PMF
994 free(channel);
995 free(name);
68c1021b
PMF
996 return ret;
997}
59b161cd 998//ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
68c1021b
PMF
999
1000/**
1001 * marker_get_private_data - Get a marker's probe private data
1002 * @channel: marker channel
1003 * @name: marker name
1004 * @probe: probe to match
1005 * @num: get the nth matching probe's private data
1006 *
1007 * Returns the nth private data pointer (starting from 0) matching, or an
1008 * ERR_PTR.
1009 * Returns the private data pointer, or an ERR_PTR.
1010 * The private data pointer should _only_ be dereferenced if the caller is the
1011 * owner of the data, or its content could vanish. This is mostly used to
1012 * confirm that a caller is the owner of a registered probe.
1013 */
1014void *marker_get_private_data(const char *channel, const char *name,
1015 marker_probe_func *probe, int num)
1016{
10c56168
DG
1017 struct cds_hlist_head *head;
1018 struct cds_hlist_node *node;
68c1021b
PMF
1019 struct marker_entry *e;
1020 size_t channel_len = strlen(channel) + 1;
1021 size_t name_len = strlen(name) + 1;
1022 int i;
1023 u32 hash;
1024
1025 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
1026 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
10c56168 1027 cds_hlist_for_each_entry(e, node, head, hlist) {
68c1021b
PMF
1028 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
1029 if (!e->ptype) {
1030 if (num == 0 && e->single.func == probe)
1031 return e->single.probe_private;
1032 } else {
1033 struct marker_probe_closure *closure;
1034 int match = 0;
1035 closure = e->multi;
1036 for (i = 0; closure[i].func; i++) {
1037 if (closure[i].func != probe)
1038 continue;
1039 if (match++ == num)
1040 return closure[i].probe_private;
1041 }
1042 }
1043 break;
1044 }
1045 }
1046 return ERR_PTR(-ENOENT);
1047}
59b161cd 1048//ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
68c1021b
PMF
1049
1050/**
1051 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1052 *
1053 * Called when no channel users are active by the channel infrastructure.
1054 * Called with lock_markers() and channel mutex held.
1055 */
59b161cd
PMF
1056//ust// void markers_compact_event_ids(void)
1057//ust// {
1058//ust// struct marker_entry *entry;
1059//ust// unsigned int i;
1060//ust// struct hlist_head *head;
1061//ust// struct hlist_node *node;
1062//ust// int ret;
1063//ust//
1064//ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1065//ust// head = &marker_table[i];
1066//ust// hlist_for_each_entry(entry, node, head, hlist) {
1067//ust// ret = ltt_channels_get_index_from_name(entry->channel);
1068//ust// WARN_ON(ret < 0);
1069//ust// entry->channel_id = ret;
1070//ust// ret = _ltt_channels_get_event_id(entry->channel,
1071//ust// entry->name);
1072//ust// WARN_ON(ret < 0);
1073//ust// entry->event_id = ret;
1074//ust// }
1075//ust// }
1076//ust// }
68c1021b 1077
9c67dc50 1078//ust//#ifdef CONFIG_MODULES
68c1021b 1079
772030fe
PMF
1080/*
1081 * Returns 0 if current not found.
1082 * Returns 1 if current found.
1083 */
1084int lib_get_iter_markers(struct marker_iter *iter)
1085{
1086 struct lib *iter_lib;
1087 int found = 0;
1088
f7b16408 1089//ust// pthread_mutex_lock(&module_mutex);
0222e121 1090 cds_list_for_each_entry(iter_lib, &libs, list) {
772030fe
PMF
1091 if (iter_lib < iter->lib)
1092 continue;
1093 else if (iter_lib > iter->lib)
1094 iter->marker = NULL;
1095 found = marker_get_iter_range(&iter->marker,
1096 iter_lib->markers_start,
1097 iter_lib->markers_start + iter_lib->markers_count);
1098 if (found) {
1099 iter->lib = iter_lib;
1100 break;
1101 }
1102 }
f7b16408 1103//ust// pthread_mutex_unlock(&module_mutex);
772030fe
PMF
1104 return found;
1105}
1106
68c1021b
PMF
1107/**
1108 * marker_get_iter_range - Get a next marker iterator given a range.
1109 * @marker: current markers (in), next marker (out)
1110 * @begin: beginning of the range
1111 * @end: end of the range
1112 *
1113 * Returns whether a next marker has been found (1) or not (0).
1114 * Will return the first marker in the range if the input marker is NULL.
1115 */
1116int marker_get_iter_range(struct marker **marker, struct marker *begin,
1117 struct marker *end)
1118{
1119 if (!*marker && begin != end) {
1120 *marker = begin;
1121 return 1;
1122 }
1123 if (*marker >= begin && *marker < end)
1124 return 1;
1125 return 0;
1126}
59b161cd 1127//ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
68c1021b
PMF
1128
1129static void marker_get_iter(struct marker_iter *iter)
1130{
1131 int found = 0;
1132
1133 /* Core kernel markers */
98963de4 1134 if (!iter->lib) {
c463904d 1135 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
68c1021b
PMF
1136 found = marker_get_iter_range(&iter->marker,
1137 __start___markers, __stop___markers);
1138 if (found)
1139 goto end;
1140 }
1141 /* Markers in modules. */
98963de4 1142 found = lib_get_iter_markers(iter);
68c1021b
PMF
1143end:
1144 if (!found)
1145 marker_iter_reset(iter);
1146}
1147
1148void marker_iter_start(struct marker_iter *iter)
1149{
1150 marker_get_iter(iter);
1151}
59b161cd 1152//ust// EXPORT_SYMBOL_GPL(marker_iter_start);
68c1021b
PMF
1153
1154void marker_iter_next(struct marker_iter *iter)
1155{
1156 iter->marker++;
1157 /*
1158 * iter->marker may be invalid because we blindly incremented it.
1159 * Make sure it is valid by marshalling on the markers, getting the
1160 * markers from following modules if necessary.
1161 */
1162 marker_get_iter(iter);
1163}
59b161cd 1164//ust// EXPORT_SYMBOL_GPL(marker_iter_next);
68c1021b
PMF
1165
1166void marker_iter_stop(struct marker_iter *iter)
1167{
1168}
59b161cd 1169//ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
68c1021b
PMF
1170
1171void marker_iter_reset(struct marker_iter *iter)
1172{
98963de4 1173 iter->lib = NULL;
68c1021b
PMF
1174 iter->marker = NULL;
1175}
59b161cd 1176//ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
68c1021b
PMF
1177
1178#ifdef CONFIG_MARKERS_USERSPACE
1179/*
1180 * must be called with current->user_markers_mutex held
1181 */
10c56168 1182static void free_user_marker(char __user *state, struct cds_hlist_head *head)
68c1021b
PMF
1183{
1184 struct user_marker *umark;
10c56168 1185 struct cds_hlist_node *pos, *n;
68c1021b 1186
10c56168 1187 cds_hlist_for_each_entry_safe(umark, pos, n, head, hlist) {
68c1021b 1188 if (umark->state == state) {
10c56168 1189 cds_hlist_del(&umark->hlist);
909bc43f 1190 free(umark);
68c1021b
PMF
1191 }
1192 }
1193}
1194
c1f20530
PMF
1195/*
1196 * Update current process.
1197 * Note that we have to wait a whole scheduler period before we are sure that
1198 * every running userspace threads have their markers updated.
1199 * (synchronize_sched() can be used to insure this).
1200 */
1201//ust// void marker_update_process(void)
59b161cd
PMF
1202//ust// {
1203//ust// struct user_marker *umark;
c1f20530 1204//ust// struct hlist_node *pos;
59b161cd 1205//ust// struct marker_entry *entry;
59b161cd 1206//ust//
f7b16408
PMF
1207//ust// pthread_mutex_lock(&markers_mutex);
1208//ust// pthread_mutex_lock(&current->group_leader->user_markers_mutex);
c1f20530
PMF
1209//ust// if (strcmp(current->comm, "testprog") == 0)
1210//ust// DBG("do update pending for testprog");
1211//ust// hlist_for_each_entry(umark, pos,
1212//ust// &current->group_leader->user_markers, hlist) {
1213//ust// DBG("Updating marker %s in %s", umark->name, current->comm);
59b161cd
PMF
1214//ust// entry = get_marker("userspace", umark->name);
1215//ust// if (entry) {
1216//ust// if (entry->format &&
1217//ust// strcmp(entry->format, umark->format) != 0) {
c1f20530 1218//ust// WARN("error, wrong format in process %s",
59b161cd 1219//ust// current->comm);
c1f20530 1220//ust// break;
59b161cd 1221//ust// }
c1f20530
PMF
1222//ust// if (put_user(!!entry->refcount, umark->state)) {
1223//ust// WARN("Marker in %s caused a fault",
1224//ust// current->comm);
1225//ust// break;
59b161cd 1226//ust// }
59b161cd 1227//ust// } else {
59b161cd 1228//ust// if (put_user(0, umark->state)) {
c1f20530
PMF
1229//ust// WARN("Marker in %s caused a fault", current->comm);
1230//ust// break;
59b161cd
PMF
1231//ust// }
1232//ust// }
59b161cd 1233//ust// }
c1f20530 1234//ust// clear_thread_flag(TIF_MARKER_PENDING);
f7b16408
PMF
1235//ust// pthread_mutex_unlock(&current->group_leader->user_markers_mutex);
1236//ust// pthread_mutex_unlock(&markers_mutex);
59b161cd 1237//ust// }
68c1021b 1238
68c1021b
PMF
1239/*
1240 * Called at process exit and upon do_execve().
1241 * We assume that when the leader exits, no more references can be done to the
1242 * leader structure by the other threads.
1243 */
1244void exit_user_markers(struct task_struct *p)
1245{
1246 struct user_marker *umark;
10c56168 1247 struct cds_hlist_node *pos, *n;
68c1021b
PMF
1248
1249 if (thread_group_leader(p)) {
f7b16408
PMF
1250 pthread_mutex_lock(&markers_mutex);
1251 pthread_mutex_lock(&p->user_markers_mutex);
10c56168 1252 cds_hlist_for_each_entry_safe(umark, pos, n, &p->user_markers,
68c1021b 1253 hlist)
909bc43f 1254 free(umark);
68c1021b
PMF
1255 INIT_HLIST_HEAD(&p->user_markers);
1256 p->user_markers_sequence++;
f7b16408
PMF
1257 pthread_mutex_unlock(&p->user_markers_mutex);
1258 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
1259 }
1260}
1261
1262int is_marker_enabled(const char *channel, const char *name)
1263{
1264 struct marker_entry *entry;
1265
f7b16408 1266 pthread_mutex_lock(&markers_mutex);
68c1021b 1267 entry = get_marker(channel, name);
f7b16408 1268 pthread_mutex_unlock(&markers_mutex);
68c1021b
PMF
1269
1270 return entry && !!entry->refcount;
1271}
9c67dc50 1272//ust// #endif
68c1021b
PMF
1273
1274int marker_module_notify(struct notifier_block *self,
1275 unsigned long val, void *data)
1276{
1277 struct module *mod = data;
1278
1279 switch (val) {
1280 case MODULE_STATE_COMING:
1281 marker_update_probe_range(mod->markers,
1282 mod->markers + mod->num_markers);
1283 break;
1284 case MODULE_STATE_GOING:
1285 marker_update_probe_range(mod->markers,
1286 mod->markers + mod->num_markers);
1287 break;
1288 }
1289 return 0;
1290}
1291
1292struct notifier_block marker_module_nb = {
1293 .notifier_call = marker_module_notify,
1294 .priority = 0,
1295};
1296
59b161cd
PMF
1297//ust// static int init_markers(void)
1298//ust// {
1299//ust// return register_module_notifier(&marker_module_nb);
1300//ust// }
1301//ust// __initcall(init_markers);
1302/* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
68c1021b
PMF
1303
1304#endif /* CONFIG_MODULES */
1305
b73a4c47 1306void ltt_dump_marker_state(struct ust_trace *trace)
9c67dc50 1307{
48454c95 1308 struct marker_entry *entry;
9c67dc50 1309 struct ltt_probe_private_data call_data;
10c56168
DG
1310 struct cds_hlist_head *head;
1311 struct cds_hlist_node *node;
48454c95 1312 unsigned int i;
9c67dc50 1313
f7b16408 1314 pthread_mutex_lock(&markers_mutex);
9c67dc50
PMF
1315 call_data.trace = trace;
1316 call_data.serializer = NULL;
1317
48454c95
PMF
1318 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1319 head = &marker_table[i];
10c56168 1320 cds_hlist_for_each_entry(entry, node, head, hlist) {
48454c95 1321 __trace_mark(0, metadata, core_marker_id,
9c67dc50 1322 &call_data,
48454c95
PMF
1323 "channel %s name %s event_id %hu "
1324 "int #1u%zu long #1u%zu pointer #1u%zu "
1325 "size_t #1u%zu alignment #1u%u",
1326 entry->channel,
1327 entry->name,
1328 entry->event_id,
1329 sizeof(int), sizeof(long),
1330 sizeof(void *), sizeof(size_t),
1331 ltt_get_alignment());
1332 if (entry->format)
1333 __trace_mark(0, metadata,
1334 core_marker_format,
1335 &call_data,
1336 "channel %s name %s format %s",
1337 entry->channel,
1338 entry->name,
1339 entry->format);
1340 }
9c67dc50 1341 }
f7b16408 1342 pthread_mutex_unlock(&markers_mutex);
9c67dc50 1343}
59b161cd 1344//ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
98963de4 1345
20b37a31
PMF
1346static void (*new_marker_cb)(struct marker *) = NULL;
1347
1348void marker_set_new_marker_cb(void (*cb)(struct marker *))
1349{
1350 new_marker_cb = cb;
1351}
1352
1353static void new_markers(struct marker *start, struct marker *end)
1354{
1355 if(new_marker_cb) {
1356 struct marker *m;
1357 for(m=start; m < end; m++) {
1358 new_marker_cb(m);
1359 }
1360 }
1361}
1362
bf961c7e 1363int marker_register_lib(struct marker *markers_start, int markers_count)
98963de4
PMF
1364{
1365 struct lib *pl;
1366
1dba3e6c 1367 pl = (struct lib *) zmalloc(sizeof(struct lib));
98963de4
PMF
1368
1369 pl->markers_start = markers_start;
1370 pl->markers_count = markers_count;
1371
0b5207fa
PMF
1372 /* FIXME: maybe protect this with its own mutex? */
1373 lock_markers();
0222e121 1374 cds_list_add(&pl->list, &libs);
0b5207fa 1375 unlock_markers();
98963de4 1376
20b37a31
PMF
1377 new_markers(markers_start, markers_start + markers_count);
1378
4db647c5
PMF
1379 /* FIXME: update just the loaded lib */
1380 lib_update_markers();
1381
1c184644 1382 DBG("just registered a markers section from %p and having %d markers", markers_start, markers_count);
98963de4
PMF
1383
1384 return 0;
1385}
c463904d 1386
24b6668c 1387int marker_unregister_lib(struct marker *markers_start)
0b5207fa 1388{
24b6668c
PMF
1389 struct lib *lib;
1390
0b5207fa
PMF
1391 /*FIXME: implement; but before implementing, marker_register_lib must
1392 have appropriate locking. */
1393
24b6668c
PMF
1394 lock_markers();
1395
1396 /* FIXME: we should probably take a mutex here on libs */
f7b16408 1397//ust// pthread_mutex_lock(&module_mutex);
0222e121 1398 cds_list_for_each_entry(lib, &libs, list) {
24b6668c
PMF
1399 if(lib->markers_start == markers_start) {
1400 struct lib *lib2free = lib;
0222e121 1401 cds_list_del(&lib->list);
24b6668c
PMF
1402 free(lib2free);
1403 break;
1404 }
1405 }
1406
1407 unlock_markers();
1408
0b5207fa
PMF
1409 return 0;
1410}
1411
4db647c5
PMF
1412static int initialized = 0;
1413
1414void __attribute__((constructor)) init_markers(void)
c463904d 1415{
4db647c5 1416 if(!initialized) {
bf961c7e 1417 marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker));
4db647c5
PMF
1418 initialized = 1;
1419 }
c463904d 1420}
24b6668c
PMF
1421
1422void __attribute__((constructor)) destroy_markers(void)
1423{
1424 marker_unregister_lib(__start___markers);
1425}
This page took 0.09279 seconds and 4 git commands to generate.