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