trace_functions_graph.c 35 KB

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