trace_functions_graph.c 38 KB

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