1 /* 2 * ring buffer based initcalls tracer 3 * 4 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com> 5 * 6 */ 7 8 #include <linux/init.h> 9 #include <linux/debugfs.h> 10 #include <linux/ftrace.h> 11 #include <linux/kallsyms.h> 12 13 #include "trace.h" 14 #include "trace_output.h" 15 16 static struct trace_array *boot_trace; 17 static bool pre_initcalls_finished; 18 19 /* Tells the boot tracer that the pre_smp_initcalls are finished. 20 * So we are ready . 21 * It doesn't enable sched events tracing however. 22 * You have to call enable_boot_trace to do so. 23 */ 24 void start_boot_trace(void) 25 { 26 pre_initcalls_finished = true; 27 } 28 29 void enable_boot_trace(void) 30 { 31 if (boot_trace && pre_initcalls_finished) 32 tracing_start_sched_switch_record(); 33 } 34 35 void disable_boot_trace(void) 36 { 37 if (boot_trace && pre_initcalls_finished) 38 tracing_stop_sched_switch_record(); 39 } 40 41 static int boot_trace_init(struct trace_array *tr) 42 { 43 int cpu; 44 boot_trace = tr; 45 46 if (!tr) 47 return 0; 48 49 for_each_cpu(cpu, cpu_possible_mask) 50 tracing_reset(tr, cpu); 51 52 tracing_sched_switch_assign_trace(tr); 53 return 0; 54 } 55 56 static enum print_line_t 57 initcall_call_print_line(struct trace_iterator *iter) 58 { 59 struct trace_entry *entry = iter->ent; 60 struct trace_seq *s = &iter->seq; 61 struct trace_boot_call *field; 62 struct boot_trace_call *call; 63 u64 ts; 64 unsigned long nsec_rem; 65 int ret; 66 67 trace_assign_type(field, entry); 68 call = &field->boot_call; 69 ts = iter->ts; 70 nsec_rem = do_div(ts, 1000000000); 71 72 ret = trace_seq_printf(s, "[%5ld.%09ld] calling %s @ %i\n", 73 (unsigned long)ts, nsec_rem, call->func, call->caller); 74 75 if (!ret) 76 return TRACE_TYPE_PARTIAL_LINE; 77 else 78 return TRACE_TYPE_HANDLED; 79 } 80 81 static enum print_line_t 82 initcall_ret_print_line(struct trace_iterator *iter) 83 { 84 struct trace_entry *entry = iter->ent; 85 struct trace_seq *s = &iter->seq; 86 struct trace_boot_ret *field; 87 struct boot_trace_ret *init_ret; 88 u64 ts; 89 unsigned long nsec_rem; 90 int ret; 91 92 trace_assign_type(field, entry); 93 init_ret = &field->boot_ret; 94 ts = iter->ts; 95 nsec_rem = do_div(ts, 1000000000); 96 97 ret = trace_seq_printf(s, "[%5ld.%09ld] initcall %s " 98 "returned %d after %llu msecs\n", 99 (unsigned long) ts, 100 nsec_rem, 101 init_ret->func, init_ret->result, init_ret->duration); 102 103 if (!ret) 104 return TRACE_TYPE_PARTIAL_LINE; 105 else 106 return TRACE_TYPE_HANDLED; 107 } 108 109 static enum print_line_t initcall_print_line(struct trace_iterator *iter) 110 { 111 struct trace_entry *entry = iter->ent; 112 113 switch (entry->type) { 114 case TRACE_BOOT_CALL: 115 return initcall_call_print_line(iter); 116 case TRACE_BOOT_RET: 117 return initcall_ret_print_line(iter); 118 default: 119 return TRACE_TYPE_UNHANDLED; 120 } 121 } 122 123 struct tracer boot_tracer __read_mostly = 124 { 125 .name = "initcall", 126 .init = boot_trace_init, 127 .reset = tracing_reset_online_cpus, 128 .print_line = initcall_print_line, 129 }; 130 131 void trace_boot_call(struct boot_trace_call *bt, initcall_t fn) 132 { 133 struct ring_buffer_event *event; 134 struct trace_boot_call *entry; 135 struct trace_array *tr = boot_trace; 136 137 if (!tr || !pre_initcalls_finished) 138 return; 139 140 /* Get its name now since this function could 141 * disappear because it is in the .init section. 142 */ 143 sprint_symbol(bt->func, (unsigned long)fn); 144 preempt_disable(); 145 146 event = trace_buffer_lock_reserve(tr, TRACE_BOOT_CALL, 147 sizeof(*entry), 0, 0); 148 if (!event) 149 goto out; 150 entry = ring_buffer_event_data(event); 151 entry->boot_call = *bt; 152 trace_buffer_unlock_commit(tr, event, 0, 0); 153 out: 154 preempt_enable(); 155 } 156 157 void trace_boot_ret(struct boot_trace_ret *bt, initcall_t fn) 158 { 159 struct ring_buffer_event *event; 160 struct trace_boot_ret *entry; 161 struct trace_array *tr = boot_trace; 162 163 if (!tr || !pre_initcalls_finished) 164 return; 165 166 sprint_symbol(bt->func, (unsigned long)fn); 167 preempt_disable(); 168 169 event = trace_buffer_lock_reserve(tr, TRACE_BOOT_RET, 170 sizeof(*entry), 0, 0); 171 if (!event) 172 goto out; 173 entry = ring_buffer_event_data(event); 174 entry->boot_ret = *bt; 175 trace_buffer_unlock_commit(tr, event, 0, 0); 176 out: 177 preempt_enable(); 178 } 179