trace_functions_graph.c 39 KB

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