| 1 | /* marker-loader.c |
| 2 | * |
| 3 | * Marker Loader |
| 4 | * |
| 5 | * Loads a function at a marker call site. |
| 6 | * |
| 7 | * (C) Copyright 2006 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 8 | * |
| 9 | * This file is released under the GPLv2. |
| 10 | * See the file COPYING for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/marker.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kallsyms.h> |
| 16 | |
| 17 | /* function to install */ |
| 18 | void do_mark1(const char *format, int value) |
| 19 | { |
| 20 | printk("value is %d\n", value); |
| 21 | } |
| 22 | |
| 23 | static void *saved_over; |
| 24 | |
| 25 | static void **target_mark_call; |
| 26 | static void **target_mark_jump_over; |
| 27 | static void **target_mark_jump_call; |
| 28 | static void **target_mark_jump_inline; |
| 29 | |
| 30 | void show_symbol_pointers(void) |
| 31 | { |
| 32 | printk("Marker loader : Loading symbols...\n"); |
| 33 | printk(" %s %p %p\n", __stringify(CALL), target_mark_call, |
| 34 | target_mark_call?*target_mark_call:0x0); |
| 35 | printk(" %s %p %p\n", __stringify(JUMP_OVER), target_mark_jump_over, |
| 36 | target_mark_jump_over?*target_mark_jump_over:0x0); |
| 37 | printk(" %s %p %p\n", __stringify(JUMP_CALL), target_mark_jump_call, |
| 38 | target_mark_jump_call?*target_mark_jump_call:0x0); |
| 39 | printk(" %s %p %p\n", __stringify(JUMP_INLINE), target_mark_jump_inline, |
| 40 | target_mark_jump_inline?*target_mark_jump_inline:0x0); |
| 41 | } |
| 42 | |
| 43 | int mark_install_hook(void) |
| 44 | { |
| 45 | target_mark_call = (void**)kallsyms_lookup_name(__stringify(CALL)); |
| 46 | target_mark_jump_over = (void**)kallsyms_lookup_name(__stringify(JUMP_OVER)); |
| 47 | target_mark_jump_call = (void**)kallsyms_lookup_name(__stringify(JUMP_CALL)); |
| 48 | target_mark_jump_inline = (void**)kallsyms_lookup_name(__stringify(JUMP_INLINE)); |
| 49 | |
| 50 | show_symbol_pointers(); |
| 51 | |
| 52 | if(!(target_mark_call && target_mark_jump_over && target_mark_jump_call && |
| 53 | target_mark_jump_inline)) { |
| 54 | printk("Target symbols missing in kallsyms.\n"); |
| 55 | return -EPERM; |
| 56 | } |
| 57 | |
| 58 | printk("Installing hook\n"); |
| 59 | *target_mark_call = (void*)do_mark1; |
| 60 | saved_over = *target_mark_jump_over; |
| 61 | *target_mark_jump_over = *target_mark_jump_call; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | int init_module(void) |
| 67 | { |
| 68 | return mark_install_hook(); |
| 69 | } |
| 70 | |
| 71 | void cleanup_module(void) |
| 72 | { |
| 73 | printk("Removing hook\n"); |
| 74 | *target_mark_jump_over = saved_over; |
| 75 | *target_mark_call = __mark_empty_function; |
| 76 | |
| 77 | /* Wait for instrumentation functions to return before quitting */ |
| 78 | synchronize_sched(); |
| 79 | } |
| 80 | |
| 81 | MODULE_LICENSE("GPL"); |
| 82 | MODULE_AUTHOR("Mathieu Desnoyers"); |
| 83 | MODULE_DESCRIPTION("Marker Loader"); |
| 84 | |