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