trace_functions_graph.c 36 KB

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