trace_functions_graph.c 38 KB

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