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