trace_functions_graph.c 34 KB

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