trace_functions_graph.c 36 KB

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