trace_functions_graph.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. *
  3. * Function graph tracer.
  4. * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
  7. *
  8. */
  9. #include <linux/uaccess.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/slab.h>
  12. #include <linux/fs.h>
  13. #include "trace.h"
  14. #include "trace_output.h"
  15. static bool kill_ftrace_graph;
  16. /**
  17. * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
  18. *
  19. * ftrace_graph_stop() is called when a severe error is detected in
  20. * the function graph tracing. This function is called by the critical
  21. * paths of function graph to keep those paths from doing any more harm.
  22. */
  23. bool ftrace_graph_is_dead(void)
  24. {
  25. return kill_ftrace_graph;
  26. }
  27. /**
  28. * ftrace_graph_stop - set to permanently disable function graph tracincg
  29. *
  30. * In case of an error int function graph tracing, this is called
  31. * to try to keep function graph tracing from causing any more harm.
  32. * Usually this is pretty severe and this is called to try to at least
  33. * get a warning out to the user.
  34. */
  35. void ftrace_graph_stop(void)
  36. {
  37. kill_ftrace_graph = true;
  38. }
  39. /* When set, irq functions will be ignored */
  40. static int ftrace_graph_skip_irqs;
  41. struct fgraph_cpu_data {
  42. pid_t last_pid;
  43. int depth;
  44. int depth_irq;
  45. int ignore;
  46. unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
  47. };
  48. struct fgraph_data {
  49. struct fgraph_cpu_data __percpu *cpu_data;
  50. /* Place to preserve last processed entry. */
  51. struct ftrace_graph_ent_entry ent;
  52. struct ftrace_graph_ret_entry ret;
  53. int failed;
  54. int cpu;
  55. };
  56. #define TRACE_GRAPH_INDENT 2
  57. static unsigned int max_depth;
  58. static struct tracer_opt trace_opts[] = {
  59. /* Display overruns? (for self-debug purpose) */
  60. { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
  61. /* Display CPU ? */
  62. { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
  63. /* Display Overhead ? */
  64. { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
  65. /* Display proc name/pid */
  66. { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
  67. /* Display duration of execution */
  68. { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
  69. /* Display absolute time of an entry */
  70. { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
  71. /* Display interrupts */
  72. { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
  73. /* Display function name after trailing } */
  74. { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
  75. { } /* Empty entry */
  76. };
  77. static struct tracer_flags tracer_flags = {
  78. /* Don't display overruns, proc, or tail by default */
  79. .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
  80. TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS,
  81. .opts = trace_opts
  82. };
  83. static struct trace_array *graph_array;
  84. /*
  85. * DURATION column is being also used to display IRQ signs,
  86. * following values are used by print_graph_irq and others
  87. * to fill in space into DURATION column.
  88. */
  89. enum {
  90. FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  91. FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  92. FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  93. };
  94. static void
  95. print_graph_duration(unsigned long long duration, struct trace_seq *s,
  96. u32 flags);
  97. /* Add a function return address to the trace stack on thread info.*/
  98. int
  99. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  100. unsigned long frame_pointer)
  101. {
  102. unsigned long long calltime;
  103. int index;
  104. if (unlikely(ftrace_graph_is_dead()))
  105. return -EBUSY;
  106. if (!current->ret_stack)
  107. return -EBUSY;
  108. /*
  109. * We must make sure the ret_stack is tested before we read
  110. * anything else.
  111. */
  112. smp_rmb();
  113. /* The return trace stack is full */
  114. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  115. atomic_inc(&current->trace_overrun);
  116. return -EBUSY;
  117. }
  118. /*
  119. * The curr_ret_stack is an index to ftrace return stack of
  120. * current task. Its value should be in [0, FTRACE_RETFUNC_
  121. * DEPTH) when the function graph tracer is used. To support
  122. * filtering out specific functions, it makes the index
  123. * negative by subtracting huge value (FTRACE_NOTRACE_DEPTH)
  124. * so when it sees a negative index the ftrace will ignore
  125. * the record. And the index gets recovered when returning
  126. * from the filtered function by adding the FTRACE_NOTRACE_
  127. * DEPTH and then it'll continue to record functions normally.
  128. *
  129. * The curr_ret_stack is initialized to -1 and get increased
  130. * in this function. So it can be less than -1 only if it was
  131. * filtered out via ftrace_graph_notrace_addr() which can be
  132. * set from set_graph_notrace file in tracefs by user.
  133. */
  134. if (current->curr_ret_stack < -1)
  135. return -EBUSY;
  136. calltime = trace_clock_local();
  137. index = ++current->curr_ret_stack;
  138. if (ftrace_graph_notrace_addr(func))
  139. current->curr_ret_stack -= FTRACE_NOTRACE_DEPTH;
  140. barrier();
  141. current->ret_stack[index].ret = ret;
  142. current->ret_stack[index].func = func;
  143. current->ret_stack[index].calltime = calltime;
  144. current->ret_stack[index].subtime = 0;
  145. current->ret_stack[index].fp = frame_pointer;
  146. *depth = current->curr_ret_stack;
  147. return 0;
  148. }
  149. /* Retrieve a function return address to the trace stack on thread info.*/
  150. static void
  151. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  152. unsigned long frame_pointer)
  153. {
  154. int index;
  155. index = current->curr_ret_stack;
  156. /*
  157. * A negative index here means that it's just returned from a
  158. * notrace'd function. Recover index to get an original
  159. * return address. See ftrace_push_return_trace().
  160. *
  161. * TODO: Need to check whether the stack gets corrupted.
  162. */
  163. if (index < 0)
  164. index += FTRACE_NOTRACE_DEPTH;
  165. if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
  166. ftrace_graph_stop();
  167. WARN_ON(1);
  168. /* Might as well panic, otherwise we have no where to go */
  169. *ret = (unsigned long)panic;
  170. return;
  171. }
  172. #if defined(CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST) && !defined(CC_USING_FENTRY)
  173. /*
  174. * The arch may choose to record the frame pointer used
  175. * and check it here to make sure that it is what we expect it
  176. * to be. If gcc does not set the place holder of the return
  177. * address in the frame pointer, and does a copy instead, then
  178. * the function graph trace will fail. This test detects this
  179. * case.
  180. *
  181. * Currently, x86_32 with optimize for size (-Os) makes the latest
  182. * gcc do the above.
  183. *
  184. * Note, -mfentry does not use frame pointers, and this test
  185. * is not needed if CC_USING_FENTRY is set.
  186. */
  187. if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
  188. ftrace_graph_stop();
  189. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  190. " from func %ps return to %lx\n",
  191. current->ret_stack[index].fp,
  192. frame_pointer,
  193. (void *)current->ret_stack[index].func,
  194. current->ret_stack[index].ret);
  195. *ret = (unsigned long)panic;
  196. return;
  197. }
  198. #endif
  199. *ret = current->ret_stack[index].ret;
  200. trace->func = current->ret_stack[index].func;
  201. trace->calltime = current->ret_stack[index].calltime;
  202. trace->overrun = atomic_read(&current->trace_overrun);
  203. trace->depth = index;
  204. }
  205. /*
  206. * Send the trace to the ring-buffer.
  207. * @return the original return address.
  208. */
  209. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  210. {
  211. struct ftrace_graph_ret trace;
  212. unsigned long ret;
  213. ftrace_pop_return_trace(&trace, &ret, frame_pointer);
  214. trace.rettime = trace_clock_local();
  215. barrier();
  216. current->curr_ret_stack--;
  217. /*
  218. * The curr_ret_stack can be less than -1 only if it was
  219. * filtered out and it's about to return from the function.
  220. * Recover the index and continue to trace normal functions.
  221. */
  222. if (current->curr_ret_stack < -1) {
  223. current->curr_ret_stack += FTRACE_NOTRACE_DEPTH;
  224. return ret;
  225. }
  226. /*
  227. * The trace should run after decrementing the ret counter
  228. * in case an interrupt were to come in. We don't want to
  229. * lose the interrupt if max_depth is set.
  230. */
  231. ftrace_graph_return(&trace);
  232. if (unlikely(!ret)) {
  233. ftrace_graph_stop();
  234. WARN_ON(1);
  235. /* Might as well panic. What else to do? */
  236. ret = (unsigned long)panic;
  237. }
  238. return ret;
  239. }
  240. int __trace_graph_entry(struct trace_array *tr,
  241. struct ftrace_graph_ent *trace,
  242. unsigned long flags,
  243. int pc)
  244. {
  245. struct trace_event_call *call = &event_funcgraph_entry;
  246. struct ring_buffer_event *event;
  247. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  248. struct ftrace_graph_ent_entry *entry;
  249. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  250. return 0;
  251. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
  252. sizeof(*entry), flags, pc);
  253. if (!event)
  254. return 0;
  255. entry = ring_buffer_event_data(event);
  256. entry->graph_ent = *trace;
  257. if (!call_filter_check_discard(call, entry, buffer, event))
  258. __buffer_unlock_commit(buffer, event);
  259. return 1;
  260. }
  261. static inline int ftrace_graph_ignore_irqs(void)
  262. {
  263. if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
  264. return 0;
  265. return in_irq();
  266. }
  267. int trace_graph_entry(struct ftrace_graph_ent *trace)
  268. {
  269. struct trace_array *tr = graph_array;
  270. struct trace_array_cpu *data;
  271. unsigned long flags;
  272. long disabled;
  273. int ret;
  274. int cpu;
  275. int pc;
  276. if (!ftrace_trace_task(current))
  277. return 0;
  278. /* trace it when it is-nested-in or is a function enabled. */
  279. if ((!(trace->depth || ftrace_graph_addr(trace->func)) ||
  280. ftrace_graph_ignore_irqs()) || (trace->depth < 0) ||
  281. (max_depth && trace->depth >= max_depth))
  282. return 0;
  283. /*
  284. * Do not trace a function if it's filtered by set_graph_notrace.
  285. * Make the index of ret stack negative to indicate that it should
  286. * ignore further functions. But it needs its own ret stack entry
  287. * to recover the original index in order to continue tracing after
  288. * returning from the function.
  289. */
  290. if (ftrace_graph_notrace_addr(trace->func))
  291. return 1;
  292. local_irq_save(flags);
  293. cpu = raw_smp_processor_id();
  294. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  295. disabled = atomic_inc_return(&data->disabled);
  296. if (likely(disabled == 1)) {
  297. pc = preempt_count();
  298. ret = __trace_graph_entry(tr, trace, flags, pc);
  299. } else {
  300. ret = 0;
  301. }
  302. atomic_dec(&data->disabled);
  303. local_irq_restore(flags);
  304. return ret;
  305. }
  306. static int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
  307. {
  308. if (tracing_thresh)
  309. return 1;
  310. else
  311. return trace_graph_entry(trace);
  312. }
  313. static void
  314. __trace_graph_function(struct trace_array *tr,
  315. unsigned long ip, unsigned long flags, int pc)
  316. {
  317. u64 time = trace_clock_local();
  318. struct ftrace_graph_ent ent = {
  319. .func = ip,
  320. .depth = 0,
  321. };
  322. struct ftrace_graph_ret ret = {
  323. .func = ip,
  324. .depth = 0,
  325. .calltime = time,
  326. .rettime = time,
  327. };
  328. __trace_graph_entry(tr, &ent, flags, pc);
  329. __trace_graph_return(tr, &ret, flags, pc);
  330. }
  331. void
  332. trace_graph_function(struct trace_array *tr,
  333. unsigned long ip, unsigned long parent_ip,
  334. unsigned long flags, int pc)
  335. {
  336. __trace_graph_function(tr, ip, flags, pc);
  337. }
  338. void __trace_graph_return(struct trace_array *tr,
  339. struct ftrace_graph_ret *trace,
  340. unsigned long flags,
  341. int pc)
  342. {
  343. struct trace_event_call *call = &event_funcgraph_exit;
  344. struct ring_buffer_event *event;
  345. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  346. struct ftrace_graph_ret_entry *entry;
  347. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  348. return;
  349. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
  350. sizeof(*entry), flags, pc);
  351. if (!event)
  352. return;
  353. entry = ring_buffer_event_data(event);
  354. entry->ret = *trace;
  355. if (!call_filter_check_discard(call, entry, buffer, event))
  356. __buffer_unlock_commit(buffer, event);
  357. }
  358. void trace_graph_return(struct ftrace_graph_ret *trace)
  359. {
  360. struct trace_array *tr = graph_array;
  361. struct trace_array_cpu *data;
  362. unsigned long flags;
  363. long disabled;
  364. int cpu;
  365. int pc;
  366. local_irq_save(flags);
  367. cpu = raw_smp_processor_id();
  368. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  369. disabled = atomic_inc_return(&data->disabled);
  370. if (likely(disabled == 1)) {
  371. pc = preempt_count();
  372. __trace_graph_return(tr, trace, flags, pc);
  373. }
  374. atomic_dec(&data->disabled);
  375. local_irq_restore(flags);
  376. }
  377. void set_graph_array(struct trace_array *tr)
  378. {
  379. graph_array = tr;
  380. /* Make graph_array visible before we start tracing */
  381. smp_mb();
  382. }
  383. static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
  384. {
  385. if (tracing_thresh &&
  386. (trace->rettime - trace->calltime < tracing_thresh))
  387. return;
  388. else
  389. trace_graph_return(trace);
  390. }
  391. static int graph_trace_init(struct trace_array *tr)
  392. {
  393. int ret;
  394. set_graph_array(tr);
  395. if (tracing_thresh)
  396. ret = register_ftrace_graph(&trace_graph_thresh_return,
  397. &trace_graph_thresh_entry);
  398. else
  399. ret = register_ftrace_graph(&trace_graph_return,
  400. &trace_graph_entry);
  401. if (ret)
  402. return ret;
  403. tracing_start_cmdline_record();
  404. return 0;
  405. }
  406. static void graph_trace_reset(struct trace_array *tr)
  407. {
  408. tracing_stop_cmdline_record();
  409. unregister_ftrace_graph();
  410. }
  411. static int graph_trace_update_thresh(struct trace_array *tr)
  412. {
  413. graph_trace_reset(tr);
  414. return graph_trace_init(tr);
  415. }
  416. static int max_bytes_for_cpu;
  417. static void print_graph_cpu(struct trace_seq *s, int cpu)
  418. {
  419. /*
  420. * Start with a space character - to make it stand out
  421. * to the right a bit when trace output is pasted into
  422. * email:
  423. */
  424. trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
  425. }
  426. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  427. static void print_graph_proc(struct trace_seq *s, pid_t pid)
  428. {
  429. char comm[TASK_COMM_LEN];
  430. /* sign + log10(MAX_INT) + '\0' */
  431. char pid_str[11];
  432. int spaces = 0;
  433. int len;
  434. int i;
  435. trace_find_cmdline(pid, comm);
  436. comm[7] = '\0';
  437. sprintf(pid_str, "%d", pid);
  438. /* 1 stands for the "-" character */
  439. len = strlen(comm) + strlen(pid_str) + 1;
  440. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  441. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  442. /* First spaces to align center */
  443. for (i = 0; i < spaces / 2; i++)
  444. trace_seq_putc(s, ' ');
  445. trace_seq_printf(s, "%s-%s", comm, pid_str);
  446. /* Last spaces to align center */
  447. for (i = 0; i < spaces - (spaces / 2); i++)
  448. trace_seq_putc(s, ' ');
  449. }
  450. static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  451. {
  452. trace_seq_putc(s, ' ');
  453. trace_print_lat_fmt(s, entry);
  454. }
  455. /* If the pid changed since the last trace, output this event */
  456. static void
  457. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  458. {
  459. pid_t prev_pid;
  460. pid_t *last_pid;
  461. if (!data)
  462. return;
  463. last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  464. if (*last_pid == pid)
  465. return;
  466. prev_pid = *last_pid;
  467. *last_pid = pid;
  468. if (prev_pid == -1)
  469. return;
  470. /*
  471. * Context-switch trace line:
  472. ------------------------------------------
  473. | 1) migration/0--1 => sshd-1755
  474. ------------------------------------------
  475. */
  476. trace_seq_puts(s, " ------------------------------------------\n");
  477. print_graph_cpu(s, cpu);
  478. print_graph_proc(s, prev_pid);
  479. trace_seq_puts(s, " => ");
  480. print_graph_proc(s, pid);
  481. trace_seq_puts(s, "\n ------------------------------------------\n\n");
  482. }
  483. static struct ftrace_graph_ret_entry *
  484. get_return_for_leaf(struct trace_iterator *iter,
  485. struct ftrace_graph_ent_entry *curr)
  486. {
  487. struct fgraph_data *data = iter->private;
  488. struct ring_buffer_iter *ring_iter = NULL;
  489. struct ring_buffer_event *event;
  490. struct ftrace_graph_ret_entry *next;
  491. /*
  492. * If the previous output failed to write to the seq buffer,
  493. * then we just reuse the data from before.
  494. */
  495. if (data && data->failed) {
  496. curr = &data->ent;
  497. next = &data->ret;
  498. } else {
  499. ring_iter = trace_buffer_iter(iter, iter->cpu);
  500. /* First peek to compare current entry and the next one */
  501. if (ring_iter)
  502. event = ring_buffer_iter_peek(ring_iter, NULL);
  503. else {
  504. /*
  505. * We need to consume the current entry to see
  506. * the next one.
  507. */
  508. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
  509. NULL, NULL);
  510. event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
  511. NULL, NULL);
  512. }
  513. if (!event)
  514. return NULL;
  515. next = ring_buffer_event_data(event);
  516. if (data) {
  517. /*
  518. * Save current and next entries for later reference
  519. * if the output fails.
  520. */
  521. data->ent = *curr;
  522. /*
  523. * If the next event is not a return type, then
  524. * we only care about what type it is. Otherwise we can
  525. * safely copy the entire event.
  526. */
  527. if (next->ent.type == TRACE_GRAPH_RET)
  528. data->ret = *next;
  529. else
  530. data->ret.ent.type = next->ent.type;
  531. }
  532. }
  533. if (next->ent.type != TRACE_GRAPH_RET)
  534. return NULL;
  535. if (curr->ent.pid != next->ent.pid ||
  536. curr->graph_ent.func != next->ret.func)
  537. return NULL;
  538. /* this is a leaf, now advance the iterator */
  539. if (ring_iter)
  540. ring_buffer_read(ring_iter, NULL);
  541. return next;
  542. }
  543. static void print_graph_abs_time(u64 t, struct trace_seq *s)
  544. {
  545. unsigned long usecs_rem;
  546. usecs_rem = do_div(t, NSEC_PER_SEC);
  547. usecs_rem /= 1000;
  548. trace_seq_printf(s, "%5lu.%06lu | ",
  549. (unsigned long)t, usecs_rem);
  550. }
  551. static void
  552. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  553. enum trace_type type, int cpu, pid_t pid, u32 flags)
  554. {
  555. struct trace_seq *s = &iter->seq;
  556. struct trace_entry *ent = iter->ent;
  557. if (addr < (unsigned long)__irqentry_text_start ||
  558. addr >= (unsigned long)__irqentry_text_end)
  559. return;
  560. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  561. /* Absolute time */
  562. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  563. print_graph_abs_time(iter->ts, s);
  564. /* Cpu */
  565. if (flags & TRACE_GRAPH_PRINT_CPU)
  566. print_graph_cpu(s, cpu);
  567. /* Proc */
  568. if (flags & TRACE_GRAPH_PRINT_PROC) {
  569. print_graph_proc(s, pid);
  570. trace_seq_puts(s, " | ");
  571. }
  572. /* Latency format */
  573. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  574. print_graph_lat_fmt(s, ent);
  575. }
  576. /* No overhead */
  577. print_graph_duration(0, s, flags | FLAGS_FILL_START);
  578. if (type == TRACE_GRAPH_ENT)
  579. trace_seq_puts(s, "==========>");
  580. else
  581. trace_seq_puts(s, "<==========");
  582. print_graph_duration(0, s, flags | FLAGS_FILL_END);
  583. trace_seq_putc(s, '\n');
  584. }
  585. void
  586. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  587. {
  588. unsigned long nsecs_rem = do_div(duration, 1000);
  589. /* log10(ULONG_MAX) + '\0' */
  590. char usecs_str[21];
  591. char nsecs_str[5];
  592. int len;
  593. int i;
  594. sprintf(usecs_str, "%lu", (unsigned long) duration);
  595. /* Print msecs */
  596. trace_seq_printf(s, "%s", usecs_str);
  597. len = strlen(usecs_str);
  598. /* Print nsecs (we don't want to exceed 7 numbers) */
  599. if (len < 7) {
  600. size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
  601. snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
  602. trace_seq_printf(s, ".%s", nsecs_str);
  603. len += strlen(nsecs_str);
  604. }
  605. trace_seq_puts(s, " us ");
  606. /* Print remaining spaces to fit the row's width */
  607. for (i = len; i < 7; i++)
  608. trace_seq_putc(s, ' ');
  609. }
  610. static void
  611. print_graph_duration(unsigned long long duration, struct trace_seq *s,
  612. u32 flags)
  613. {
  614. if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
  615. !(trace_flags & TRACE_ITER_CONTEXT_INFO))
  616. return;
  617. /* No real adata, just filling the column with spaces */
  618. switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
  619. case FLAGS_FILL_FULL:
  620. trace_seq_puts(s, " | ");
  621. return;
  622. case FLAGS_FILL_START:
  623. trace_seq_puts(s, " ");
  624. return;
  625. case FLAGS_FILL_END:
  626. trace_seq_puts(s, " |");
  627. return;
  628. }
  629. /* Signal a overhead of time execution to the output */
  630. if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
  631. trace_seq_printf(s, "%c ", trace_find_mark(duration));
  632. else
  633. trace_seq_puts(s, " ");
  634. trace_print_graph_duration(duration, s);
  635. trace_seq_puts(s, "| ");
  636. }
  637. /* Case of a leaf function on its call entry */
  638. static enum print_line_t
  639. print_graph_entry_leaf(struct trace_iterator *iter,
  640. struct ftrace_graph_ent_entry *entry,
  641. struct ftrace_graph_ret_entry *ret_entry,
  642. struct trace_seq *s, u32 flags)
  643. {
  644. struct fgraph_data *data = iter->private;
  645. struct ftrace_graph_ret *graph_ret;
  646. struct ftrace_graph_ent *call;
  647. unsigned long long duration;
  648. int i;
  649. graph_ret = &ret_entry->ret;
  650. call = &entry->graph_ent;
  651. duration = graph_ret->rettime - graph_ret->calltime;
  652. if (data) {
  653. struct fgraph_cpu_data *cpu_data;
  654. int cpu = iter->cpu;
  655. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  656. /*
  657. * Comments display at + 1 to depth. Since
  658. * this is a leaf function, keep the comments
  659. * equal to this depth.
  660. */
  661. cpu_data->depth = call->depth - 1;
  662. /* No need to keep this function around for this depth */
  663. if (call->depth < FTRACE_RETFUNC_DEPTH)
  664. cpu_data->enter_funcs[call->depth] = 0;
  665. }
  666. /* Overhead and duration */
  667. print_graph_duration(duration, s, flags);
  668. /* Function */
  669. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  670. trace_seq_putc(s, ' ');
  671. trace_seq_printf(s, "%ps();\n", (void *)call->func);
  672. return trace_handle_return(s);
  673. }
  674. static enum print_line_t
  675. print_graph_entry_nested(struct trace_iterator *iter,
  676. struct ftrace_graph_ent_entry *entry,
  677. struct trace_seq *s, int cpu, u32 flags)
  678. {
  679. struct ftrace_graph_ent *call = &entry->graph_ent;
  680. struct fgraph_data *data = iter->private;
  681. int i;
  682. if (data) {
  683. struct fgraph_cpu_data *cpu_data;
  684. int cpu = iter->cpu;
  685. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  686. cpu_data->depth = call->depth;
  687. /* Save this function pointer to see if the exit matches */
  688. if (call->depth < FTRACE_RETFUNC_DEPTH)
  689. cpu_data->enter_funcs[call->depth] = call->func;
  690. }
  691. /* No time */
  692. print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
  693. /* Function */
  694. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  695. trace_seq_putc(s, ' ');
  696. trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  697. if (trace_seq_has_overflowed(s))
  698. return TRACE_TYPE_PARTIAL_LINE;
  699. /*
  700. * we already consumed the current entry to check the next one
  701. * and see if this is a leaf.
  702. */
  703. return TRACE_TYPE_NO_CONSUME;
  704. }
  705. static void
  706. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  707. int type, unsigned long addr, u32 flags)
  708. {
  709. struct fgraph_data *data = iter->private;
  710. struct trace_entry *ent = iter->ent;
  711. int cpu = iter->cpu;
  712. /* Pid */
  713. verif_pid(s, ent->pid, cpu, data);
  714. if (type)
  715. /* Interrupt */
  716. print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
  717. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  718. return;
  719. /* Absolute time */
  720. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  721. print_graph_abs_time(iter->ts, s);
  722. /* Cpu */
  723. if (flags & TRACE_GRAPH_PRINT_CPU)
  724. print_graph_cpu(s, cpu);
  725. /* Proc */
  726. if (flags & TRACE_GRAPH_PRINT_PROC) {
  727. print_graph_proc(s, ent->pid);
  728. trace_seq_puts(s, " | ");
  729. }
  730. /* Latency format */
  731. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  732. print_graph_lat_fmt(s, ent);
  733. return;
  734. }
  735. /*
  736. * Entry check for irq code
  737. *
  738. * returns 1 if
  739. * - we are inside irq code
  740. * - we just entered irq code
  741. *
  742. * retunns 0 if
  743. * - funcgraph-interrupts option is set
  744. * - we are not inside irq code
  745. */
  746. static int
  747. check_irq_entry(struct trace_iterator *iter, u32 flags,
  748. unsigned long addr, int depth)
  749. {
  750. int cpu = iter->cpu;
  751. int *depth_irq;
  752. struct fgraph_data *data = iter->private;
  753. /*
  754. * If we are either displaying irqs, or we got called as
  755. * a graph event and private data does not exist,
  756. * then we bypass the irq check.
  757. */
  758. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  759. (!data))
  760. return 0;
  761. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  762. /*
  763. * We are inside the irq code
  764. */
  765. if (*depth_irq >= 0)
  766. return 1;
  767. if ((addr < (unsigned long)__irqentry_text_start) ||
  768. (addr >= (unsigned long)__irqentry_text_end))
  769. return 0;
  770. /*
  771. * We are entering irq code.
  772. */
  773. *depth_irq = depth;
  774. return 1;
  775. }
  776. /*
  777. * Return check for irq code
  778. *
  779. * returns 1 if
  780. * - we are inside irq code
  781. * - we just left irq code
  782. *
  783. * returns 0 if
  784. * - funcgraph-interrupts option is set
  785. * - we are not inside irq code
  786. */
  787. static int
  788. check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
  789. {
  790. int cpu = iter->cpu;
  791. int *depth_irq;
  792. struct fgraph_data *data = iter->private;
  793. /*
  794. * If we are either displaying irqs, or we got called as
  795. * a graph event and private data does not exist,
  796. * then we bypass the irq check.
  797. */
  798. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  799. (!data))
  800. return 0;
  801. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  802. /*
  803. * We are not inside the irq code.
  804. */
  805. if (*depth_irq == -1)
  806. return 0;
  807. /*
  808. * We are inside the irq code, and this is returning entry.
  809. * Let's not trace it and clear the entry depth, since
  810. * we are out of irq code.
  811. *
  812. * This condition ensures that we 'leave the irq code' once
  813. * we are out of the entry depth. Thus protecting us from
  814. * the RETURN entry loss.
  815. */
  816. if (*depth_irq >= depth) {
  817. *depth_irq = -1;
  818. return 1;
  819. }
  820. /*
  821. * We are inside the irq code, and this is not the entry.
  822. */
  823. return 1;
  824. }
  825. static enum print_line_t
  826. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  827. struct trace_iterator *iter, u32 flags)
  828. {
  829. struct fgraph_data *data = iter->private;
  830. struct ftrace_graph_ent *call = &field->graph_ent;
  831. struct ftrace_graph_ret_entry *leaf_ret;
  832. static enum print_line_t ret;
  833. int cpu = iter->cpu;
  834. if (check_irq_entry(iter, flags, call->func, call->depth))
  835. return TRACE_TYPE_HANDLED;
  836. print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
  837. leaf_ret = get_return_for_leaf(iter, field);
  838. if (leaf_ret)
  839. ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
  840. else
  841. ret = print_graph_entry_nested(iter, field, s, cpu, flags);
  842. if (data) {
  843. /*
  844. * If we failed to write our output, then we need to make
  845. * note of it. Because we already consumed our entry.
  846. */
  847. if (s->full) {
  848. data->failed = 1;
  849. data->cpu = cpu;
  850. } else
  851. data->failed = 0;
  852. }
  853. return ret;
  854. }
  855. static enum print_line_t
  856. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  857. struct trace_entry *ent, struct trace_iterator *iter,
  858. u32 flags)
  859. {
  860. unsigned long long duration = trace->rettime - trace->calltime;
  861. struct fgraph_data *data = iter->private;
  862. pid_t pid = ent->pid;
  863. int cpu = iter->cpu;
  864. int func_match = 1;
  865. int i;
  866. if (check_irq_return(iter, flags, trace->depth))
  867. return TRACE_TYPE_HANDLED;
  868. if (data) {
  869. struct fgraph_cpu_data *cpu_data;
  870. int cpu = iter->cpu;
  871. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  872. /*
  873. * Comments display at + 1 to depth. This is the
  874. * return from a function, we now want the comments
  875. * to display at the same level of the bracket.
  876. */
  877. cpu_data->depth = trace->depth - 1;
  878. if (trace->depth < FTRACE_RETFUNC_DEPTH) {
  879. if (cpu_data->enter_funcs[trace->depth] != trace->func)
  880. func_match = 0;
  881. cpu_data->enter_funcs[trace->depth] = 0;
  882. }
  883. }
  884. print_graph_prologue(iter, s, 0, 0, flags);
  885. /* Overhead and duration */
  886. print_graph_duration(duration, s, flags);
  887. /* Closing brace */
  888. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
  889. trace_seq_putc(s, ' ');
  890. /*
  891. * If the return function does not have a matching entry,
  892. * then the entry was lost. Instead of just printing
  893. * the '}' and letting the user guess what function this
  894. * belongs to, write out the function name. Always do
  895. * that if the funcgraph-tail option is enabled.
  896. */
  897. if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
  898. trace_seq_puts(s, "}\n");
  899. else
  900. trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
  901. /* Overrun */
  902. if (flags & TRACE_GRAPH_PRINT_OVERRUN)
  903. trace_seq_printf(s, " (Overruns: %lu)\n",
  904. trace->overrun);
  905. print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
  906. cpu, pid, flags);
  907. return trace_handle_return(s);
  908. }
  909. static enum print_line_t
  910. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  911. struct trace_iterator *iter, u32 flags)
  912. {
  913. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  914. struct fgraph_data *data = iter->private;
  915. struct trace_event *event;
  916. int depth = 0;
  917. int ret;
  918. int i;
  919. if (data)
  920. depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
  921. print_graph_prologue(iter, s, 0, 0, flags);
  922. /* No time */
  923. print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
  924. /* Indentation */
  925. if (depth > 0)
  926. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
  927. trace_seq_putc(s, ' ');
  928. /* The comment */
  929. trace_seq_puts(s, "/* ");
  930. switch (iter->ent->type) {
  931. case TRACE_BPRINT:
  932. ret = trace_print_bprintk_msg_only(iter);
  933. if (ret != TRACE_TYPE_HANDLED)
  934. return ret;
  935. break;
  936. case TRACE_PRINT:
  937. ret = trace_print_printk_msg_only(iter);
  938. if (ret != TRACE_TYPE_HANDLED)
  939. return ret;
  940. break;
  941. default:
  942. event = ftrace_find_event(ent->type);
  943. if (!event)
  944. return TRACE_TYPE_UNHANDLED;
  945. ret = event->funcs->trace(iter, sym_flags, event);
  946. if (ret != TRACE_TYPE_HANDLED)
  947. return ret;
  948. }
  949. if (trace_seq_has_overflowed(s))
  950. goto out;
  951. /* Strip ending newline */
  952. if (s->buffer[s->seq.len - 1] == '\n') {
  953. s->buffer[s->seq.len - 1] = '\0';
  954. s->seq.len--;
  955. }
  956. trace_seq_puts(s, " */\n");
  957. out:
  958. return trace_handle_return(s);
  959. }
  960. enum print_line_t
  961. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  962. {
  963. struct ftrace_graph_ent_entry *field;
  964. struct fgraph_data *data = iter->private;
  965. struct trace_entry *entry = iter->ent;
  966. struct trace_seq *s = &iter->seq;
  967. int cpu = iter->cpu;
  968. int ret;
  969. if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
  970. per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
  971. return TRACE_TYPE_HANDLED;
  972. }
  973. /*
  974. * If the last output failed, there's a possibility we need
  975. * to print out the missing entry which would never go out.
  976. */
  977. if (data && data->failed) {
  978. field = &data->ent;
  979. iter->cpu = data->cpu;
  980. ret = print_graph_entry(field, s, iter, flags);
  981. if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
  982. per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
  983. ret = TRACE_TYPE_NO_CONSUME;
  984. }
  985. iter->cpu = cpu;
  986. return ret;
  987. }
  988. switch (entry->type) {
  989. case TRACE_GRAPH_ENT: {
  990. /*
  991. * print_graph_entry() may consume the current event,
  992. * thus @field may become invalid, so we need to save it.
  993. * sizeof(struct ftrace_graph_ent_entry) is very small,
  994. * it can be safely saved at the stack.
  995. */
  996. struct ftrace_graph_ent_entry saved;
  997. trace_assign_type(field, entry);
  998. saved = *field;
  999. return print_graph_entry(&saved, s, iter, flags);
  1000. }
  1001. case TRACE_GRAPH_RET: {
  1002. struct ftrace_graph_ret_entry *field;
  1003. trace_assign_type(field, entry);
  1004. return print_graph_return(&field->ret, s, entry, iter, flags);
  1005. }
  1006. case TRACE_STACK:
  1007. case TRACE_FN:
  1008. /* dont trace stack and functions as comments */
  1009. return TRACE_TYPE_UNHANDLED;
  1010. default:
  1011. return print_graph_comment(s, entry, iter, flags);
  1012. }
  1013. return TRACE_TYPE_HANDLED;
  1014. }
  1015. static enum print_line_t
  1016. print_graph_function(struct trace_iterator *iter)
  1017. {
  1018. return print_graph_function_flags(iter, tracer_flags.val);
  1019. }
  1020. static enum print_line_t
  1021. print_graph_function_event(struct trace_iterator *iter, int flags,
  1022. struct trace_event *event)
  1023. {
  1024. return print_graph_function(iter);
  1025. }
  1026. static void print_lat_header(struct seq_file *s, u32 flags)
  1027. {
  1028. static const char spaces[] = " " /* 16 spaces */
  1029. " " /* 4 spaces */
  1030. " "; /* 17 spaces */
  1031. int size = 0;
  1032. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1033. size += 16;
  1034. if (flags & TRACE_GRAPH_PRINT_CPU)
  1035. size += 4;
  1036. if (flags & TRACE_GRAPH_PRINT_PROC)
  1037. size += 17;
  1038. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  1039. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  1040. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  1041. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  1042. seq_printf(s, "#%.*s||| / \n", size, spaces);
  1043. }
  1044. static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
  1045. {
  1046. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  1047. if (lat)
  1048. print_lat_header(s, flags);
  1049. /* 1st line */
  1050. seq_putc(s, '#');
  1051. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1052. seq_puts(s, " TIME ");
  1053. if (flags & TRACE_GRAPH_PRINT_CPU)
  1054. seq_puts(s, " CPU");
  1055. if (flags & TRACE_GRAPH_PRINT_PROC)
  1056. seq_puts(s, " TASK/PID ");
  1057. if (lat)
  1058. seq_puts(s, "||||");
  1059. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1060. seq_puts(s, " DURATION ");
  1061. seq_puts(s, " FUNCTION CALLS\n");
  1062. /* 2nd line */
  1063. seq_putc(s, '#');
  1064. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1065. seq_puts(s, " | ");
  1066. if (flags & TRACE_GRAPH_PRINT_CPU)
  1067. seq_puts(s, " | ");
  1068. if (flags & TRACE_GRAPH_PRINT_PROC)
  1069. seq_puts(s, " | | ");
  1070. if (lat)
  1071. seq_puts(s, "||||");
  1072. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1073. seq_puts(s, " | | ");
  1074. seq_puts(s, " | | | |\n");
  1075. }
  1076. static void print_graph_headers(struct seq_file *s)
  1077. {
  1078. print_graph_headers_flags(s, tracer_flags.val);
  1079. }
  1080. void print_graph_headers_flags(struct seq_file *s, u32 flags)
  1081. {
  1082. struct trace_iterator *iter = s->private;
  1083. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  1084. return;
  1085. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  1086. /* print nothing if the buffers are empty */
  1087. if (trace_empty(iter))
  1088. return;
  1089. print_trace_header(s, iter);
  1090. }
  1091. __print_graph_headers_flags(s, flags);
  1092. }
  1093. void graph_trace_open(struct trace_iterator *iter)
  1094. {
  1095. /* pid and depth on the last trace processed */
  1096. struct fgraph_data *data;
  1097. gfp_t gfpflags;
  1098. int cpu;
  1099. iter->private = NULL;
  1100. /* We can be called in atomic context via ftrace_dump() */
  1101. gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
  1102. data = kzalloc(sizeof(*data), gfpflags);
  1103. if (!data)
  1104. goto out_err;
  1105. data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
  1106. if (!data->cpu_data)
  1107. goto out_err_free;
  1108. for_each_possible_cpu(cpu) {
  1109. pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  1110. int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
  1111. int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
  1112. int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  1113. *pid = -1;
  1114. *depth = 0;
  1115. *ignore = 0;
  1116. *depth_irq = -1;
  1117. }
  1118. iter->private = data;
  1119. return;
  1120. out_err_free:
  1121. kfree(data);
  1122. out_err:
  1123. pr_warning("function graph tracer: not enough memory\n");
  1124. }
  1125. void graph_trace_close(struct trace_iterator *iter)
  1126. {
  1127. struct fgraph_data *data = iter->private;
  1128. if (data) {
  1129. free_percpu(data->cpu_data);
  1130. kfree(data);
  1131. }
  1132. }
  1133. static int
  1134. func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  1135. {
  1136. if (bit == TRACE_GRAPH_PRINT_IRQS)
  1137. ftrace_graph_skip_irqs = !set;
  1138. return 0;
  1139. }
  1140. static struct trace_event_functions graph_functions = {
  1141. .trace = print_graph_function_event,
  1142. };
  1143. static struct trace_event graph_trace_entry_event = {
  1144. .type = TRACE_GRAPH_ENT,
  1145. .funcs = &graph_functions,
  1146. };
  1147. static struct trace_event graph_trace_ret_event = {
  1148. .type = TRACE_GRAPH_RET,
  1149. .funcs = &graph_functions
  1150. };
  1151. static struct tracer graph_trace __tracer_data = {
  1152. .name = "function_graph",
  1153. .update_thresh = graph_trace_update_thresh,
  1154. .open = graph_trace_open,
  1155. .pipe_open = graph_trace_open,
  1156. .close = graph_trace_close,
  1157. .pipe_close = graph_trace_close,
  1158. .init = graph_trace_init,
  1159. .reset = graph_trace_reset,
  1160. .print_line = print_graph_function,
  1161. .print_header = print_graph_headers,
  1162. .flags = &tracer_flags,
  1163. .set_flag = func_graph_set_flag,
  1164. #ifdef CONFIG_FTRACE_SELFTEST
  1165. .selftest = trace_selftest_startup_function_graph,
  1166. #endif
  1167. };
  1168. static ssize_t
  1169. graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1170. loff_t *ppos)
  1171. {
  1172. unsigned long val;
  1173. int ret;
  1174. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1175. if (ret)
  1176. return ret;
  1177. max_depth = val;
  1178. *ppos += cnt;
  1179. return cnt;
  1180. }
  1181. static ssize_t
  1182. graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
  1183. loff_t *ppos)
  1184. {
  1185. char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
  1186. int n;
  1187. n = sprintf(buf, "%d\n", max_depth);
  1188. return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
  1189. }
  1190. static const struct file_operations graph_depth_fops = {
  1191. .open = tracing_open_generic,
  1192. .write = graph_depth_write,
  1193. .read = graph_depth_read,
  1194. .llseek = generic_file_llseek,
  1195. };
  1196. static __init int init_graph_tracefs(void)
  1197. {
  1198. struct dentry *d_tracer;
  1199. d_tracer = tracing_init_dentry();
  1200. if (IS_ERR(d_tracer))
  1201. return 0;
  1202. trace_create_file("max_graph_depth", 0644, d_tracer,
  1203. NULL, &graph_depth_fops);
  1204. return 0;
  1205. }
  1206. fs_initcall(init_graph_tracefs);
  1207. static __init int init_graph_trace(void)
  1208. {
  1209. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  1210. if (!register_trace_event(&graph_trace_entry_event)) {
  1211. pr_warning("Warning: could not register graph trace events\n");
  1212. return 1;
  1213. }
  1214. if (!register_trace_event(&graph_trace_ret_event)) {
  1215. pr_warning("Warning: could not register graph trace events\n");
  1216. return 1;
  1217. }
  1218. return register_tracer(&graph_trace);
  1219. }
  1220. core_initcall(init_graph_trace);