trace.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 William Lee Irwin III
  13. */
  14. #include <linux/utsrelease.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/linkage.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/module.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ctype.h>
  26. #include <linux/init.h>
  27. #include <linux/poll.h>
  28. #include <linux/gfp.h>
  29. #include <linux/fs.h>
  30. #include <linux/stacktrace.h>
  31. #include "trace.h"
  32. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  33. unsigned long __read_mostly tracing_thresh;
  34. static int tracing_disabled = 1;
  35. static long
  36. ns2usecs(cycle_t nsec)
  37. {
  38. nsec += 500;
  39. do_div(nsec, 1000);
  40. return nsec;
  41. }
  42. cycle_t ftrace_now(int cpu)
  43. {
  44. return cpu_clock(cpu);
  45. }
  46. static struct trace_array global_trace;
  47. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  48. static struct trace_array max_tr;
  49. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  50. static int tracer_enabled = 1;
  51. static unsigned long trace_nr_entries = 65536UL;
  52. static struct tracer *trace_types __read_mostly;
  53. static struct tracer *current_trace __read_mostly;
  54. static int max_tracer_type_len;
  55. static DEFINE_MUTEX(trace_types_lock);
  56. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  57. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  58. void trace_wake_up(void)
  59. {
  60. /*
  61. * The runqueue_is_locked() can fail, but this is the best we
  62. * have for now:
  63. */
  64. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  65. wake_up(&trace_wait);
  66. }
  67. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  68. static int __init set_nr_entries(char *str)
  69. {
  70. if (!str)
  71. return 0;
  72. trace_nr_entries = simple_strtoul(str, &str, 0);
  73. return 1;
  74. }
  75. __setup("trace_entries=", set_nr_entries);
  76. unsigned long nsecs_to_usecs(unsigned long nsecs)
  77. {
  78. return nsecs / 1000;
  79. }
  80. enum trace_type {
  81. __TRACE_FIRST_TYPE = 0,
  82. TRACE_FN,
  83. TRACE_CTX,
  84. TRACE_WAKE,
  85. TRACE_STACK,
  86. TRACE_SPECIAL,
  87. __TRACE_LAST_TYPE
  88. };
  89. enum trace_flag_type {
  90. TRACE_FLAG_IRQS_OFF = 0x01,
  91. TRACE_FLAG_NEED_RESCHED = 0x02,
  92. TRACE_FLAG_HARDIRQ = 0x04,
  93. TRACE_FLAG_SOFTIRQ = 0x08,
  94. };
  95. #define TRACE_ITER_SYM_MASK \
  96. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  97. /* These must match the bit postions above */
  98. static const char *trace_options[] = {
  99. "print-parent",
  100. "sym-offset",
  101. "sym-addr",
  102. "verbose",
  103. "raw",
  104. "hex",
  105. "bin",
  106. "block",
  107. "stacktrace",
  108. NULL
  109. };
  110. static DEFINE_SPINLOCK(ftrace_max_lock);
  111. /*
  112. * Copy the new maximum trace into the separate maximum-trace
  113. * structure. (this way the maximum trace is permanently saved,
  114. * for later retrieval via /debugfs/tracing/latency_trace)
  115. */
  116. static void
  117. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  118. {
  119. struct trace_array_cpu *data = tr->data[cpu];
  120. max_tr.cpu = cpu;
  121. max_tr.time_start = data->preempt_timestamp;
  122. data = max_tr.data[cpu];
  123. data->saved_latency = tracing_max_latency;
  124. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  125. data->pid = tsk->pid;
  126. data->uid = tsk->uid;
  127. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  128. data->policy = tsk->policy;
  129. data->rt_priority = tsk->rt_priority;
  130. /* record this tasks comm */
  131. tracing_record_cmdline(current);
  132. }
  133. void check_pages(struct trace_array_cpu *data)
  134. {
  135. struct page *page, *tmp;
  136. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  137. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  138. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  139. BUG_ON(page->lru.next->prev != &page->lru);
  140. BUG_ON(page->lru.prev->next != &page->lru);
  141. }
  142. }
  143. void *head_page(struct trace_array_cpu *data)
  144. {
  145. struct page *page;
  146. check_pages(data);
  147. if (list_empty(&data->trace_pages))
  148. return NULL;
  149. page = list_entry(data->trace_pages.next, struct page, lru);
  150. BUG_ON(&page->lru == &data->trace_pages);
  151. return page_address(page);
  152. }
  153. static int
  154. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  155. {
  156. int len = (PAGE_SIZE - 1) - s->len;
  157. va_list ap;
  158. int ret;
  159. if (!len)
  160. return 0;
  161. va_start(ap, fmt);
  162. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  163. va_end(ap);
  164. /* If we can't write it all, don't bother writing anything */
  165. if (ret > len)
  166. return 0;
  167. s->len += ret;
  168. return len;
  169. }
  170. static int
  171. trace_seq_puts(struct trace_seq *s, const char *str)
  172. {
  173. int len = strlen(str);
  174. if (len > ((PAGE_SIZE - 1) - s->len))
  175. return 0;
  176. memcpy(s->buffer + s->len, str, len);
  177. s->len += len;
  178. return len;
  179. }
  180. static int
  181. trace_seq_putc(struct trace_seq *s, unsigned char c)
  182. {
  183. if (s->len >= (PAGE_SIZE - 1))
  184. return 0;
  185. s->buffer[s->len++] = c;
  186. return 1;
  187. }
  188. static int
  189. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  190. {
  191. if (len > ((PAGE_SIZE - 1) - s->len))
  192. return 0;
  193. memcpy(s->buffer + s->len, mem, len);
  194. s->len += len;
  195. return len;
  196. }
  197. #define HEX_CHARS 17
  198. static int
  199. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  200. {
  201. unsigned char hex[HEX_CHARS];
  202. unsigned char *data;
  203. unsigned char byte;
  204. int i, j;
  205. BUG_ON(len >= HEX_CHARS);
  206. data = mem;
  207. #ifdef __BIG_ENDIAN
  208. for (i = 0, j = 0; i < len; i++) {
  209. #else
  210. for (i = len-1, j = 0; i >= 0; i--) {
  211. #endif
  212. byte = data[i];
  213. hex[j] = byte & 0x0f;
  214. if (hex[j] >= 10)
  215. hex[j] += 'a' - 10;
  216. else
  217. hex[j] += '0';
  218. j++;
  219. hex[j] = byte >> 4;
  220. if (hex[j] >= 10)
  221. hex[j] += 'a' - 10;
  222. else
  223. hex[j] += '0';
  224. j++;
  225. }
  226. hex[j] = ' ';
  227. j++;
  228. return trace_seq_putmem(s, hex, j);
  229. }
  230. static void
  231. trace_seq_reset(struct trace_seq *s)
  232. {
  233. s->len = 0;
  234. }
  235. static void
  236. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  237. {
  238. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  239. s->buffer[len] = 0;
  240. seq_puts(m, s->buffer);
  241. trace_seq_reset(s);
  242. }
  243. static void
  244. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  245. {
  246. struct list_head flip_pages;
  247. INIT_LIST_HEAD(&flip_pages);
  248. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  249. sizeof(struct trace_array_cpu) -
  250. offsetof(struct trace_array_cpu, trace_head_idx));
  251. check_pages(tr1);
  252. check_pages(tr2);
  253. list_splice_init(&tr1->trace_pages, &flip_pages);
  254. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  255. list_splice_init(&flip_pages, &tr2->trace_pages);
  256. BUG_ON(!list_empty(&flip_pages));
  257. check_pages(tr1);
  258. check_pages(tr2);
  259. }
  260. void
  261. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  262. {
  263. struct trace_array_cpu *data;
  264. int i;
  265. WARN_ON_ONCE(!irqs_disabled());
  266. spin_lock(&ftrace_max_lock);
  267. /* clear out all the previous traces */
  268. for_each_possible_cpu(i) {
  269. data = tr->data[i];
  270. flip_trace(max_tr.data[i], data);
  271. tracing_reset(data);
  272. }
  273. __update_max_tr(tr, tsk, cpu);
  274. spin_unlock(&ftrace_max_lock);
  275. }
  276. /**
  277. * update_max_tr_single - only copy one trace over, and reset the rest
  278. * @tr - tracer
  279. * @tsk - task with the latency
  280. * @cpu - the cpu of the buffer to copy.
  281. */
  282. void
  283. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  284. {
  285. struct trace_array_cpu *data = tr->data[cpu];
  286. int i;
  287. WARN_ON_ONCE(!irqs_disabled());
  288. spin_lock(&ftrace_max_lock);
  289. for_each_possible_cpu(i)
  290. tracing_reset(max_tr.data[i]);
  291. flip_trace(max_tr.data[cpu], data);
  292. tracing_reset(data);
  293. __update_max_tr(tr, tsk, cpu);
  294. spin_unlock(&ftrace_max_lock);
  295. }
  296. int register_tracer(struct tracer *type)
  297. {
  298. struct tracer *t;
  299. int len;
  300. int ret = 0;
  301. if (!type->name) {
  302. pr_info("Tracer must have a name\n");
  303. return -1;
  304. }
  305. mutex_lock(&trace_types_lock);
  306. for (t = trace_types; t; t = t->next) {
  307. if (strcmp(type->name, t->name) == 0) {
  308. /* already found */
  309. pr_info("Trace %s already registered\n",
  310. type->name);
  311. ret = -1;
  312. goto out;
  313. }
  314. }
  315. #ifdef CONFIG_FTRACE_STARTUP_TEST
  316. if (type->selftest) {
  317. struct tracer *saved_tracer = current_trace;
  318. struct trace_array_cpu *data;
  319. struct trace_array *tr = &global_trace;
  320. int saved_ctrl = tr->ctrl;
  321. int i;
  322. /*
  323. * Run a selftest on this tracer.
  324. * Here we reset the trace buffer, and set the current
  325. * tracer to be this tracer. The tracer can then run some
  326. * internal tracing to verify that everything is in order.
  327. * If we fail, we do not register this tracer.
  328. */
  329. for_each_possible_cpu(i) {
  330. data = tr->data[i];
  331. if (!head_page(data))
  332. continue;
  333. tracing_reset(data);
  334. }
  335. current_trace = type;
  336. tr->ctrl = 0;
  337. /* the test is responsible for initializing and enabling */
  338. pr_info("Testing tracer %s: ", type->name);
  339. ret = type->selftest(type, tr);
  340. /* the test is responsible for resetting too */
  341. current_trace = saved_tracer;
  342. tr->ctrl = saved_ctrl;
  343. if (ret) {
  344. printk(KERN_CONT "FAILED!\n");
  345. goto out;
  346. }
  347. /* Only reset on passing, to avoid touching corrupted buffers */
  348. for_each_possible_cpu(i) {
  349. data = tr->data[i];
  350. if (!head_page(data))
  351. continue;
  352. tracing_reset(data);
  353. }
  354. printk(KERN_CONT "PASSED\n");
  355. }
  356. #endif
  357. type->next = trace_types;
  358. trace_types = type;
  359. len = strlen(type->name);
  360. if (len > max_tracer_type_len)
  361. max_tracer_type_len = len;
  362. out:
  363. mutex_unlock(&trace_types_lock);
  364. return ret;
  365. }
  366. void unregister_tracer(struct tracer *type)
  367. {
  368. struct tracer **t;
  369. int len;
  370. mutex_lock(&trace_types_lock);
  371. for (t = &trace_types; *t; t = &(*t)->next) {
  372. if (*t == type)
  373. goto found;
  374. }
  375. pr_info("Trace %s not registered\n", type->name);
  376. goto out;
  377. found:
  378. *t = (*t)->next;
  379. if (strlen(type->name) != max_tracer_type_len)
  380. goto out;
  381. max_tracer_type_len = 0;
  382. for (t = &trace_types; *t; t = &(*t)->next) {
  383. len = strlen((*t)->name);
  384. if (len > max_tracer_type_len)
  385. max_tracer_type_len = len;
  386. }
  387. out:
  388. mutex_unlock(&trace_types_lock);
  389. }
  390. void tracing_reset(struct trace_array_cpu *data)
  391. {
  392. data->trace_idx = 0;
  393. data->trace_head = data->trace_tail = head_page(data);
  394. data->trace_head_idx = 0;
  395. data->trace_tail_idx = 0;
  396. }
  397. #define SAVED_CMDLINES 128
  398. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  399. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  400. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  401. static int cmdline_idx;
  402. static DEFINE_SPINLOCK(trace_cmdline_lock);
  403. atomic_t trace_record_cmdline_disabled;
  404. static void trace_init_cmdlines(void)
  405. {
  406. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  407. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  408. cmdline_idx = 0;
  409. }
  410. void trace_stop_cmdline_recording(void);
  411. static void trace_save_cmdline(struct task_struct *tsk)
  412. {
  413. unsigned map;
  414. unsigned idx;
  415. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  416. return;
  417. /*
  418. * It's not the end of the world if we don't get
  419. * the lock, but we also don't want to spin
  420. * nor do we want to disable interrupts,
  421. * so if we miss here, then better luck next time.
  422. */
  423. if (!spin_trylock(&trace_cmdline_lock))
  424. return;
  425. idx = map_pid_to_cmdline[tsk->pid];
  426. if (idx >= SAVED_CMDLINES) {
  427. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  428. map = map_cmdline_to_pid[idx];
  429. if (map <= PID_MAX_DEFAULT)
  430. map_pid_to_cmdline[map] = (unsigned)-1;
  431. map_pid_to_cmdline[tsk->pid] = idx;
  432. cmdline_idx = idx;
  433. }
  434. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  435. spin_unlock(&trace_cmdline_lock);
  436. }
  437. static char *trace_find_cmdline(int pid)
  438. {
  439. char *cmdline = "<...>";
  440. unsigned map;
  441. if (!pid)
  442. return "<idle>";
  443. if (pid > PID_MAX_DEFAULT)
  444. goto out;
  445. map = map_pid_to_cmdline[pid];
  446. if (map >= SAVED_CMDLINES)
  447. goto out;
  448. cmdline = saved_cmdlines[map];
  449. out:
  450. return cmdline;
  451. }
  452. void tracing_record_cmdline(struct task_struct *tsk)
  453. {
  454. if (atomic_read(&trace_record_cmdline_disabled))
  455. return;
  456. trace_save_cmdline(tsk);
  457. }
  458. static inline struct list_head *
  459. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  460. {
  461. /*
  462. * Roundrobin - but skip the head (which is not a real page):
  463. */
  464. next = next->next;
  465. if (unlikely(next == &data->trace_pages))
  466. next = next->next;
  467. BUG_ON(next == &data->trace_pages);
  468. return next;
  469. }
  470. static inline void *
  471. trace_next_page(struct trace_array_cpu *data, void *addr)
  472. {
  473. struct list_head *next;
  474. struct page *page;
  475. page = virt_to_page(addr);
  476. next = trace_next_list(data, &page->lru);
  477. page = list_entry(next, struct page, lru);
  478. return page_address(page);
  479. }
  480. static inline struct trace_entry *
  481. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  482. {
  483. unsigned long idx, idx_next;
  484. struct trace_entry *entry;
  485. data->trace_idx++;
  486. idx = data->trace_head_idx;
  487. idx_next = idx + 1;
  488. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  489. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  490. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  491. data->trace_head = trace_next_page(data, data->trace_head);
  492. idx_next = 0;
  493. }
  494. if (data->trace_head == data->trace_tail &&
  495. idx_next == data->trace_tail_idx) {
  496. /* overrun */
  497. data->trace_tail_idx++;
  498. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  499. data->trace_tail =
  500. trace_next_page(data, data->trace_tail);
  501. data->trace_tail_idx = 0;
  502. }
  503. }
  504. data->trace_head_idx = idx_next;
  505. return entry;
  506. }
  507. static inline void
  508. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  509. {
  510. struct task_struct *tsk = current;
  511. unsigned long pc;
  512. pc = preempt_count();
  513. entry->preempt_count = pc & 0xff;
  514. entry->pid = tsk->pid;
  515. entry->t = ftrace_now(raw_smp_processor_id());
  516. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  517. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  518. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  519. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  520. }
  521. void
  522. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  523. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  524. {
  525. struct trace_entry *entry;
  526. unsigned long irq_flags;
  527. spin_lock_irqsave(&data->lock, irq_flags);
  528. entry = tracing_get_trace_entry(tr, data);
  529. tracing_generic_entry_update(entry, flags);
  530. entry->type = TRACE_FN;
  531. entry->fn.ip = ip;
  532. entry->fn.parent_ip = parent_ip;
  533. spin_unlock_irqrestore(&data->lock, irq_flags);
  534. trace_wake_up();
  535. }
  536. void
  537. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  538. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  539. {
  540. if (likely(!atomic_read(&data->disabled)))
  541. trace_function(tr, data, ip, parent_ip, flags);
  542. }
  543. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  544. void
  545. __trace_special(void *__tr, void *__data,
  546. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  547. {
  548. struct trace_array_cpu *data = __data;
  549. struct trace_array *tr = __tr;
  550. struct trace_entry *entry;
  551. unsigned long irq_flags;
  552. spin_lock_irqsave(&data->lock, irq_flags);
  553. entry = tracing_get_trace_entry(tr, data);
  554. tracing_generic_entry_update(entry, 0);
  555. entry->type = TRACE_SPECIAL;
  556. entry->special.arg1 = arg1;
  557. entry->special.arg2 = arg2;
  558. entry->special.arg3 = arg3;
  559. spin_unlock_irqrestore(&data->lock, irq_flags);
  560. trace_wake_up();
  561. }
  562. #endif
  563. void __trace_stack(struct trace_array *tr,
  564. struct trace_array_cpu *data,
  565. unsigned long flags,
  566. int skip)
  567. {
  568. struct trace_entry *entry;
  569. struct stack_trace trace;
  570. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  571. return;
  572. entry = tracing_get_trace_entry(tr, data);
  573. tracing_generic_entry_update(entry, flags);
  574. entry->type = TRACE_STACK;
  575. memset(&entry->stack, 0, sizeof(entry->stack));
  576. trace.nr_entries = 0;
  577. trace.max_entries = FTRACE_STACK_ENTRIES;
  578. trace.skip = skip;
  579. trace.entries = entry->stack.caller;
  580. save_stack_trace(&trace);
  581. }
  582. void
  583. tracing_sched_switch_trace(struct trace_array *tr,
  584. struct trace_array_cpu *data,
  585. struct task_struct *prev,
  586. struct task_struct *next,
  587. unsigned long flags)
  588. {
  589. struct trace_entry *entry;
  590. unsigned long irq_flags;
  591. spin_lock_irqsave(&data->lock, irq_flags);
  592. entry = tracing_get_trace_entry(tr, data);
  593. tracing_generic_entry_update(entry, flags);
  594. entry->type = TRACE_CTX;
  595. entry->ctx.prev_pid = prev->pid;
  596. entry->ctx.prev_prio = prev->prio;
  597. entry->ctx.prev_state = prev->state;
  598. entry->ctx.next_pid = next->pid;
  599. entry->ctx.next_prio = next->prio;
  600. __trace_stack(tr, data, flags, 4);
  601. spin_unlock_irqrestore(&data->lock, irq_flags);
  602. }
  603. void
  604. tracing_sched_wakeup_trace(struct trace_array *tr,
  605. struct trace_array_cpu *data,
  606. struct task_struct *wakee,
  607. struct task_struct *curr,
  608. unsigned long flags)
  609. {
  610. struct trace_entry *entry;
  611. unsigned long irq_flags;
  612. spin_lock_irqsave(&data->lock, irq_flags);
  613. entry = tracing_get_trace_entry(tr, data);
  614. tracing_generic_entry_update(entry, flags);
  615. entry->type = TRACE_WAKE;
  616. entry->ctx.prev_pid = curr->pid;
  617. entry->ctx.prev_prio = curr->prio;
  618. entry->ctx.prev_state = curr->state;
  619. entry->ctx.next_pid = wakee->pid;
  620. entry->ctx.next_prio = wakee->prio;
  621. __trace_stack(tr, data, flags, 5);
  622. spin_unlock_irqrestore(&data->lock, irq_flags);
  623. trace_wake_up();
  624. }
  625. #ifdef CONFIG_FTRACE
  626. static void
  627. function_trace_call(unsigned long ip, unsigned long parent_ip)
  628. {
  629. struct trace_array *tr = &global_trace;
  630. struct trace_array_cpu *data;
  631. unsigned long flags;
  632. long disabled;
  633. int cpu;
  634. if (unlikely(!tracer_enabled))
  635. return;
  636. local_irq_save(flags);
  637. cpu = raw_smp_processor_id();
  638. data = tr->data[cpu];
  639. disabled = atomic_inc_return(&data->disabled);
  640. if (likely(disabled == 1))
  641. trace_function(tr, data, ip, parent_ip, flags);
  642. atomic_dec(&data->disabled);
  643. local_irq_restore(flags);
  644. }
  645. static struct ftrace_ops trace_ops __read_mostly =
  646. {
  647. .func = function_trace_call,
  648. };
  649. void tracing_start_function_trace(void)
  650. {
  651. register_ftrace_function(&trace_ops);
  652. }
  653. void tracing_stop_function_trace(void)
  654. {
  655. unregister_ftrace_function(&trace_ops);
  656. }
  657. #endif
  658. enum trace_file_type {
  659. TRACE_FILE_LAT_FMT = 1,
  660. };
  661. static struct trace_entry *
  662. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  663. struct trace_iterator *iter, int cpu)
  664. {
  665. struct page *page;
  666. struct trace_entry *array;
  667. if (iter->next_idx[cpu] >= tr->entries ||
  668. iter->next_idx[cpu] >= data->trace_idx ||
  669. (data->trace_head == data->trace_tail &&
  670. data->trace_head_idx == data->trace_tail_idx))
  671. return NULL;
  672. if (!iter->next_page[cpu]) {
  673. /* Initialize the iterator for this cpu trace buffer */
  674. WARN_ON(!data->trace_tail);
  675. page = virt_to_page(data->trace_tail);
  676. iter->next_page[cpu] = &page->lru;
  677. iter->next_page_idx[cpu] = data->trace_tail_idx;
  678. }
  679. page = list_entry(iter->next_page[cpu], struct page, lru);
  680. BUG_ON(&data->trace_pages == &page->lru);
  681. array = page_address(page);
  682. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  683. return &array[iter->next_page_idx[cpu]];
  684. }
  685. static struct trace_entry *
  686. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  687. {
  688. struct trace_array *tr = iter->tr;
  689. struct trace_entry *ent, *next = NULL;
  690. int next_cpu = -1;
  691. int cpu;
  692. for_each_possible_cpu(cpu) {
  693. if (!head_page(tr->data[cpu]))
  694. continue;
  695. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  696. /*
  697. * Pick the entry with the smallest timestamp:
  698. */
  699. if (ent && (!next || ent->t < next->t)) {
  700. next = ent;
  701. next_cpu = cpu;
  702. }
  703. }
  704. if (ent_cpu)
  705. *ent_cpu = next_cpu;
  706. return next;
  707. }
  708. static void trace_iterator_increment(struct trace_iterator *iter)
  709. {
  710. iter->idx++;
  711. iter->next_idx[iter->cpu]++;
  712. iter->next_page_idx[iter->cpu]++;
  713. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  714. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  715. iter->next_page_idx[iter->cpu] = 0;
  716. iter->next_page[iter->cpu] =
  717. trace_next_list(data, iter->next_page[iter->cpu]);
  718. }
  719. }
  720. static void trace_consume(struct trace_iterator *iter)
  721. {
  722. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  723. data->trace_tail_idx++;
  724. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  725. data->trace_tail = trace_next_page(data, data->trace_tail);
  726. data->trace_tail_idx = 0;
  727. }
  728. /* Check if we empty it, then reset the index */
  729. if (data->trace_head == data->trace_tail &&
  730. data->trace_head_idx == data->trace_tail_idx)
  731. data->trace_idx = 0;
  732. }
  733. static void *find_next_entry_inc(struct trace_iterator *iter)
  734. {
  735. struct trace_entry *next;
  736. int next_cpu = -1;
  737. next = find_next_entry(iter, &next_cpu);
  738. iter->prev_ent = iter->ent;
  739. iter->prev_cpu = iter->cpu;
  740. iter->ent = next;
  741. iter->cpu = next_cpu;
  742. if (next)
  743. trace_iterator_increment(iter);
  744. return next ? iter : NULL;
  745. }
  746. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  747. {
  748. struct trace_iterator *iter = m->private;
  749. void *last_ent = iter->ent;
  750. int i = (int)*pos;
  751. void *ent;
  752. (*pos)++;
  753. /* can't go backwards */
  754. if (iter->idx > i)
  755. return NULL;
  756. if (iter->idx < 0)
  757. ent = find_next_entry_inc(iter);
  758. else
  759. ent = iter;
  760. while (ent && iter->idx < i)
  761. ent = find_next_entry_inc(iter);
  762. iter->pos = *pos;
  763. if (last_ent && !ent)
  764. seq_puts(m, "\n\nvim:ft=help\n");
  765. return ent;
  766. }
  767. static void *s_start(struct seq_file *m, loff_t *pos)
  768. {
  769. struct trace_iterator *iter = m->private;
  770. void *p = NULL;
  771. loff_t l = 0;
  772. int i;
  773. mutex_lock(&trace_types_lock);
  774. if (!current_trace || current_trace != iter->trace)
  775. return NULL;
  776. atomic_inc(&trace_record_cmdline_disabled);
  777. /* let the tracer grab locks here if needed */
  778. if (current_trace->start)
  779. current_trace->start(iter);
  780. if (*pos != iter->pos) {
  781. iter->ent = NULL;
  782. iter->cpu = 0;
  783. iter->idx = -1;
  784. iter->prev_ent = NULL;
  785. iter->prev_cpu = -1;
  786. for_each_possible_cpu(i) {
  787. iter->next_idx[i] = 0;
  788. iter->next_page[i] = NULL;
  789. }
  790. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  791. ;
  792. } else {
  793. l = *pos - 1;
  794. p = s_next(m, p, &l);
  795. }
  796. return p;
  797. }
  798. static void s_stop(struct seq_file *m, void *p)
  799. {
  800. struct trace_iterator *iter = m->private;
  801. atomic_dec(&trace_record_cmdline_disabled);
  802. /* let the tracer release locks here if needed */
  803. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  804. iter->trace->stop(iter);
  805. mutex_unlock(&trace_types_lock);
  806. }
  807. static int
  808. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  809. {
  810. #ifdef CONFIG_KALLSYMS
  811. char str[KSYM_SYMBOL_LEN];
  812. kallsyms_lookup(address, NULL, NULL, NULL, str);
  813. return trace_seq_printf(s, fmt, str);
  814. #endif
  815. return 1;
  816. }
  817. static int
  818. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  819. unsigned long address)
  820. {
  821. #ifdef CONFIG_KALLSYMS
  822. char str[KSYM_SYMBOL_LEN];
  823. sprint_symbol(str, address);
  824. return trace_seq_printf(s, fmt, str);
  825. #endif
  826. return 1;
  827. }
  828. #ifndef CONFIG_64BIT
  829. # define IP_FMT "%08lx"
  830. #else
  831. # define IP_FMT "%016lx"
  832. #endif
  833. static int
  834. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  835. {
  836. int ret;
  837. if (!ip)
  838. return trace_seq_printf(s, "0");
  839. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  840. ret = seq_print_sym_offset(s, "%s", ip);
  841. else
  842. ret = seq_print_sym_short(s, "%s", ip);
  843. if (!ret)
  844. return 0;
  845. if (sym_flags & TRACE_ITER_SYM_ADDR)
  846. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  847. return ret;
  848. }
  849. static void print_lat_help_header(struct seq_file *m)
  850. {
  851. seq_puts(m, "# _------=> CPU# \n");
  852. seq_puts(m, "# / _-----=> irqs-off \n");
  853. seq_puts(m, "# | / _----=> need-resched \n");
  854. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  855. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  856. seq_puts(m, "# |||| / \n");
  857. seq_puts(m, "# ||||| delay \n");
  858. seq_puts(m, "# cmd pid ||||| time | caller \n");
  859. seq_puts(m, "# \\ / ||||| \\ | / \n");
  860. }
  861. static void print_func_help_header(struct seq_file *m)
  862. {
  863. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  864. seq_puts(m, "# | | | | |\n");
  865. }
  866. static void
  867. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  868. {
  869. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  870. struct trace_array *tr = iter->tr;
  871. struct trace_array_cpu *data = tr->data[tr->cpu];
  872. struct tracer *type = current_trace;
  873. unsigned long total = 0;
  874. unsigned long entries = 0;
  875. int cpu;
  876. const char *name = "preemption";
  877. if (type)
  878. name = type->name;
  879. for_each_possible_cpu(cpu) {
  880. if (head_page(tr->data[cpu])) {
  881. total += tr->data[cpu]->trace_idx;
  882. if (tr->data[cpu]->trace_idx > tr->entries)
  883. entries += tr->entries;
  884. else
  885. entries += tr->data[cpu]->trace_idx;
  886. }
  887. }
  888. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  889. name, UTS_RELEASE);
  890. seq_puts(m, "-----------------------------------"
  891. "---------------------------------\n");
  892. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  893. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  894. nsecs_to_usecs(data->saved_latency),
  895. entries,
  896. total,
  897. tr->cpu,
  898. #if defined(CONFIG_PREEMPT_NONE)
  899. "server",
  900. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  901. "desktop",
  902. #elif defined(CONFIG_PREEMPT_DESKTOP)
  903. "preempt",
  904. #else
  905. "unknown",
  906. #endif
  907. /* These are reserved for later use */
  908. 0, 0, 0, 0);
  909. #ifdef CONFIG_SMP
  910. seq_printf(m, " #P:%d)\n", num_online_cpus());
  911. #else
  912. seq_puts(m, ")\n");
  913. #endif
  914. seq_puts(m, " -----------------\n");
  915. seq_printf(m, " | task: %.16s-%d "
  916. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  917. data->comm, data->pid, data->uid, data->nice,
  918. data->policy, data->rt_priority);
  919. seq_puts(m, " -----------------\n");
  920. if (data->critical_start) {
  921. seq_puts(m, " => started at: ");
  922. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  923. trace_print_seq(m, &iter->seq);
  924. seq_puts(m, "\n => ended at: ");
  925. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  926. trace_print_seq(m, &iter->seq);
  927. seq_puts(m, "\n");
  928. }
  929. seq_puts(m, "\n");
  930. }
  931. static void
  932. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  933. {
  934. int hardirq, softirq;
  935. char *comm;
  936. comm = trace_find_cmdline(entry->pid);
  937. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  938. trace_seq_printf(s, "%d", cpu);
  939. trace_seq_printf(s, "%c%c",
  940. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  941. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  942. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  943. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  944. if (hardirq && softirq)
  945. trace_seq_putc(s, 'H');
  946. else {
  947. if (hardirq)
  948. trace_seq_putc(s, 'h');
  949. else {
  950. if (softirq)
  951. trace_seq_putc(s, 's');
  952. else
  953. trace_seq_putc(s, '.');
  954. }
  955. }
  956. if (entry->preempt_count)
  957. trace_seq_printf(s, "%x", entry->preempt_count);
  958. else
  959. trace_seq_puts(s, ".");
  960. }
  961. unsigned long preempt_mark_thresh = 100;
  962. static void
  963. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  964. unsigned long rel_usecs)
  965. {
  966. trace_seq_printf(s, " %4lldus", abs_usecs);
  967. if (rel_usecs > preempt_mark_thresh)
  968. trace_seq_puts(s, "!: ");
  969. else if (rel_usecs > 1)
  970. trace_seq_puts(s, "+: ");
  971. else
  972. trace_seq_puts(s, " : ");
  973. }
  974. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  975. static int
  976. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  977. {
  978. struct trace_seq *s = &iter->seq;
  979. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  980. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  981. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  982. struct trace_entry *entry = iter->ent;
  983. unsigned long abs_usecs;
  984. unsigned long rel_usecs;
  985. char *comm;
  986. int S;
  987. int i;
  988. if (!next_entry)
  989. next_entry = entry;
  990. rel_usecs = ns2usecs(next_entry->t - entry->t);
  991. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  992. if (verbose) {
  993. comm = trace_find_cmdline(entry->pid);
  994. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  995. " %ld.%03ldms (+%ld.%03ldms): ",
  996. comm,
  997. entry->pid, cpu, entry->flags,
  998. entry->preempt_count, trace_idx,
  999. ns2usecs(entry->t),
  1000. abs_usecs/1000,
  1001. abs_usecs % 1000, rel_usecs/1000,
  1002. rel_usecs % 1000);
  1003. } else {
  1004. if (entry->type != TRACE_STACK) {
  1005. lat_print_generic(s, entry, cpu);
  1006. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1007. }
  1008. }
  1009. switch (entry->type) {
  1010. case TRACE_FN:
  1011. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1012. trace_seq_puts(s, " (");
  1013. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1014. trace_seq_puts(s, ")\n");
  1015. break;
  1016. case TRACE_CTX:
  1017. case TRACE_WAKE:
  1018. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1019. state_to_char[entry->ctx.prev_state] : 'X';
  1020. comm = trace_find_cmdline(entry->ctx.next_pid);
  1021. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
  1022. entry->ctx.prev_pid,
  1023. entry->ctx.prev_prio,
  1024. S, entry->type == TRACE_CTX ? "==>" : " +",
  1025. entry->ctx.next_pid,
  1026. entry->ctx.next_prio,
  1027. comm);
  1028. break;
  1029. case TRACE_SPECIAL:
  1030. trace_seq_printf(s, " %ld %ld %ld\n",
  1031. entry->special.arg1,
  1032. entry->special.arg2,
  1033. entry->special.arg3);
  1034. break;
  1035. case TRACE_STACK:
  1036. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1037. if (i)
  1038. trace_seq_puts(s, " <= ");
  1039. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1040. }
  1041. trace_seq_puts(s, "\n");
  1042. break;
  1043. default:
  1044. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1045. }
  1046. return 1;
  1047. }
  1048. static int print_trace_fmt(struct trace_iterator *iter)
  1049. {
  1050. struct trace_seq *s = &iter->seq;
  1051. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1052. struct trace_entry *entry;
  1053. unsigned long usec_rem;
  1054. unsigned long long t;
  1055. unsigned long secs;
  1056. char *comm;
  1057. int ret;
  1058. int S;
  1059. int i;
  1060. entry = iter->ent;
  1061. comm = trace_find_cmdline(iter->ent->pid);
  1062. t = ns2usecs(entry->t);
  1063. usec_rem = do_div(t, 1000000ULL);
  1064. secs = (unsigned long)t;
  1065. if (entry->type != TRACE_STACK) {
  1066. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1067. if (!ret)
  1068. return 0;
  1069. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1070. if (!ret)
  1071. return 0;
  1072. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1073. if (!ret)
  1074. return 0;
  1075. }
  1076. switch (entry->type) {
  1077. case TRACE_FN:
  1078. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1079. if (!ret)
  1080. return 0;
  1081. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1082. entry->fn.parent_ip) {
  1083. ret = trace_seq_printf(s, " <-");
  1084. if (!ret)
  1085. return 0;
  1086. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1087. sym_flags);
  1088. if (!ret)
  1089. return 0;
  1090. }
  1091. ret = trace_seq_printf(s, "\n");
  1092. if (!ret)
  1093. return 0;
  1094. break;
  1095. case TRACE_CTX:
  1096. case TRACE_WAKE:
  1097. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1098. state_to_char[entry->ctx.prev_state] : 'X';
  1099. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
  1100. entry->ctx.prev_pid,
  1101. entry->ctx.prev_prio,
  1102. S,
  1103. entry->type == TRACE_CTX ? "==>" : " +",
  1104. entry->ctx.next_pid,
  1105. entry->ctx.next_prio);
  1106. if (!ret)
  1107. return 0;
  1108. break;
  1109. case TRACE_SPECIAL:
  1110. ret = trace_seq_printf(s, " %ld %ld %ld\n",
  1111. entry->special.arg1,
  1112. entry->special.arg2,
  1113. entry->special.arg3);
  1114. if (!ret)
  1115. return 0;
  1116. break;
  1117. case TRACE_STACK:
  1118. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1119. if (i) {
  1120. ret = trace_seq_puts(s, " <= ");
  1121. if (!ret)
  1122. return 0;
  1123. }
  1124. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1125. sym_flags);
  1126. if (!ret)
  1127. return 0;
  1128. }
  1129. ret = trace_seq_puts(s, "\n");
  1130. if (!ret)
  1131. return 0;
  1132. break;
  1133. }
  1134. return 1;
  1135. }
  1136. static int print_raw_fmt(struct trace_iterator *iter)
  1137. {
  1138. struct trace_seq *s = &iter->seq;
  1139. struct trace_entry *entry;
  1140. int ret;
  1141. int S;
  1142. entry = iter->ent;
  1143. ret = trace_seq_printf(s, "%d %d %llu ",
  1144. entry->pid, iter->cpu, entry->t);
  1145. if (!ret)
  1146. return 0;
  1147. switch (entry->type) {
  1148. case TRACE_FN:
  1149. ret = trace_seq_printf(s, "%x %x\n",
  1150. entry->fn.ip, entry->fn.parent_ip);
  1151. if (!ret)
  1152. return 0;
  1153. break;
  1154. case TRACE_CTX:
  1155. case TRACE_WAKE:
  1156. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1157. state_to_char[entry->ctx.prev_state] : 'X';
  1158. if (entry->type == TRACE_WAKE)
  1159. S = '+';
  1160. ret = trace_seq_printf(s, "%d %d %c %d %d\n",
  1161. entry->ctx.prev_pid,
  1162. entry->ctx.prev_prio,
  1163. S,
  1164. entry->ctx.next_pid,
  1165. entry->ctx.next_prio);
  1166. if (!ret)
  1167. return 0;
  1168. break;
  1169. case TRACE_SPECIAL:
  1170. case TRACE_STACK:
  1171. ret = trace_seq_printf(s, " %ld %ld %ld\n",
  1172. entry->special.arg1,
  1173. entry->special.arg2,
  1174. entry->special.arg3);
  1175. if (!ret)
  1176. return 0;
  1177. break;
  1178. }
  1179. return 1;
  1180. }
  1181. #define SEQ_PUT_FIELD_RET(s, x) \
  1182. do { \
  1183. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1184. return 0; \
  1185. } while (0)
  1186. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1187. do { \
  1188. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1189. return 0; \
  1190. } while (0)
  1191. static int print_hex_fmt(struct trace_iterator *iter)
  1192. {
  1193. struct trace_seq *s = &iter->seq;
  1194. unsigned char newline = '\n';
  1195. struct trace_entry *entry;
  1196. int S;
  1197. entry = iter->ent;
  1198. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1199. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1200. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1201. switch (entry->type) {
  1202. case TRACE_FN:
  1203. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1204. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1205. break;
  1206. case TRACE_CTX:
  1207. case TRACE_WAKE:
  1208. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1209. state_to_char[entry->ctx.prev_state] : 'X';
  1210. if (entry->type == TRACE_WAKE)
  1211. S = '+';
  1212. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1213. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1214. SEQ_PUT_HEX_FIELD_RET(s, S);
  1215. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1216. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1217. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1218. break;
  1219. case TRACE_SPECIAL:
  1220. case TRACE_STACK:
  1221. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1222. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1223. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1224. break;
  1225. }
  1226. SEQ_PUT_FIELD_RET(s, newline);
  1227. return 1;
  1228. }
  1229. static int print_bin_fmt(struct trace_iterator *iter)
  1230. {
  1231. struct trace_seq *s = &iter->seq;
  1232. struct trace_entry *entry;
  1233. entry = iter->ent;
  1234. SEQ_PUT_FIELD_RET(s, entry->pid);
  1235. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1236. SEQ_PUT_FIELD_RET(s, entry->t);
  1237. switch (entry->type) {
  1238. case TRACE_FN:
  1239. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1240. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1241. break;
  1242. case TRACE_CTX:
  1243. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1244. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1245. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1246. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1247. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1248. break;
  1249. case TRACE_SPECIAL:
  1250. case TRACE_STACK:
  1251. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1252. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1253. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1254. break;
  1255. }
  1256. return 1;
  1257. }
  1258. static int trace_empty(struct trace_iterator *iter)
  1259. {
  1260. struct trace_array_cpu *data;
  1261. int cpu;
  1262. for_each_possible_cpu(cpu) {
  1263. data = iter->tr->data[cpu];
  1264. if (head_page(data) && data->trace_idx &&
  1265. (data->trace_tail != data->trace_head ||
  1266. data->trace_tail_idx != data->trace_head_idx))
  1267. return 0;
  1268. }
  1269. return 1;
  1270. }
  1271. static int print_trace_line(struct trace_iterator *iter)
  1272. {
  1273. if (trace_flags & TRACE_ITER_BIN)
  1274. return print_bin_fmt(iter);
  1275. if (trace_flags & TRACE_ITER_HEX)
  1276. return print_hex_fmt(iter);
  1277. if (trace_flags & TRACE_ITER_RAW)
  1278. return print_raw_fmt(iter);
  1279. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1280. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1281. return print_trace_fmt(iter);
  1282. }
  1283. static int s_show(struct seq_file *m, void *v)
  1284. {
  1285. struct trace_iterator *iter = v;
  1286. if (iter->ent == NULL) {
  1287. if (iter->tr) {
  1288. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1289. seq_puts(m, "#\n");
  1290. }
  1291. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1292. /* print nothing if the buffers are empty */
  1293. if (trace_empty(iter))
  1294. return 0;
  1295. print_trace_header(m, iter);
  1296. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1297. print_lat_help_header(m);
  1298. } else {
  1299. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1300. print_func_help_header(m);
  1301. }
  1302. } else {
  1303. print_trace_line(iter);
  1304. trace_print_seq(m, &iter->seq);
  1305. }
  1306. return 0;
  1307. }
  1308. static struct seq_operations tracer_seq_ops = {
  1309. .start = s_start,
  1310. .next = s_next,
  1311. .stop = s_stop,
  1312. .show = s_show,
  1313. };
  1314. static struct trace_iterator *
  1315. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1316. {
  1317. struct trace_iterator *iter;
  1318. if (tracing_disabled) {
  1319. *ret = -ENODEV;
  1320. return NULL;
  1321. }
  1322. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1323. if (!iter) {
  1324. *ret = -ENOMEM;
  1325. goto out;
  1326. }
  1327. mutex_lock(&trace_types_lock);
  1328. if (current_trace && current_trace->print_max)
  1329. iter->tr = &max_tr;
  1330. else
  1331. iter->tr = inode->i_private;
  1332. iter->trace = current_trace;
  1333. iter->pos = -1;
  1334. /* TODO stop tracer */
  1335. *ret = seq_open(file, &tracer_seq_ops);
  1336. if (!*ret) {
  1337. struct seq_file *m = file->private_data;
  1338. m->private = iter;
  1339. /* stop the trace while dumping */
  1340. if (iter->tr->ctrl)
  1341. tracer_enabled = 0;
  1342. if (iter->trace && iter->trace->open)
  1343. iter->trace->open(iter);
  1344. } else {
  1345. kfree(iter);
  1346. iter = NULL;
  1347. }
  1348. mutex_unlock(&trace_types_lock);
  1349. out:
  1350. return iter;
  1351. }
  1352. int tracing_open_generic(struct inode *inode, struct file *filp)
  1353. {
  1354. if (tracing_disabled)
  1355. return -ENODEV;
  1356. filp->private_data = inode->i_private;
  1357. return 0;
  1358. }
  1359. int tracing_release(struct inode *inode, struct file *file)
  1360. {
  1361. struct seq_file *m = (struct seq_file *)file->private_data;
  1362. struct trace_iterator *iter = m->private;
  1363. mutex_lock(&trace_types_lock);
  1364. if (iter->trace && iter->trace->close)
  1365. iter->trace->close(iter);
  1366. /* reenable tracing if it was previously enabled */
  1367. if (iter->tr->ctrl)
  1368. tracer_enabled = 1;
  1369. mutex_unlock(&trace_types_lock);
  1370. seq_release(inode, file);
  1371. kfree(iter);
  1372. return 0;
  1373. }
  1374. static int tracing_open(struct inode *inode, struct file *file)
  1375. {
  1376. int ret;
  1377. __tracing_open(inode, file, &ret);
  1378. return ret;
  1379. }
  1380. static int tracing_lt_open(struct inode *inode, struct file *file)
  1381. {
  1382. struct trace_iterator *iter;
  1383. int ret;
  1384. iter = __tracing_open(inode, file, &ret);
  1385. if (!ret)
  1386. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1387. return ret;
  1388. }
  1389. static void *
  1390. t_next(struct seq_file *m, void *v, loff_t *pos)
  1391. {
  1392. struct tracer *t = m->private;
  1393. (*pos)++;
  1394. if (t)
  1395. t = t->next;
  1396. m->private = t;
  1397. return t;
  1398. }
  1399. static void *t_start(struct seq_file *m, loff_t *pos)
  1400. {
  1401. struct tracer *t = m->private;
  1402. loff_t l = 0;
  1403. mutex_lock(&trace_types_lock);
  1404. for (; t && l < *pos; t = t_next(m, t, &l))
  1405. ;
  1406. return t;
  1407. }
  1408. static void t_stop(struct seq_file *m, void *p)
  1409. {
  1410. mutex_unlock(&trace_types_lock);
  1411. }
  1412. static int t_show(struct seq_file *m, void *v)
  1413. {
  1414. struct tracer *t = v;
  1415. if (!t)
  1416. return 0;
  1417. seq_printf(m, "%s", t->name);
  1418. if (t->next)
  1419. seq_putc(m, ' ');
  1420. else
  1421. seq_putc(m, '\n');
  1422. return 0;
  1423. }
  1424. static struct seq_operations show_traces_seq_ops = {
  1425. .start = t_start,
  1426. .next = t_next,
  1427. .stop = t_stop,
  1428. .show = t_show,
  1429. };
  1430. static int show_traces_open(struct inode *inode, struct file *file)
  1431. {
  1432. int ret;
  1433. if (tracing_disabled)
  1434. return -ENODEV;
  1435. ret = seq_open(file, &show_traces_seq_ops);
  1436. if (!ret) {
  1437. struct seq_file *m = file->private_data;
  1438. m->private = trace_types;
  1439. }
  1440. return ret;
  1441. }
  1442. static struct file_operations tracing_fops = {
  1443. .open = tracing_open,
  1444. .read = seq_read,
  1445. .llseek = seq_lseek,
  1446. .release = tracing_release,
  1447. };
  1448. static struct file_operations tracing_lt_fops = {
  1449. .open = tracing_lt_open,
  1450. .read = seq_read,
  1451. .llseek = seq_lseek,
  1452. .release = tracing_release,
  1453. };
  1454. static struct file_operations show_traces_fops = {
  1455. .open = show_traces_open,
  1456. .read = seq_read,
  1457. .release = seq_release,
  1458. };
  1459. static ssize_t
  1460. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1461. size_t cnt, loff_t *ppos)
  1462. {
  1463. char *buf;
  1464. int r = 0;
  1465. int len = 0;
  1466. int i;
  1467. /* calulate max size */
  1468. for (i = 0; trace_options[i]; i++) {
  1469. len += strlen(trace_options[i]);
  1470. len += 3; /* "no" and space */
  1471. }
  1472. /* +2 for \n and \0 */
  1473. buf = kmalloc(len + 2, GFP_KERNEL);
  1474. if (!buf)
  1475. return -ENOMEM;
  1476. for (i = 0; trace_options[i]; i++) {
  1477. if (trace_flags & (1 << i))
  1478. r += sprintf(buf + r, "%s ", trace_options[i]);
  1479. else
  1480. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1481. }
  1482. r += sprintf(buf + r, "\n");
  1483. WARN_ON(r >= len + 2);
  1484. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1485. buf, r);
  1486. kfree(buf);
  1487. return r;
  1488. }
  1489. static ssize_t
  1490. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1491. size_t cnt, loff_t *ppos)
  1492. {
  1493. char buf[64];
  1494. char *cmp = buf;
  1495. int neg = 0;
  1496. int i;
  1497. if (cnt > 63)
  1498. cnt = 63;
  1499. if (copy_from_user(&buf, ubuf, cnt))
  1500. return -EFAULT;
  1501. buf[cnt] = 0;
  1502. if (strncmp(buf, "no", 2) == 0) {
  1503. neg = 1;
  1504. cmp += 2;
  1505. }
  1506. for (i = 0; trace_options[i]; i++) {
  1507. int len = strlen(trace_options[i]);
  1508. if (strncmp(cmp, trace_options[i], len) == 0) {
  1509. if (neg)
  1510. trace_flags &= ~(1 << i);
  1511. else
  1512. trace_flags |= (1 << i);
  1513. break;
  1514. }
  1515. }
  1516. filp->f_pos += cnt;
  1517. return cnt;
  1518. }
  1519. static struct file_operations tracing_iter_fops = {
  1520. .open = tracing_open_generic,
  1521. .read = tracing_iter_ctrl_read,
  1522. .write = tracing_iter_ctrl_write,
  1523. };
  1524. static const char readme_msg[] =
  1525. "tracing mini-HOWTO:\n\n"
  1526. "# mkdir /debug\n"
  1527. "# mount -t debugfs nodev /debug\n\n"
  1528. "# cat /debug/tracing/available_tracers\n"
  1529. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1530. "# cat /debug/tracing/current_tracer\n"
  1531. "none\n"
  1532. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1533. "# cat /debug/tracing/current_tracer\n"
  1534. "sched_switch\n"
  1535. "# cat /debug/tracing/iter_ctrl\n"
  1536. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1537. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1538. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1539. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1540. "echo 0 > /debug/tracing/tracing_enabled\n"
  1541. ;
  1542. static ssize_t
  1543. tracing_readme_read(struct file *filp, char __user *ubuf,
  1544. size_t cnt, loff_t *ppos)
  1545. {
  1546. return simple_read_from_buffer(ubuf, cnt, ppos,
  1547. readme_msg, strlen(readme_msg));
  1548. }
  1549. static struct file_operations tracing_readme_fops = {
  1550. .open = tracing_open_generic,
  1551. .read = tracing_readme_read,
  1552. };
  1553. static ssize_t
  1554. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1555. size_t cnt, loff_t *ppos)
  1556. {
  1557. struct trace_array *tr = filp->private_data;
  1558. char buf[64];
  1559. int r;
  1560. r = sprintf(buf, "%ld\n", tr->ctrl);
  1561. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1562. }
  1563. static ssize_t
  1564. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1565. size_t cnt, loff_t *ppos)
  1566. {
  1567. struct trace_array *tr = filp->private_data;
  1568. long val;
  1569. char buf[64];
  1570. if (cnt > 63)
  1571. cnt = 63;
  1572. if (copy_from_user(&buf, ubuf, cnt))
  1573. return -EFAULT;
  1574. buf[cnt] = 0;
  1575. val = simple_strtoul(buf, NULL, 10);
  1576. val = !!val;
  1577. mutex_lock(&trace_types_lock);
  1578. if (tr->ctrl ^ val) {
  1579. if (val)
  1580. tracer_enabled = 1;
  1581. else
  1582. tracer_enabled = 0;
  1583. tr->ctrl = val;
  1584. if (current_trace && current_trace->ctrl_update)
  1585. current_trace->ctrl_update(tr);
  1586. }
  1587. mutex_unlock(&trace_types_lock);
  1588. filp->f_pos += cnt;
  1589. return cnt;
  1590. }
  1591. static ssize_t
  1592. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1593. size_t cnt, loff_t *ppos)
  1594. {
  1595. char buf[max_tracer_type_len+2];
  1596. int r;
  1597. mutex_lock(&trace_types_lock);
  1598. if (current_trace)
  1599. r = sprintf(buf, "%s\n", current_trace->name);
  1600. else
  1601. r = sprintf(buf, "\n");
  1602. mutex_unlock(&trace_types_lock);
  1603. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1604. }
  1605. static ssize_t
  1606. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1607. size_t cnt, loff_t *ppos)
  1608. {
  1609. struct trace_array *tr = &global_trace;
  1610. struct tracer *t;
  1611. char buf[max_tracer_type_len+1];
  1612. int i;
  1613. if (cnt > max_tracer_type_len)
  1614. cnt = max_tracer_type_len;
  1615. if (copy_from_user(&buf, ubuf, cnt))
  1616. return -EFAULT;
  1617. buf[cnt] = 0;
  1618. /* strip ending whitespace. */
  1619. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1620. buf[i] = 0;
  1621. mutex_lock(&trace_types_lock);
  1622. for (t = trace_types; t; t = t->next) {
  1623. if (strcmp(t->name, buf) == 0)
  1624. break;
  1625. }
  1626. if (!t || t == current_trace)
  1627. goto out;
  1628. if (current_trace && current_trace->reset)
  1629. current_trace->reset(tr);
  1630. current_trace = t;
  1631. if (t->init)
  1632. t->init(tr);
  1633. out:
  1634. mutex_unlock(&trace_types_lock);
  1635. filp->f_pos += cnt;
  1636. return cnt;
  1637. }
  1638. static ssize_t
  1639. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1640. size_t cnt, loff_t *ppos)
  1641. {
  1642. unsigned long *ptr = filp->private_data;
  1643. char buf[64];
  1644. int r;
  1645. r = snprintf(buf, 64, "%ld\n",
  1646. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1647. if (r > 64)
  1648. r = 64;
  1649. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1650. }
  1651. static ssize_t
  1652. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1653. size_t cnt, loff_t *ppos)
  1654. {
  1655. long *ptr = filp->private_data;
  1656. long val;
  1657. char buf[64];
  1658. if (cnt > 63)
  1659. cnt = 63;
  1660. if (copy_from_user(&buf, ubuf, cnt))
  1661. return -EFAULT;
  1662. buf[cnt] = 0;
  1663. val = simple_strtoul(buf, NULL, 10);
  1664. *ptr = val * 1000;
  1665. return cnt;
  1666. }
  1667. static atomic_t tracing_reader;
  1668. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1669. {
  1670. struct trace_iterator *iter;
  1671. if (tracing_disabled)
  1672. return -ENODEV;
  1673. /* We only allow for reader of the pipe */
  1674. if (atomic_inc_return(&tracing_reader) != 1) {
  1675. atomic_dec(&tracing_reader);
  1676. return -EBUSY;
  1677. }
  1678. /* create a buffer to store the information to pass to userspace */
  1679. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1680. if (!iter)
  1681. return -ENOMEM;
  1682. iter->tr = &global_trace;
  1683. filp->private_data = iter;
  1684. return 0;
  1685. }
  1686. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1687. {
  1688. struct trace_iterator *iter = file->private_data;
  1689. kfree(iter);
  1690. atomic_dec(&tracing_reader);
  1691. return 0;
  1692. }
  1693. static unsigned int
  1694. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1695. {
  1696. struct trace_iterator *iter = filp->private_data;
  1697. if (trace_flags & TRACE_ITER_BLOCK) {
  1698. /*
  1699. * Always select as readable when in blocking mode
  1700. */
  1701. return POLLIN | POLLRDNORM;
  1702. }
  1703. else {
  1704. if (!trace_empty(iter))
  1705. return POLLIN | POLLRDNORM;
  1706. poll_wait(filp, &trace_wait, poll_table);
  1707. if (!trace_empty(iter))
  1708. return POLLIN | POLLRDNORM;
  1709. return 0;
  1710. }
  1711. }
  1712. /*
  1713. * Consumer reader.
  1714. */
  1715. static ssize_t
  1716. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1717. size_t cnt, loff_t *ppos)
  1718. {
  1719. struct trace_iterator *iter = filp->private_data;
  1720. struct trace_array_cpu *data;
  1721. static cpumask_t mask;
  1722. static int start;
  1723. unsigned long flags;
  1724. #ifdef CONFIG_FTRACE
  1725. int ftrace_save;
  1726. #endif
  1727. int read = 0;
  1728. int cpu;
  1729. int len;
  1730. int ret;
  1731. /* return any leftover data */
  1732. if (iter->seq.len > start) {
  1733. len = iter->seq.len - start;
  1734. if (cnt > len)
  1735. cnt = len;
  1736. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1737. if (ret)
  1738. cnt = -EFAULT;
  1739. start += len;
  1740. return cnt;
  1741. }
  1742. trace_seq_reset(&iter->seq);
  1743. start = 0;
  1744. while (trace_empty(iter)) {
  1745. if (!(trace_flags & TRACE_ITER_BLOCK))
  1746. return -EWOULDBLOCK;
  1747. /*
  1748. * This is a make-shift waitqueue. The reason we don't use
  1749. * an actual wait queue is because:
  1750. * 1) we only ever have one waiter
  1751. * 2) the tracing, traces all functions, we don't want
  1752. * the overhead of calling wake_up and friends
  1753. * (and tracing them too)
  1754. * Anyway, this is really very primitive wakeup.
  1755. */
  1756. set_current_state(TASK_INTERRUPTIBLE);
  1757. iter->tr->waiter = current;
  1758. /* sleep for one second, and try again. */
  1759. schedule_timeout(HZ);
  1760. iter->tr->waiter = NULL;
  1761. if (signal_pending(current))
  1762. return -EINTR;
  1763. /*
  1764. * We block until we read something and tracing is disabled.
  1765. * We still block if tracing is disabled, but we have never
  1766. * read anything. This allows a user to cat this file, and
  1767. * then enable tracing. But after we have read something,
  1768. * we give an EOF when tracing is again disabled.
  1769. *
  1770. * iter->pos will be 0 if we haven't read anything.
  1771. */
  1772. if (!tracer_enabled && iter->pos)
  1773. break;
  1774. continue;
  1775. }
  1776. /* stop when tracing is finished */
  1777. if (trace_empty(iter))
  1778. return 0;
  1779. if (cnt >= PAGE_SIZE)
  1780. cnt = PAGE_SIZE - 1;
  1781. memset(iter, 0, sizeof(*iter));
  1782. iter->tr = &global_trace;
  1783. iter->pos = -1;
  1784. /*
  1785. * We need to stop all tracing on all CPUS to read the
  1786. * the next buffer. This is a bit expensive, but is
  1787. * not done often. We fill all what we can read,
  1788. * and then release the locks again.
  1789. */
  1790. cpus_clear(mask);
  1791. local_irq_save(flags);
  1792. #ifdef CONFIG_FTRACE
  1793. ftrace_save = ftrace_enabled;
  1794. ftrace_enabled = 0;
  1795. #endif
  1796. smp_wmb();
  1797. for_each_possible_cpu(cpu) {
  1798. data = iter->tr->data[cpu];
  1799. if (!head_page(data) || !data->trace_idx)
  1800. continue;
  1801. atomic_inc(&data->disabled);
  1802. cpu_set(cpu, mask);
  1803. }
  1804. for_each_cpu_mask(cpu, mask) {
  1805. data = iter->tr->data[cpu];
  1806. spin_lock(&data->lock);
  1807. }
  1808. while (find_next_entry_inc(iter) != NULL) {
  1809. int len = iter->seq.len;
  1810. ret = print_trace_line(iter);
  1811. if (!ret) {
  1812. /* don't print partial lines */
  1813. iter->seq.len = len;
  1814. break;
  1815. }
  1816. trace_consume(iter);
  1817. if (iter->seq.len >= cnt)
  1818. break;
  1819. }
  1820. for_each_cpu_mask(cpu, mask) {
  1821. data = iter->tr->data[cpu];
  1822. spin_unlock(&data->lock);
  1823. }
  1824. for_each_cpu_mask(cpu, mask) {
  1825. data = iter->tr->data[cpu];
  1826. atomic_dec(&data->disabled);
  1827. }
  1828. #ifdef CONFIG_FTRACE
  1829. ftrace_enabled = ftrace_save;
  1830. #endif
  1831. local_irq_restore(flags);
  1832. /* Now copy what we have to the user */
  1833. read = iter->seq.len;
  1834. if (read > cnt)
  1835. read = cnt;
  1836. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1837. if (read < iter->seq.len)
  1838. start = read;
  1839. else
  1840. trace_seq_reset(&iter->seq);
  1841. if (ret)
  1842. read = -EFAULT;
  1843. return read;
  1844. }
  1845. static struct file_operations tracing_max_lat_fops = {
  1846. .open = tracing_open_generic,
  1847. .read = tracing_max_lat_read,
  1848. .write = tracing_max_lat_write,
  1849. };
  1850. static struct file_operations tracing_ctrl_fops = {
  1851. .open = tracing_open_generic,
  1852. .read = tracing_ctrl_read,
  1853. .write = tracing_ctrl_write,
  1854. };
  1855. static struct file_operations set_tracer_fops = {
  1856. .open = tracing_open_generic,
  1857. .read = tracing_set_trace_read,
  1858. .write = tracing_set_trace_write,
  1859. };
  1860. static struct file_operations tracing_pipe_fops = {
  1861. .open = tracing_open_pipe,
  1862. .poll = tracing_poll_pipe,
  1863. .read = tracing_read_pipe,
  1864. .release = tracing_release_pipe,
  1865. };
  1866. #ifdef CONFIG_DYNAMIC_FTRACE
  1867. static ssize_t
  1868. tracing_read_long(struct file *filp, char __user *ubuf,
  1869. size_t cnt, loff_t *ppos)
  1870. {
  1871. unsigned long *p = filp->private_data;
  1872. char buf[64];
  1873. int r;
  1874. r = sprintf(buf, "%ld\n", *p);
  1875. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1876. }
  1877. static struct file_operations tracing_read_long_fops = {
  1878. .open = tracing_open_generic,
  1879. .read = tracing_read_long,
  1880. };
  1881. #endif
  1882. static struct dentry *d_tracer;
  1883. struct dentry *tracing_init_dentry(void)
  1884. {
  1885. static int once;
  1886. if (d_tracer)
  1887. return d_tracer;
  1888. d_tracer = debugfs_create_dir("tracing", NULL);
  1889. if (!d_tracer && !once) {
  1890. once = 1;
  1891. pr_warning("Could not create debugfs directory 'tracing'\n");
  1892. return NULL;
  1893. }
  1894. return d_tracer;
  1895. }
  1896. #ifdef CONFIG_FTRACE_SELFTEST
  1897. /* Let selftest have access to static functions in this file */
  1898. #include "trace_selftest.c"
  1899. #endif
  1900. static __init void tracer_init_debugfs(void)
  1901. {
  1902. struct dentry *d_tracer;
  1903. struct dentry *entry;
  1904. d_tracer = tracing_init_dentry();
  1905. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1906. &global_trace, &tracing_ctrl_fops);
  1907. if (!entry)
  1908. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1909. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1910. NULL, &tracing_iter_fops);
  1911. if (!entry)
  1912. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1913. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1914. &global_trace, &tracing_lt_fops);
  1915. if (!entry)
  1916. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1917. entry = debugfs_create_file("trace", 0444, d_tracer,
  1918. &global_trace, &tracing_fops);
  1919. if (!entry)
  1920. pr_warning("Could not create debugfs 'trace' entry\n");
  1921. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1922. &global_trace, &show_traces_fops);
  1923. if (!entry)
  1924. pr_warning("Could not create debugfs 'trace' entry\n");
  1925. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1926. &global_trace, &set_tracer_fops);
  1927. if (!entry)
  1928. pr_warning("Could not create debugfs 'trace' entry\n");
  1929. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1930. &tracing_max_latency,
  1931. &tracing_max_lat_fops);
  1932. if (!entry)
  1933. pr_warning("Could not create debugfs "
  1934. "'tracing_max_latency' entry\n");
  1935. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1936. &tracing_thresh, &tracing_max_lat_fops);
  1937. if (!entry)
  1938. pr_warning("Could not create debugfs "
  1939. "'tracing_threash' entry\n");
  1940. entry = debugfs_create_file("README", 0644, d_tracer,
  1941. NULL, &tracing_readme_fops);
  1942. if (!entry)
  1943. pr_warning("Could not create debugfs 'README' entry\n");
  1944. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  1945. NULL, &tracing_pipe_fops);
  1946. if (!entry)
  1947. pr_warning("Could not create debugfs "
  1948. "'tracing_threash' entry\n");
  1949. #ifdef CONFIG_DYNAMIC_FTRACE
  1950. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1951. &ftrace_update_tot_cnt,
  1952. &tracing_read_long_fops);
  1953. if (!entry)
  1954. pr_warning("Could not create debugfs "
  1955. "'dyn_ftrace_total_info' entry\n");
  1956. #endif
  1957. }
  1958. /* dummy trace to disable tracing */
  1959. static struct tracer no_tracer __read_mostly =
  1960. {
  1961. .name = "none",
  1962. };
  1963. static int trace_alloc_page(void)
  1964. {
  1965. struct trace_array_cpu *data;
  1966. struct page *page, *tmp;
  1967. LIST_HEAD(pages);
  1968. void *array;
  1969. int i;
  1970. /* first allocate a page for each CPU */
  1971. for_each_possible_cpu(i) {
  1972. array = (void *)__get_free_page(GFP_KERNEL);
  1973. if (array == NULL) {
  1974. printk(KERN_ERR "tracer: failed to allocate page"
  1975. "for trace buffer!\n");
  1976. goto free_pages;
  1977. }
  1978. page = virt_to_page(array);
  1979. list_add(&page->lru, &pages);
  1980. /* Only allocate if we are actually using the max trace */
  1981. #ifdef CONFIG_TRACER_MAX_TRACE
  1982. array = (void *)__get_free_page(GFP_KERNEL);
  1983. if (array == NULL) {
  1984. printk(KERN_ERR "tracer: failed to allocate page"
  1985. "for trace buffer!\n");
  1986. goto free_pages;
  1987. }
  1988. page = virt_to_page(array);
  1989. list_add(&page->lru, &pages);
  1990. #endif
  1991. }
  1992. /* Now that we successfully allocate a page per CPU, add them */
  1993. for_each_possible_cpu(i) {
  1994. data = global_trace.data[i];
  1995. spin_lock_init(&data->lock);
  1996. lockdep_set_class(&data->lock, &data->lock_key);
  1997. page = list_entry(pages.next, struct page, lru);
  1998. list_del_init(&page->lru);
  1999. list_add_tail(&page->lru, &data->trace_pages);
  2000. ClearPageLRU(page);
  2001. #ifdef CONFIG_TRACER_MAX_TRACE
  2002. data = max_tr.data[i];
  2003. spin_lock_init(&data->lock);
  2004. lockdep_set_class(&data->lock, &data->lock_key);
  2005. page = list_entry(pages.next, struct page, lru);
  2006. list_del_init(&page->lru);
  2007. list_add_tail(&page->lru, &data->trace_pages);
  2008. SetPageLRU(page);
  2009. #endif
  2010. }
  2011. global_trace.entries += ENTRIES_PER_PAGE;
  2012. return 0;
  2013. free_pages:
  2014. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2015. list_del_init(&page->lru);
  2016. __free_page(page);
  2017. }
  2018. return -ENOMEM;
  2019. }
  2020. __init static int tracer_alloc_buffers(void)
  2021. {
  2022. struct trace_array_cpu *data;
  2023. void *array;
  2024. struct page *page;
  2025. int pages = 0;
  2026. int ret = -ENOMEM;
  2027. int i;
  2028. global_trace.ctrl = tracer_enabled;
  2029. /* Allocate the first page for all buffers */
  2030. for_each_possible_cpu(i) {
  2031. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2032. max_tr.data[i] = &per_cpu(max_data, i);
  2033. array = (void *)__get_free_page(GFP_KERNEL);
  2034. if (array == NULL) {
  2035. printk(KERN_ERR "tracer: failed to allocate page"
  2036. "for trace buffer!\n");
  2037. goto free_buffers;
  2038. }
  2039. /* set the array to the list */
  2040. INIT_LIST_HEAD(&data->trace_pages);
  2041. page = virt_to_page(array);
  2042. list_add(&page->lru, &data->trace_pages);
  2043. /* use the LRU flag to differentiate the two buffers */
  2044. ClearPageLRU(page);
  2045. /* Only allocate if we are actually using the max trace */
  2046. #ifdef CONFIG_TRACER_MAX_TRACE
  2047. array = (void *)__get_free_page(GFP_KERNEL);
  2048. if (array == NULL) {
  2049. printk(KERN_ERR "tracer: failed to allocate page"
  2050. "for trace buffer!\n");
  2051. goto free_buffers;
  2052. }
  2053. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2054. page = virt_to_page(array);
  2055. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2056. SetPageLRU(page);
  2057. #endif
  2058. }
  2059. /*
  2060. * Since we allocate by orders of pages, we may be able to
  2061. * round up a bit.
  2062. */
  2063. global_trace.entries = ENTRIES_PER_PAGE;
  2064. pages++;
  2065. while (global_trace.entries < trace_nr_entries) {
  2066. if (trace_alloc_page())
  2067. break;
  2068. pages++;
  2069. }
  2070. max_tr.entries = global_trace.entries;
  2071. pr_info("tracer: %d pages allocated for %ld",
  2072. pages, trace_nr_entries);
  2073. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2074. pr_info(" actual entries %ld\n", global_trace.entries);
  2075. tracer_init_debugfs();
  2076. trace_init_cmdlines();
  2077. register_tracer(&no_tracer);
  2078. current_trace = &no_tracer;
  2079. /* All seems OK, enable tracing */
  2080. tracing_disabled = 0;
  2081. return 0;
  2082. free_buffers:
  2083. for (i-- ; i >= 0; i--) {
  2084. struct page *page, *tmp;
  2085. struct trace_array_cpu *data = global_trace.data[i];
  2086. if (data) {
  2087. list_for_each_entry_safe(page, tmp,
  2088. &data->trace_pages, lru) {
  2089. list_del_init(&page->lru);
  2090. __free_page(page);
  2091. }
  2092. }
  2093. #ifdef CONFIG_TRACER_MAX_TRACE
  2094. data = max_tr.data[i];
  2095. if (data) {
  2096. list_for_each_entry_safe(page, tmp,
  2097. &data->trace_pages, lru) {
  2098. list_del_init(&page->lru);
  2099. __free_page(page);
  2100. }
  2101. }
  2102. #endif
  2103. }
  2104. return ret;
  2105. }
  2106. fs_initcall(tracer_alloc_buffers);