trace.c 59 KB

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