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