trace_functions_graph.c 36 KB

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