trace_functions_graph.c 35 KB

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