trace.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  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. /* dummy trace to disable tracing */
  35. static struct tracer no_tracer __read_mostly =
  36. {
  37. .name = "none",
  38. };
  39. static int trace_alloc_page(void);
  40. static int trace_free_page(void);
  41. static int tracing_disabled = 1;
  42. long
  43. ns2usecs(cycle_t nsec)
  44. {
  45. nsec += 500;
  46. do_div(nsec, 1000);
  47. return nsec;
  48. }
  49. cycle_t ftrace_now(int cpu)
  50. {
  51. return cpu_clock(cpu);
  52. }
  53. static struct trace_array global_trace;
  54. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  55. static struct trace_array max_tr;
  56. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  57. static int tracer_enabled = 1;
  58. static unsigned long trace_nr_entries = 65536UL;
  59. static struct tracer *trace_types __read_mostly;
  60. static struct tracer *current_trace __read_mostly;
  61. static int max_tracer_type_len;
  62. static DEFINE_MUTEX(trace_types_lock);
  63. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  64. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  65. void trace_wake_up(void)
  66. {
  67. /*
  68. * The runqueue_is_locked() can fail, but this is the best we
  69. * have for now:
  70. */
  71. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  72. wake_up(&trace_wait);
  73. }
  74. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  75. static int __init set_nr_entries(char *str)
  76. {
  77. if (!str)
  78. return 0;
  79. trace_nr_entries = simple_strtoul(str, &str, 0);
  80. return 1;
  81. }
  82. __setup("trace_entries=", set_nr_entries);
  83. unsigned long nsecs_to_usecs(unsigned long nsecs)
  84. {
  85. return nsecs / 1000;
  86. }
  87. enum trace_flag_type {
  88. TRACE_FLAG_IRQS_OFF = 0x01,
  89. TRACE_FLAG_NEED_RESCHED = 0x02,
  90. TRACE_FLAG_HARDIRQ = 0x04,
  91. TRACE_FLAG_SOFTIRQ = 0x08,
  92. };
  93. #define TRACE_ITER_SYM_MASK \
  94. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  95. /* These must match the bit postions above */
  96. static const char *trace_options[] = {
  97. "print-parent",
  98. "sym-offset",
  99. "sym-addr",
  100. "verbose",
  101. "raw",
  102. "hex",
  103. "bin",
  104. "block",
  105. "stacktrace",
  106. "sched-tree",
  107. NULL
  108. };
  109. static raw_spinlock_t ftrace_max_lock =
  110. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  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. 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 const char hex2asc[] = "0123456789abcdef";
  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 = mem;
  204. unsigned char byte;
  205. int i, j;
  206. BUG_ON(len >= HEX_CHARS);
  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++] = hex2asc[byte & 0x0f];
  214. hex[j++] = hex2asc[byte >> 4];
  215. }
  216. hex[j++] = ' ';
  217. return trace_seq_putmem(s, hex, j);
  218. }
  219. static void
  220. trace_seq_reset(struct trace_seq *s)
  221. {
  222. s->len = 0;
  223. }
  224. static void
  225. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  226. {
  227. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  228. s->buffer[len] = 0;
  229. seq_puts(m, s->buffer);
  230. trace_seq_reset(s);
  231. }
  232. static void
  233. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  234. {
  235. struct list_head flip_pages;
  236. INIT_LIST_HEAD(&flip_pages);
  237. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  238. sizeof(struct trace_array_cpu) -
  239. offsetof(struct trace_array_cpu, trace_head_idx));
  240. check_pages(tr1);
  241. check_pages(tr2);
  242. list_splice_init(&tr1->trace_pages, &flip_pages);
  243. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  244. list_splice_init(&flip_pages, &tr2->trace_pages);
  245. BUG_ON(!list_empty(&flip_pages));
  246. check_pages(tr1);
  247. check_pages(tr2);
  248. }
  249. void
  250. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  251. {
  252. struct trace_array_cpu *data;
  253. int i;
  254. WARN_ON_ONCE(!irqs_disabled());
  255. __raw_spin_lock(&ftrace_max_lock);
  256. /* clear out all the previous traces */
  257. for_each_possible_cpu(i) {
  258. data = tr->data[i];
  259. flip_trace(max_tr.data[i], data);
  260. tracing_reset(data);
  261. }
  262. __update_max_tr(tr, tsk, cpu);
  263. __raw_spin_unlock(&ftrace_max_lock);
  264. }
  265. /**
  266. * update_max_tr_single - only copy one trace over, and reset the rest
  267. * @tr - tracer
  268. * @tsk - task with the latency
  269. * @cpu - the cpu of the buffer to copy.
  270. */
  271. void
  272. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  273. {
  274. struct trace_array_cpu *data = tr->data[cpu];
  275. int i;
  276. WARN_ON_ONCE(!irqs_disabled());
  277. __raw_spin_lock(&ftrace_max_lock);
  278. for_each_possible_cpu(i)
  279. tracing_reset(max_tr.data[i]);
  280. flip_trace(max_tr.data[cpu], data);
  281. tracing_reset(data);
  282. __update_max_tr(tr, tsk, cpu);
  283. __raw_spin_unlock(&ftrace_max_lock);
  284. }
  285. int register_tracer(struct tracer *type)
  286. {
  287. struct tracer *t;
  288. int len;
  289. int ret = 0;
  290. if (!type->name) {
  291. pr_info("Tracer must have a name\n");
  292. return -1;
  293. }
  294. mutex_lock(&trace_types_lock);
  295. for (t = trace_types; t; t = t->next) {
  296. if (strcmp(type->name, t->name) == 0) {
  297. /* already found */
  298. pr_info("Trace %s already registered\n",
  299. type->name);
  300. ret = -1;
  301. goto out;
  302. }
  303. }
  304. #ifdef CONFIG_FTRACE_STARTUP_TEST
  305. if (type->selftest) {
  306. struct tracer *saved_tracer = current_trace;
  307. struct trace_array_cpu *data;
  308. struct trace_array *tr = &global_trace;
  309. int saved_ctrl = tr->ctrl;
  310. int i;
  311. /*
  312. * Run a selftest on this tracer.
  313. * Here we reset the trace buffer, and set the current
  314. * tracer to be this tracer. The tracer can then run some
  315. * internal tracing to verify that everything is in order.
  316. * If we fail, we do not register this tracer.
  317. */
  318. for_each_possible_cpu(i) {
  319. data = tr->data[i];
  320. if (!head_page(data))
  321. continue;
  322. tracing_reset(data);
  323. }
  324. current_trace = type;
  325. tr->ctrl = 0;
  326. /* the test is responsible for initializing and enabling */
  327. pr_info("Testing tracer %s: ", type->name);
  328. ret = type->selftest(type, tr);
  329. /* the test is responsible for resetting too */
  330. current_trace = saved_tracer;
  331. tr->ctrl = saved_ctrl;
  332. if (ret) {
  333. printk(KERN_CONT "FAILED!\n");
  334. goto out;
  335. }
  336. /* Only reset on passing, to avoid touching corrupted buffers */
  337. for_each_possible_cpu(i) {
  338. data = tr->data[i];
  339. if (!head_page(data))
  340. continue;
  341. tracing_reset(data);
  342. }
  343. printk(KERN_CONT "PASSED\n");
  344. }
  345. #endif
  346. type->next = trace_types;
  347. trace_types = type;
  348. len = strlen(type->name);
  349. if (len > max_tracer_type_len)
  350. max_tracer_type_len = len;
  351. out:
  352. mutex_unlock(&trace_types_lock);
  353. return ret;
  354. }
  355. void unregister_tracer(struct tracer *type)
  356. {
  357. struct tracer **t;
  358. int len;
  359. mutex_lock(&trace_types_lock);
  360. for (t = &trace_types; *t; t = &(*t)->next) {
  361. if (*t == type)
  362. goto found;
  363. }
  364. pr_info("Trace %s not registered\n", type->name);
  365. goto out;
  366. found:
  367. *t = (*t)->next;
  368. if (strlen(type->name) != max_tracer_type_len)
  369. goto out;
  370. max_tracer_type_len = 0;
  371. for (t = &trace_types; *t; t = &(*t)->next) {
  372. len = strlen((*t)->name);
  373. if (len > max_tracer_type_len)
  374. max_tracer_type_len = len;
  375. }
  376. out:
  377. mutex_unlock(&trace_types_lock);
  378. }
  379. void tracing_reset(struct trace_array_cpu *data)
  380. {
  381. data->trace_idx = 0;
  382. data->trace_head = data->trace_tail = head_page(data);
  383. data->trace_head_idx = 0;
  384. data->trace_tail_idx = 0;
  385. }
  386. #define SAVED_CMDLINES 128
  387. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  388. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  389. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  390. static int cmdline_idx;
  391. static DEFINE_SPINLOCK(trace_cmdline_lock);
  392. atomic_t trace_record_cmdline_disabled;
  393. static void trace_init_cmdlines(void)
  394. {
  395. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  396. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  397. cmdline_idx = 0;
  398. }
  399. void trace_stop_cmdline_recording(void);
  400. static void trace_save_cmdline(struct task_struct *tsk)
  401. {
  402. unsigned map;
  403. unsigned idx;
  404. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  405. return;
  406. /*
  407. * It's not the end of the world if we don't get
  408. * the lock, but we also don't want to spin
  409. * nor do we want to disable interrupts,
  410. * so if we miss here, then better luck next time.
  411. */
  412. if (!spin_trylock(&trace_cmdline_lock))
  413. return;
  414. idx = map_pid_to_cmdline[tsk->pid];
  415. if (idx >= SAVED_CMDLINES) {
  416. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  417. map = map_cmdline_to_pid[idx];
  418. if (map <= PID_MAX_DEFAULT)
  419. map_pid_to_cmdline[map] = (unsigned)-1;
  420. map_pid_to_cmdline[tsk->pid] = idx;
  421. cmdline_idx = idx;
  422. }
  423. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  424. spin_unlock(&trace_cmdline_lock);
  425. }
  426. static char *trace_find_cmdline(int pid)
  427. {
  428. char *cmdline = "<...>";
  429. unsigned map;
  430. if (!pid)
  431. return "<idle>";
  432. if (pid > PID_MAX_DEFAULT)
  433. goto out;
  434. map = map_pid_to_cmdline[pid];
  435. if (map >= SAVED_CMDLINES)
  436. goto out;
  437. cmdline = saved_cmdlines[map];
  438. out:
  439. return cmdline;
  440. }
  441. void tracing_record_cmdline(struct task_struct *tsk)
  442. {
  443. if (atomic_read(&trace_record_cmdline_disabled))
  444. return;
  445. trace_save_cmdline(tsk);
  446. }
  447. static inline struct list_head *
  448. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  449. {
  450. /*
  451. * Roundrobin - but skip the head (which is not a real page):
  452. */
  453. next = next->next;
  454. if (unlikely(next == &data->trace_pages))
  455. next = next->next;
  456. BUG_ON(next == &data->trace_pages);
  457. return next;
  458. }
  459. static inline void *
  460. trace_next_page(struct trace_array_cpu *data, void *addr)
  461. {
  462. struct list_head *next;
  463. struct page *page;
  464. page = virt_to_page(addr);
  465. next = trace_next_list(data, &page->lru);
  466. page = list_entry(next, struct page, lru);
  467. return page_address(page);
  468. }
  469. static inline struct trace_entry *
  470. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  471. {
  472. unsigned long idx, idx_next;
  473. struct trace_entry *entry;
  474. data->trace_idx++;
  475. idx = data->trace_head_idx;
  476. idx_next = idx + 1;
  477. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  478. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  479. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  480. data->trace_head = trace_next_page(data, data->trace_head);
  481. idx_next = 0;
  482. }
  483. if (data->trace_head == data->trace_tail &&
  484. idx_next == data->trace_tail_idx) {
  485. /* overrun */
  486. data->trace_tail_idx++;
  487. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  488. data->trace_tail =
  489. trace_next_page(data, data->trace_tail);
  490. data->trace_tail_idx = 0;
  491. }
  492. }
  493. data->trace_head_idx = idx_next;
  494. return entry;
  495. }
  496. static inline void
  497. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  498. {
  499. struct task_struct *tsk = current;
  500. unsigned long pc;
  501. pc = preempt_count();
  502. entry->preempt_count = pc & 0xff;
  503. entry->pid = (tsk) ? tsk->pid : 0;
  504. entry->t = ftrace_now(raw_smp_processor_id());
  505. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  506. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  507. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  508. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  509. }
  510. void
  511. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  512. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  513. {
  514. struct trace_entry *entry;
  515. unsigned long irq_flags;
  516. raw_local_irq_save(irq_flags);
  517. __raw_spin_lock(&data->lock);
  518. entry = tracing_get_trace_entry(tr, data);
  519. tracing_generic_entry_update(entry, flags);
  520. entry->type = TRACE_FN;
  521. entry->fn.ip = ip;
  522. entry->fn.parent_ip = parent_ip;
  523. __raw_spin_unlock(&data->lock);
  524. raw_local_irq_restore(irq_flags);
  525. }
  526. void
  527. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  528. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  529. {
  530. if (likely(!atomic_read(&data->disabled)))
  531. trace_function(tr, data, ip, parent_ip, flags);
  532. }
  533. void
  534. __trace_special(void *__tr, void *__data,
  535. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  536. {
  537. struct trace_array_cpu *data = __data;
  538. struct trace_array *tr = __tr;
  539. struct trace_entry *entry;
  540. unsigned long irq_flags;
  541. raw_local_irq_save(irq_flags);
  542. __raw_spin_lock(&data->lock);
  543. entry = tracing_get_trace_entry(tr, data);
  544. tracing_generic_entry_update(entry, 0);
  545. entry->type = TRACE_SPECIAL;
  546. entry->special.arg1 = arg1;
  547. entry->special.arg2 = arg2;
  548. entry->special.arg3 = arg3;
  549. __raw_spin_unlock(&data->lock);
  550. raw_local_irq_restore(irq_flags);
  551. trace_wake_up();
  552. }
  553. void __trace_stack(struct trace_array *tr,
  554. struct trace_array_cpu *data,
  555. unsigned long flags,
  556. int skip)
  557. {
  558. struct trace_entry *entry;
  559. struct stack_trace trace;
  560. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  561. return;
  562. entry = tracing_get_trace_entry(tr, data);
  563. tracing_generic_entry_update(entry, flags);
  564. entry->type = TRACE_STACK;
  565. memset(&entry->stack, 0, sizeof(entry->stack));
  566. trace.nr_entries = 0;
  567. trace.max_entries = FTRACE_STACK_ENTRIES;
  568. trace.skip = skip;
  569. trace.entries = entry->stack.caller;
  570. save_stack_trace(&trace);
  571. }
  572. void
  573. tracing_sched_switch_trace(struct trace_array *tr,
  574. struct trace_array_cpu *data,
  575. struct task_struct *prev,
  576. struct task_struct *next,
  577. unsigned long flags)
  578. {
  579. struct trace_entry *entry;
  580. unsigned long irq_flags;
  581. raw_local_irq_save(irq_flags);
  582. __raw_spin_lock(&data->lock);
  583. entry = tracing_get_trace_entry(tr, data);
  584. tracing_generic_entry_update(entry, flags);
  585. entry->type = TRACE_CTX;
  586. entry->ctx.prev_pid = prev->pid;
  587. entry->ctx.prev_prio = prev->prio;
  588. entry->ctx.prev_state = prev->state;
  589. entry->ctx.next_pid = next->pid;
  590. entry->ctx.next_prio = next->prio;
  591. entry->ctx.next_state = next->state;
  592. __trace_stack(tr, data, flags, 4);
  593. __raw_spin_unlock(&data->lock);
  594. raw_local_irq_restore(irq_flags);
  595. }
  596. void
  597. tracing_sched_wakeup_trace(struct trace_array *tr,
  598. struct trace_array_cpu *data,
  599. struct task_struct *wakee,
  600. struct task_struct *curr,
  601. unsigned long flags)
  602. {
  603. struct trace_entry *entry;
  604. unsigned long irq_flags;
  605. raw_local_irq_save(irq_flags);
  606. __raw_spin_lock(&data->lock);
  607. entry = tracing_get_trace_entry(tr, data);
  608. tracing_generic_entry_update(entry, flags);
  609. entry->type = TRACE_WAKE;
  610. entry->ctx.prev_pid = curr->pid;
  611. entry->ctx.prev_prio = curr->prio;
  612. entry->ctx.prev_state = curr->state;
  613. entry->ctx.next_pid = wakee->pid;
  614. entry->ctx.next_prio = wakee->prio;
  615. entry->ctx.next_state = wakee->state;
  616. __trace_stack(tr, data, flags, 5);
  617. __raw_spin_unlock(&data->lock);
  618. raw_local_irq_restore(irq_flags);
  619. trace_wake_up();
  620. }
  621. #ifdef CONFIG_FTRACE
  622. static void
  623. function_trace_call(unsigned long ip, unsigned long parent_ip)
  624. {
  625. struct trace_array *tr = &global_trace;
  626. struct trace_array_cpu *data;
  627. unsigned long flags;
  628. long disabled;
  629. int cpu;
  630. if (unlikely(!tracer_enabled))
  631. return;
  632. local_irq_save(flags);
  633. cpu = raw_smp_processor_id();
  634. data = tr->data[cpu];
  635. disabled = atomic_inc_return(&data->disabled);
  636. if (likely(disabled == 1))
  637. trace_function(tr, data, ip, parent_ip, flags);
  638. atomic_dec(&data->disabled);
  639. local_irq_restore(flags);
  640. }
  641. static struct ftrace_ops trace_ops __read_mostly =
  642. {
  643. .func = function_trace_call,
  644. };
  645. void tracing_start_function_trace(void)
  646. {
  647. register_ftrace_function(&trace_ops);
  648. }
  649. void tracing_stop_function_trace(void)
  650. {
  651. unregister_ftrace_function(&trace_ops);
  652. }
  653. #endif
  654. enum trace_file_type {
  655. TRACE_FILE_LAT_FMT = 1,
  656. };
  657. static struct trace_entry *
  658. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  659. struct trace_iterator *iter, int cpu)
  660. {
  661. struct page *page;
  662. struct trace_entry *array;
  663. if (iter->next_idx[cpu] >= tr->entries ||
  664. iter->next_idx[cpu] >= data->trace_idx ||
  665. (data->trace_head == data->trace_tail &&
  666. data->trace_head_idx == data->trace_tail_idx))
  667. return NULL;
  668. if (!iter->next_page[cpu]) {
  669. /* Initialize the iterator for this cpu trace buffer */
  670. WARN_ON(!data->trace_tail);
  671. page = virt_to_page(data->trace_tail);
  672. iter->next_page[cpu] = &page->lru;
  673. iter->next_page_idx[cpu] = data->trace_tail_idx;
  674. }
  675. page = list_entry(iter->next_page[cpu], struct page, lru);
  676. BUG_ON(&data->trace_pages == &page->lru);
  677. array = page_address(page);
  678. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  679. return &array[iter->next_page_idx[cpu]];
  680. }
  681. static struct trace_entry *
  682. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  683. {
  684. struct trace_array *tr = iter->tr;
  685. struct trace_entry *ent, *next = NULL;
  686. int next_cpu = -1;
  687. int cpu;
  688. for_each_possible_cpu(cpu) {
  689. if (!head_page(tr->data[cpu]))
  690. continue;
  691. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  692. /*
  693. * Pick the entry with the smallest timestamp:
  694. */
  695. if (ent && (!next || ent->t < next->t)) {
  696. next = ent;
  697. next_cpu = cpu;
  698. }
  699. }
  700. if (ent_cpu)
  701. *ent_cpu = next_cpu;
  702. return next;
  703. }
  704. static void trace_iterator_increment(struct trace_iterator *iter)
  705. {
  706. iter->idx++;
  707. iter->next_idx[iter->cpu]++;
  708. iter->next_page_idx[iter->cpu]++;
  709. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  710. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  711. iter->next_page_idx[iter->cpu] = 0;
  712. iter->next_page[iter->cpu] =
  713. trace_next_list(data, iter->next_page[iter->cpu]);
  714. }
  715. }
  716. static void trace_consume(struct trace_iterator *iter)
  717. {
  718. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  719. data->trace_tail_idx++;
  720. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  721. data->trace_tail = trace_next_page(data, data->trace_tail);
  722. data->trace_tail_idx = 0;
  723. }
  724. /* Check if we empty it, then reset the index */
  725. if (data->trace_head == data->trace_tail &&
  726. data->trace_head_idx == data->trace_tail_idx)
  727. data->trace_idx = 0;
  728. }
  729. static void *find_next_entry_inc(struct trace_iterator *iter)
  730. {
  731. struct trace_entry *next;
  732. int next_cpu = -1;
  733. next = find_next_entry(iter, &next_cpu);
  734. iter->prev_ent = iter->ent;
  735. iter->prev_cpu = iter->cpu;
  736. iter->ent = next;
  737. iter->cpu = next_cpu;
  738. if (next)
  739. trace_iterator_increment(iter);
  740. return next ? iter : NULL;
  741. }
  742. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  743. {
  744. struct trace_iterator *iter = m->private;
  745. void *last_ent = iter->ent;
  746. int i = (int)*pos;
  747. void *ent;
  748. (*pos)++;
  749. /* can't go backwards */
  750. if (iter->idx > i)
  751. return NULL;
  752. if (iter->idx < 0)
  753. ent = find_next_entry_inc(iter);
  754. else
  755. ent = iter;
  756. while (ent && iter->idx < i)
  757. ent = find_next_entry_inc(iter);
  758. iter->pos = *pos;
  759. if (last_ent && !ent)
  760. seq_puts(m, "\n\nvim:ft=help\n");
  761. return ent;
  762. }
  763. static void *s_start(struct seq_file *m, loff_t *pos)
  764. {
  765. struct trace_iterator *iter = m->private;
  766. void *p = NULL;
  767. loff_t l = 0;
  768. int i;
  769. mutex_lock(&trace_types_lock);
  770. if (!current_trace || current_trace != iter->trace) {
  771. mutex_unlock(&trace_types_lock);
  772. return NULL;
  773. }
  774. atomic_inc(&trace_record_cmdline_disabled);
  775. /* let the tracer grab locks here if needed */
  776. if (current_trace->start)
  777. current_trace->start(iter);
  778. if (*pos != iter->pos) {
  779. iter->ent = NULL;
  780. iter->cpu = 0;
  781. iter->idx = -1;
  782. iter->prev_ent = NULL;
  783. iter->prev_cpu = -1;
  784. for_each_possible_cpu(i) {
  785. iter->next_idx[i] = 0;
  786. iter->next_page[i] = NULL;
  787. }
  788. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  789. ;
  790. } else {
  791. l = *pos - 1;
  792. p = s_next(m, p, &l);
  793. }
  794. return p;
  795. }
  796. static void s_stop(struct seq_file *m, void *p)
  797. {
  798. struct trace_iterator *iter = m->private;
  799. atomic_dec(&trace_record_cmdline_disabled);
  800. /* let the tracer release locks here if needed */
  801. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  802. iter->trace->stop(iter);
  803. mutex_unlock(&trace_types_lock);
  804. }
  805. static int
  806. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  807. {
  808. #ifdef CONFIG_KALLSYMS
  809. char str[KSYM_SYMBOL_LEN];
  810. kallsyms_lookup(address, NULL, NULL, NULL, str);
  811. return trace_seq_printf(s, fmt, str);
  812. #endif
  813. return 1;
  814. }
  815. static int
  816. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  817. unsigned long address)
  818. {
  819. #ifdef CONFIG_KALLSYMS
  820. char str[KSYM_SYMBOL_LEN];
  821. sprint_symbol(str, address);
  822. return trace_seq_printf(s, fmt, str);
  823. #endif
  824. return 1;
  825. }
  826. #ifndef CONFIG_64BIT
  827. # define IP_FMT "%08lx"
  828. #else
  829. # define IP_FMT "%016lx"
  830. #endif
  831. static int
  832. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  833. {
  834. int ret;
  835. if (!ip)
  836. return trace_seq_printf(s, "0");
  837. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  838. ret = seq_print_sym_offset(s, "%s", ip);
  839. else
  840. ret = seq_print_sym_short(s, "%s", ip);
  841. if (!ret)
  842. return 0;
  843. if (sym_flags & TRACE_ITER_SYM_ADDR)
  844. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  845. return ret;
  846. }
  847. static void print_lat_help_header(struct seq_file *m)
  848. {
  849. seq_puts(m, "# _------=> CPU# \n");
  850. seq_puts(m, "# / _-----=> irqs-off \n");
  851. seq_puts(m, "# | / _----=> need-resched \n");
  852. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  853. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  854. seq_puts(m, "# |||| / \n");
  855. seq_puts(m, "# ||||| delay \n");
  856. seq_puts(m, "# cmd pid ||||| time | caller \n");
  857. seq_puts(m, "# \\ / ||||| \\ | / \n");
  858. }
  859. static void print_func_help_header(struct seq_file *m)
  860. {
  861. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  862. seq_puts(m, "# | | | | |\n");
  863. }
  864. static void
  865. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  866. {
  867. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  868. struct trace_array *tr = iter->tr;
  869. struct trace_array_cpu *data = tr->data[tr->cpu];
  870. struct tracer *type = current_trace;
  871. unsigned long total = 0;
  872. unsigned long entries = 0;
  873. int cpu;
  874. const char *name = "preemption";
  875. if (type)
  876. name = type->name;
  877. for_each_possible_cpu(cpu) {
  878. if (head_page(tr->data[cpu])) {
  879. total += tr->data[cpu]->trace_idx;
  880. if (tr->data[cpu]->trace_idx > tr->entries)
  881. entries += tr->entries;
  882. else
  883. entries += tr->data[cpu]->trace_idx;
  884. }
  885. }
  886. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  887. name, UTS_RELEASE);
  888. seq_puts(m, "-----------------------------------"
  889. "---------------------------------\n");
  890. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  891. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  892. nsecs_to_usecs(data->saved_latency),
  893. entries,
  894. total,
  895. tr->cpu,
  896. #if defined(CONFIG_PREEMPT_NONE)
  897. "server",
  898. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  899. "desktop",
  900. #elif defined(CONFIG_PREEMPT_DESKTOP)
  901. "preempt",
  902. #else
  903. "unknown",
  904. #endif
  905. /* These are reserved for later use */
  906. 0, 0, 0, 0);
  907. #ifdef CONFIG_SMP
  908. seq_printf(m, " #P:%d)\n", num_online_cpus());
  909. #else
  910. seq_puts(m, ")\n");
  911. #endif
  912. seq_puts(m, " -----------------\n");
  913. seq_printf(m, " | task: %.16s-%d "
  914. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  915. data->comm, data->pid, data->uid, data->nice,
  916. data->policy, data->rt_priority);
  917. seq_puts(m, " -----------------\n");
  918. if (data->critical_start) {
  919. seq_puts(m, " => started at: ");
  920. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  921. trace_print_seq(m, &iter->seq);
  922. seq_puts(m, "\n => ended at: ");
  923. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  924. trace_print_seq(m, &iter->seq);
  925. seq_puts(m, "\n");
  926. }
  927. seq_puts(m, "\n");
  928. }
  929. static void
  930. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  931. {
  932. int hardirq, softirq;
  933. char *comm;
  934. comm = trace_find_cmdline(entry->pid);
  935. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  936. trace_seq_printf(s, "%d", cpu);
  937. trace_seq_printf(s, "%c%c",
  938. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  939. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  940. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  941. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  942. if (hardirq && softirq)
  943. trace_seq_putc(s, 'H');
  944. else {
  945. if (hardirq)
  946. trace_seq_putc(s, 'h');
  947. else {
  948. if (softirq)
  949. trace_seq_putc(s, 's');
  950. else
  951. trace_seq_putc(s, '.');
  952. }
  953. }
  954. if (entry->preempt_count)
  955. trace_seq_printf(s, "%x", entry->preempt_count);
  956. else
  957. trace_seq_puts(s, ".");
  958. }
  959. unsigned long preempt_mark_thresh = 100;
  960. static void
  961. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  962. unsigned long rel_usecs)
  963. {
  964. trace_seq_printf(s, " %4lldus", abs_usecs);
  965. if (rel_usecs > preempt_mark_thresh)
  966. trace_seq_puts(s, "!: ");
  967. else if (rel_usecs > 1)
  968. trace_seq_puts(s, "+: ");
  969. else
  970. trace_seq_puts(s, " : ");
  971. }
  972. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  973. static int
  974. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  975. {
  976. struct trace_seq *s = &iter->seq;
  977. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  978. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  979. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  980. struct trace_entry *entry = iter->ent;
  981. unsigned long abs_usecs;
  982. unsigned long rel_usecs;
  983. char *comm;
  984. int S, T;
  985. int i;
  986. unsigned state;
  987. if (!next_entry)
  988. next_entry = entry;
  989. rel_usecs = ns2usecs(next_entry->t - entry->t);
  990. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  991. if (verbose) {
  992. comm = trace_find_cmdline(entry->pid);
  993. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  994. " %ld.%03ldms (+%ld.%03ldms): ",
  995. comm,
  996. entry->pid, cpu, entry->flags,
  997. entry->preempt_count, trace_idx,
  998. ns2usecs(entry->t),
  999. abs_usecs/1000,
  1000. abs_usecs % 1000, rel_usecs/1000,
  1001. rel_usecs % 1000);
  1002. } else {
  1003. lat_print_generic(s, entry, cpu);
  1004. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1005. }
  1006. switch (entry->type) {
  1007. case TRACE_FN:
  1008. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1009. trace_seq_puts(s, " (");
  1010. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1011. trace_seq_puts(s, ")\n");
  1012. break;
  1013. case TRACE_CTX:
  1014. case TRACE_WAKE:
  1015. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1016. state_to_char[entry->ctx.next_state] : 'X';
  1017. state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
  1018. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1019. comm = trace_find_cmdline(entry->ctx.next_pid);
  1020. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1021. entry->ctx.prev_pid,
  1022. entry->ctx.prev_prio,
  1023. S, entry->type == TRACE_CTX ? "==>" : " +",
  1024. entry->ctx.next_pid,
  1025. entry->ctx.next_prio,
  1026. T, comm);
  1027. break;
  1028. case TRACE_SPECIAL:
  1029. trace_seq_printf(s, "# %ld %ld %ld\n",
  1030. entry->special.arg1,
  1031. entry->special.arg2,
  1032. entry->special.arg3);
  1033. break;
  1034. case TRACE_STACK:
  1035. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1036. if (i)
  1037. trace_seq_puts(s, " <= ");
  1038. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1039. }
  1040. trace_seq_puts(s, "\n");
  1041. break;
  1042. default:
  1043. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1044. }
  1045. return 1;
  1046. }
  1047. static int print_trace_fmt(struct trace_iterator *iter)
  1048. {
  1049. struct trace_seq *s = &iter->seq;
  1050. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1051. struct trace_entry *entry;
  1052. unsigned long usec_rem;
  1053. unsigned long long t;
  1054. unsigned long secs;
  1055. char *comm;
  1056. int ret;
  1057. int S, T;
  1058. int i;
  1059. entry = iter->ent;
  1060. comm = trace_find_cmdline(iter->ent->pid);
  1061. t = ns2usecs(entry->t);
  1062. usec_rem = do_div(t, 1000000ULL);
  1063. secs = (unsigned long)t;
  1064. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1065. if (!ret)
  1066. return 0;
  1067. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1068. if (!ret)
  1069. return 0;
  1070. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1071. if (!ret)
  1072. return 0;
  1073. switch (entry->type) {
  1074. case TRACE_FN:
  1075. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1076. if (!ret)
  1077. return 0;
  1078. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1079. entry->fn.parent_ip) {
  1080. ret = trace_seq_printf(s, " <-");
  1081. if (!ret)
  1082. return 0;
  1083. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1084. sym_flags);
  1085. if (!ret)
  1086. return 0;
  1087. }
  1088. ret = trace_seq_printf(s, "\n");
  1089. if (!ret)
  1090. return 0;
  1091. break;
  1092. case TRACE_CTX:
  1093. case TRACE_WAKE:
  1094. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1095. state_to_char[entry->ctx.prev_state] : 'X';
  1096. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1097. state_to_char[entry->ctx.next_state] : 'X';
  1098. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1099. entry->ctx.prev_pid,
  1100. entry->ctx.prev_prio,
  1101. S,
  1102. entry->type == TRACE_CTX ? "==>" : " +",
  1103. entry->ctx.next_pid,
  1104. entry->ctx.next_prio,
  1105. T);
  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, T;
  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. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1159. state_to_char[entry->ctx.next_state] : 'X';
  1160. if (entry->type == TRACE_WAKE)
  1161. S = '+';
  1162. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1163. entry->ctx.prev_pid,
  1164. entry->ctx.prev_prio,
  1165. S,
  1166. entry->ctx.next_pid,
  1167. entry->ctx.next_prio,
  1168. T);
  1169. if (!ret)
  1170. return 0;
  1171. break;
  1172. case TRACE_SPECIAL:
  1173. case TRACE_STACK:
  1174. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1175. entry->special.arg1,
  1176. entry->special.arg2,
  1177. entry->special.arg3);
  1178. if (!ret)
  1179. return 0;
  1180. break;
  1181. }
  1182. return 1;
  1183. }
  1184. #define SEQ_PUT_FIELD_RET(s, x) \
  1185. do { \
  1186. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1187. return 0; \
  1188. } while (0)
  1189. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1190. do { \
  1191. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1192. return 0; \
  1193. } while (0)
  1194. static int print_hex_fmt(struct trace_iterator *iter)
  1195. {
  1196. struct trace_seq *s = &iter->seq;
  1197. unsigned char newline = '\n';
  1198. struct trace_entry *entry;
  1199. int S, T;
  1200. entry = iter->ent;
  1201. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1202. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1203. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1204. switch (entry->type) {
  1205. case TRACE_FN:
  1206. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1207. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1208. break;
  1209. case TRACE_CTX:
  1210. case TRACE_WAKE:
  1211. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1212. state_to_char[entry->ctx.prev_state] : 'X';
  1213. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1214. state_to_char[entry->ctx.next_state] : 'X';
  1215. if (entry->type == TRACE_WAKE)
  1216. S = '+';
  1217. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1218. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1219. SEQ_PUT_HEX_FIELD_RET(s, S);
  1220. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1221. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1222. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1223. SEQ_PUT_HEX_FIELD_RET(s, T);
  1224. break;
  1225. case TRACE_SPECIAL:
  1226. case TRACE_STACK:
  1227. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1228. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1229. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1230. break;
  1231. }
  1232. SEQ_PUT_FIELD_RET(s, newline);
  1233. return 1;
  1234. }
  1235. static int print_bin_fmt(struct trace_iterator *iter)
  1236. {
  1237. struct trace_seq *s = &iter->seq;
  1238. struct trace_entry *entry;
  1239. entry = iter->ent;
  1240. SEQ_PUT_FIELD_RET(s, entry->pid);
  1241. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1242. SEQ_PUT_FIELD_RET(s, entry->t);
  1243. switch (entry->type) {
  1244. case TRACE_FN:
  1245. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1246. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1247. break;
  1248. case TRACE_CTX:
  1249. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1250. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1251. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1252. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1253. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1254. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1255. break;
  1256. case TRACE_SPECIAL:
  1257. case TRACE_STACK:
  1258. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1259. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1260. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1261. break;
  1262. }
  1263. return 1;
  1264. }
  1265. static int trace_empty(struct trace_iterator *iter)
  1266. {
  1267. struct trace_array_cpu *data;
  1268. int cpu;
  1269. for_each_possible_cpu(cpu) {
  1270. data = iter->tr->data[cpu];
  1271. if (head_page(data) && data->trace_idx &&
  1272. (data->trace_tail != data->trace_head ||
  1273. data->trace_tail_idx != data->trace_head_idx))
  1274. return 0;
  1275. }
  1276. return 1;
  1277. }
  1278. static int print_trace_line(struct trace_iterator *iter)
  1279. {
  1280. if (iter->trace && iter->trace->print_line)
  1281. return iter->trace->print_line(iter);
  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. raw_local_irq_disable();
  1514. __raw_spin_lock(&ftrace_max_lock);
  1515. for_each_possible_cpu(cpu) {
  1516. /*
  1517. * Increase/decrease the disabled counter if we are
  1518. * about to flip a bit in the cpumask:
  1519. */
  1520. if (cpu_isset(cpu, tracing_cpumask) &&
  1521. !cpu_isset(cpu, tracing_cpumask_new)) {
  1522. atomic_inc(&global_trace.data[cpu]->disabled);
  1523. }
  1524. if (!cpu_isset(cpu, tracing_cpumask) &&
  1525. cpu_isset(cpu, tracing_cpumask_new)) {
  1526. atomic_dec(&global_trace.data[cpu]->disabled);
  1527. }
  1528. }
  1529. __raw_spin_unlock(&ftrace_max_lock);
  1530. raw_local_irq_enable();
  1531. tracing_cpumask = tracing_cpumask_new;
  1532. mutex_unlock(&tracing_cpumask_update_lock);
  1533. return count;
  1534. err_unlock:
  1535. mutex_unlock(&tracing_cpumask_update_lock);
  1536. return err;
  1537. }
  1538. static struct file_operations tracing_cpumask_fops = {
  1539. .open = tracing_open_generic,
  1540. .read = tracing_cpumask_read,
  1541. .write = tracing_cpumask_write,
  1542. };
  1543. static ssize_t
  1544. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1545. size_t cnt, loff_t *ppos)
  1546. {
  1547. char *buf;
  1548. int r = 0;
  1549. int len = 0;
  1550. int i;
  1551. /* calulate max size */
  1552. for (i = 0; trace_options[i]; i++) {
  1553. len += strlen(trace_options[i]);
  1554. len += 3; /* "no" and space */
  1555. }
  1556. /* +2 for \n and \0 */
  1557. buf = kmalloc(len + 2, GFP_KERNEL);
  1558. if (!buf)
  1559. return -ENOMEM;
  1560. for (i = 0; trace_options[i]; i++) {
  1561. if (trace_flags & (1 << i))
  1562. r += sprintf(buf + r, "%s ", trace_options[i]);
  1563. else
  1564. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1565. }
  1566. r += sprintf(buf + r, "\n");
  1567. WARN_ON(r >= len + 2);
  1568. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1569. kfree(buf);
  1570. return r;
  1571. }
  1572. static ssize_t
  1573. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1574. size_t cnt, loff_t *ppos)
  1575. {
  1576. char buf[64];
  1577. char *cmp = buf;
  1578. int neg = 0;
  1579. int i;
  1580. if (cnt > 63)
  1581. cnt = 63;
  1582. if (copy_from_user(&buf, ubuf, cnt))
  1583. return -EFAULT;
  1584. buf[cnt] = 0;
  1585. if (strncmp(buf, "no", 2) == 0) {
  1586. neg = 1;
  1587. cmp += 2;
  1588. }
  1589. for (i = 0; trace_options[i]; i++) {
  1590. int len = strlen(trace_options[i]);
  1591. if (strncmp(cmp, trace_options[i], len) == 0) {
  1592. if (neg)
  1593. trace_flags &= ~(1 << i);
  1594. else
  1595. trace_flags |= (1 << i);
  1596. break;
  1597. }
  1598. }
  1599. /*
  1600. * If no option could be set, return an error:
  1601. */
  1602. if (!trace_options[i])
  1603. return -EINVAL;
  1604. filp->f_pos += cnt;
  1605. return cnt;
  1606. }
  1607. static struct file_operations tracing_iter_fops = {
  1608. .open = tracing_open_generic,
  1609. .read = tracing_iter_ctrl_read,
  1610. .write = tracing_iter_ctrl_write,
  1611. };
  1612. static const char readme_msg[] =
  1613. "tracing mini-HOWTO:\n\n"
  1614. "# mkdir /debug\n"
  1615. "# mount -t debugfs nodev /debug\n\n"
  1616. "# cat /debug/tracing/available_tracers\n"
  1617. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1618. "# cat /debug/tracing/current_tracer\n"
  1619. "none\n"
  1620. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1621. "# cat /debug/tracing/current_tracer\n"
  1622. "sched_switch\n"
  1623. "# cat /debug/tracing/iter_ctrl\n"
  1624. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1625. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1626. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1627. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1628. "echo 0 > /debug/tracing/tracing_enabled\n"
  1629. ;
  1630. static ssize_t
  1631. tracing_readme_read(struct file *filp, char __user *ubuf,
  1632. size_t cnt, loff_t *ppos)
  1633. {
  1634. return simple_read_from_buffer(ubuf, cnt, ppos,
  1635. readme_msg, strlen(readme_msg));
  1636. }
  1637. static struct file_operations tracing_readme_fops = {
  1638. .open = tracing_open_generic,
  1639. .read = tracing_readme_read,
  1640. };
  1641. static ssize_t
  1642. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1643. size_t cnt, loff_t *ppos)
  1644. {
  1645. struct trace_array *tr = filp->private_data;
  1646. char buf[64];
  1647. int r;
  1648. r = sprintf(buf, "%ld\n", tr->ctrl);
  1649. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1650. }
  1651. static ssize_t
  1652. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1653. size_t cnt, loff_t *ppos)
  1654. {
  1655. struct trace_array *tr = 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. val = !!val;
  1665. mutex_lock(&trace_types_lock);
  1666. if (tr->ctrl ^ val) {
  1667. if (val)
  1668. tracer_enabled = 1;
  1669. else
  1670. tracer_enabled = 0;
  1671. tr->ctrl = val;
  1672. if (current_trace && current_trace->ctrl_update)
  1673. current_trace->ctrl_update(tr);
  1674. }
  1675. mutex_unlock(&trace_types_lock);
  1676. filp->f_pos += cnt;
  1677. return cnt;
  1678. }
  1679. static ssize_t
  1680. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1681. size_t cnt, loff_t *ppos)
  1682. {
  1683. char buf[max_tracer_type_len+2];
  1684. int r;
  1685. mutex_lock(&trace_types_lock);
  1686. if (current_trace)
  1687. r = sprintf(buf, "%s\n", current_trace->name);
  1688. else
  1689. r = sprintf(buf, "\n");
  1690. mutex_unlock(&trace_types_lock);
  1691. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1692. }
  1693. static ssize_t
  1694. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1695. size_t cnt, loff_t *ppos)
  1696. {
  1697. struct trace_array *tr = &global_trace;
  1698. struct tracer *t;
  1699. char buf[max_tracer_type_len+1];
  1700. int i;
  1701. if (cnt > max_tracer_type_len)
  1702. cnt = max_tracer_type_len;
  1703. if (copy_from_user(&buf, ubuf, cnt))
  1704. return -EFAULT;
  1705. buf[cnt] = 0;
  1706. /* strip ending whitespace. */
  1707. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1708. buf[i] = 0;
  1709. mutex_lock(&trace_types_lock);
  1710. for (t = trace_types; t; t = t->next) {
  1711. if (strcmp(t->name, buf) == 0)
  1712. break;
  1713. }
  1714. if (!t || t == current_trace)
  1715. goto out;
  1716. if (current_trace && current_trace->reset)
  1717. current_trace->reset(tr);
  1718. current_trace = t;
  1719. if (t->init)
  1720. t->init(tr);
  1721. out:
  1722. mutex_unlock(&trace_types_lock);
  1723. filp->f_pos += cnt;
  1724. return cnt;
  1725. }
  1726. static ssize_t
  1727. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1728. size_t cnt, loff_t *ppos)
  1729. {
  1730. unsigned long *ptr = filp->private_data;
  1731. char buf[64];
  1732. int r;
  1733. r = snprintf(buf, 64, "%ld\n",
  1734. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1735. if (r > 64)
  1736. r = 64;
  1737. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1738. }
  1739. static ssize_t
  1740. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1741. size_t cnt, loff_t *ppos)
  1742. {
  1743. long *ptr = filp->private_data;
  1744. long val;
  1745. char buf[64];
  1746. if (cnt > 63)
  1747. cnt = 63;
  1748. if (copy_from_user(&buf, ubuf, cnt))
  1749. return -EFAULT;
  1750. buf[cnt] = 0;
  1751. val = simple_strtoul(buf, NULL, 10);
  1752. *ptr = val * 1000;
  1753. return cnt;
  1754. }
  1755. static atomic_t tracing_reader;
  1756. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1757. {
  1758. struct trace_iterator *iter;
  1759. if (tracing_disabled)
  1760. return -ENODEV;
  1761. /* We only allow for reader of the pipe */
  1762. if (atomic_inc_return(&tracing_reader) != 1) {
  1763. atomic_dec(&tracing_reader);
  1764. return -EBUSY;
  1765. }
  1766. /* create a buffer to store the information to pass to userspace */
  1767. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1768. if (!iter)
  1769. return -ENOMEM;
  1770. iter->tr = &global_trace;
  1771. iter->trace = current_trace;
  1772. filp->private_data = iter;
  1773. return 0;
  1774. }
  1775. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1776. {
  1777. struct trace_iterator *iter = file->private_data;
  1778. kfree(iter);
  1779. atomic_dec(&tracing_reader);
  1780. return 0;
  1781. }
  1782. static unsigned int
  1783. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1784. {
  1785. struct trace_iterator *iter = filp->private_data;
  1786. if (trace_flags & TRACE_ITER_BLOCK) {
  1787. /*
  1788. * Always select as readable when in blocking mode
  1789. */
  1790. return POLLIN | POLLRDNORM;
  1791. }
  1792. else {
  1793. if (!trace_empty(iter))
  1794. return POLLIN | POLLRDNORM;
  1795. poll_wait(filp, &trace_wait, poll_table);
  1796. if (!trace_empty(iter))
  1797. return POLLIN | POLLRDNORM;
  1798. return 0;
  1799. }
  1800. }
  1801. /*
  1802. * Consumer reader.
  1803. */
  1804. static ssize_t
  1805. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1806. size_t cnt, loff_t *ppos)
  1807. {
  1808. struct trace_iterator *iter = filp->private_data;
  1809. struct trace_array_cpu *data;
  1810. struct trace_array *tr = iter->tr;
  1811. struct tracer *tracer = iter->trace;
  1812. static cpumask_t mask;
  1813. static int start;
  1814. unsigned long flags;
  1815. #ifdef CONFIG_FTRACE
  1816. int ftrace_save;
  1817. #endif
  1818. int read = 0;
  1819. int cpu;
  1820. int len;
  1821. int ret;
  1822. /* return any leftover data */
  1823. if (iter->seq.len > start) {
  1824. len = iter->seq.len - start;
  1825. if (cnt > len)
  1826. cnt = len;
  1827. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1828. if (ret)
  1829. cnt = -EFAULT;
  1830. start += len;
  1831. return cnt;
  1832. }
  1833. trace_seq_reset(&iter->seq);
  1834. start = 0;
  1835. while (trace_empty(iter)) {
  1836. if ((filp->f_flags & O_NONBLOCK))
  1837. return -EAGAIN;
  1838. /*
  1839. * This is a make-shift waitqueue. The reason we don't use
  1840. * an actual wait queue is because:
  1841. * 1) we only ever have one waiter
  1842. * 2) the tracing, traces all functions, we don't want
  1843. * the overhead of calling wake_up and friends
  1844. * (and tracing them too)
  1845. * Anyway, this is really very primitive wakeup.
  1846. */
  1847. set_current_state(TASK_INTERRUPTIBLE);
  1848. iter->tr->waiter = current;
  1849. /* sleep for one second, and try again. */
  1850. schedule_timeout(HZ);
  1851. iter->tr->waiter = NULL;
  1852. if (signal_pending(current))
  1853. return -EINTR;
  1854. if (iter->trace != current_trace)
  1855. return 0;
  1856. /*
  1857. * We block until we read something and tracing is disabled.
  1858. * We still block if tracing is disabled, but we have never
  1859. * read anything. This allows a user to cat this file, and
  1860. * then enable tracing. But after we have read something,
  1861. * we give an EOF when tracing is again disabled.
  1862. *
  1863. * iter->pos will be 0 if we haven't read anything.
  1864. */
  1865. if (!tracer_enabled && iter->pos)
  1866. break;
  1867. continue;
  1868. }
  1869. /* stop when tracing is finished */
  1870. if (trace_empty(iter))
  1871. return 0;
  1872. if (cnt >= PAGE_SIZE)
  1873. cnt = PAGE_SIZE - 1;
  1874. memset(iter, 0, sizeof(*iter));
  1875. iter->tr = tr;
  1876. iter->trace = tracer;
  1877. iter->pos = -1;
  1878. /*
  1879. * We need to stop all tracing on all CPUS to read the
  1880. * the next buffer. This is a bit expensive, but is
  1881. * not done often. We fill all what we can read,
  1882. * and then release the locks again.
  1883. */
  1884. cpus_clear(mask);
  1885. local_irq_save(flags);
  1886. #ifdef CONFIG_FTRACE
  1887. ftrace_save = ftrace_enabled;
  1888. ftrace_enabled = 0;
  1889. #endif
  1890. smp_wmb();
  1891. for_each_possible_cpu(cpu) {
  1892. data = iter->tr->data[cpu];
  1893. if (!head_page(data) || !data->trace_idx)
  1894. continue;
  1895. atomic_inc(&data->disabled);
  1896. cpu_set(cpu, mask);
  1897. }
  1898. for_each_cpu_mask(cpu, mask) {
  1899. data = iter->tr->data[cpu];
  1900. __raw_spin_lock(&data->lock);
  1901. }
  1902. while (find_next_entry_inc(iter) != NULL) {
  1903. int len = iter->seq.len;
  1904. ret = print_trace_line(iter);
  1905. if (!ret) {
  1906. /* don't print partial lines */
  1907. iter->seq.len = len;
  1908. break;
  1909. }
  1910. trace_consume(iter);
  1911. if (iter->seq.len >= cnt)
  1912. break;
  1913. }
  1914. for_each_cpu_mask(cpu, mask) {
  1915. data = iter->tr->data[cpu];
  1916. __raw_spin_unlock(&data->lock);
  1917. }
  1918. for_each_cpu_mask(cpu, mask) {
  1919. data = iter->tr->data[cpu];
  1920. atomic_dec(&data->disabled);
  1921. }
  1922. #ifdef CONFIG_FTRACE
  1923. ftrace_enabled = ftrace_save;
  1924. #endif
  1925. local_irq_restore(flags);
  1926. /* Now copy what we have to the user */
  1927. read = iter->seq.len;
  1928. if (read > cnt)
  1929. read = cnt;
  1930. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1931. if (read < iter->seq.len)
  1932. start = read;
  1933. else
  1934. trace_seq_reset(&iter->seq);
  1935. if (ret)
  1936. read = -EFAULT;
  1937. return read;
  1938. }
  1939. static ssize_t
  1940. tracing_entries_read(struct file *filp, char __user *ubuf,
  1941. size_t cnt, loff_t *ppos)
  1942. {
  1943. struct trace_array *tr = filp->private_data;
  1944. char buf[64];
  1945. int r;
  1946. r = sprintf(buf, "%lu\n", tr->entries);
  1947. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1948. }
  1949. static ssize_t
  1950. tracing_entries_write(struct file *filp, const char __user *ubuf,
  1951. size_t cnt, loff_t *ppos)
  1952. {
  1953. unsigned long val;
  1954. char buf[64];
  1955. if (cnt > 63)
  1956. cnt = 63;
  1957. if (copy_from_user(&buf, ubuf, cnt))
  1958. return -EFAULT;
  1959. buf[cnt] = 0;
  1960. val = simple_strtoul(buf, NULL, 10);
  1961. /* must have at least 1 entry */
  1962. if (!val)
  1963. return -EINVAL;
  1964. mutex_lock(&trace_types_lock);
  1965. if (current_trace != &no_tracer) {
  1966. cnt = -EBUSY;
  1967. pr_info("ftrace: set current_tracer to none"
  1968. " before modifying buffer size\n");
  1969. goto out;
  1970. }
  1971. if (val > global_trace.entries) {
  1972. while (global_trace.entries < val) {
  1973. if (trace_alloc_page()) {
  1974. cnt = -ENOMEM;
  1975. goto out;
  1976. }
  1977. }
  1978. } else {
  1979. /* include the number of entries in val (inc of page entries) */
  1980. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  1981. trace_free_page();
  1982. }
  1983. filp->f_pos += cnt;
  1984. out:
  1985. max_tr.entries = global_trace.entries;
  1986. mutex_unlock(&trace_types_lock);
  1987. return cnt;
  1988. }
  1989. static struct file_operations tracing_max_lat_fops = {
  1990. .open = tracing_open_generic,
  1991. .read = tracing_max_lat_read,
  1992. .write = tracing_max_lat_write,
  1993. };
  1994. static struct file_operations tracing_ctrl_fops = {
  1995. .open = tracing_open_generic,
  1996. .read = tracing_ctrl_read,
  1997. .write = tracing_ctrl_write,
  1998. };
  1999. static struct file_operations set_tracer_fops = {
  2000. .open = tracing_open_generic,
  2001. .read = tracing_set_trace_read,
  2002. .write = tracing_set_trace_write,
  2003. };
  2004. static struct file_operations tracing_pipe_fops = {
  2005. .open = tracing_open_pipe,
  2006. .poll = tracing_poll_pipe,
  2007. .read = tracing_read_pipe,
  2008. .release = tracing_release_pipe,
  2009. };
  2010. static struct file_operations tracing_entries_fops = {
  2011. .open = tracing_open_generic,
  2012. .read = tracing_entries_read,
  2013. .write = tracing_entries_write,
  2014. };
  2015. #ifdef CONFIG_DYNAMIC_FTRACE
  2016. static ssize_t
  2017. tracing_read_long(struct file *filp, char __user *ubuf,
  2018. size_t cnt, loff_t *ppos)
  2019. {
  2020. unsigned long *p = filp->private_data;
  2021. char buf[64];
  2022. int r;
  2023. r = sprintf(buf, "%ld\n", *p);
  2024. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2025. }
  2026. static struct file_operations tracing_read_long_fops = {
  2027. .open = tracing_open_generic,
  2028. .read = tracing_read_long,
  2029. };
  2030. #endif
  2031. static struct dentry *d_tracer;
  2032. struct dentry *tracing_init_dentry(void)
  2033. {
  2034. static int once;
  2035. if (d_tracer)
  2036. return d_tracer;
  2037. d_tracer = debugfs_create_dir("tracing", NULL);
  2038. if (!d_tracer && !once) {
  2039. once = 1;
  2040. pr_warning("Could not create debugfs directory 'tracing'\n");
  2041. return NULL;
  2042. }
  2043. return d_tracer;
  2044. }
  2045. #ifdef CONFIG_FTRACE_SELFTEST
  2046. /* Let selftest have access to static functions in this file */
  2047. #include "trace_selftest.c"
  2048. #endif
  2049. static __init void tracer_init_debugfs(void)
  2050. {
  2051. struct dentry *d_tracer;
  2052. struct dentry *entry;
  2053. d_tracer = tracing_init_dentry();
  2054. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2055. &global_trace, &tracing_ctrl_fops);
  2056. if (!entry)
  2057. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2058. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2059. NULL, &tracing_iter_fops);
  2060. if (!entry)
  2061. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2062. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2063. NULL, &tracing_cpumask_fops);
  2064. if (!entry)
  2065. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2066. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2067. &global_trace, &tracing_lt_fops);
  2068. if (!entry)
  2069. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2070. entry = debugfs_create_file("trace", 0444, d_tracer,
  2071. &global_trace, &tracing_fops);
  2072. if (!entry)
  2073. pr_warning("Could not create debugfs 'trace' entry\n");
  2074. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2075. &global_trace, &show_traces_fops);
  2076. if (!entry)
  2077. pr_warning("Could not create debugfs 'trace' entry\n");
  2078. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2079. &global_trace, &set_tracer_fops);
  2080. if (!entry)
  2081. pr_warning("Could not create debugfs 'trace' entry\n");
  2082. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2083. &tracing_max_latency,
  2084. &tracing_max_lat_fops);
  2085. if (!entry)
  2086. pr_warning("Could not create debugfs "
  2087. "'tracing_max_latency' entry\n");
  2088. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2089. &tracing_thresh, &tracing_max_lat_fops);
  2090. if (!entry)
  2091. pr_warning("Could not create debugfs "
  2092. "'tracing_threash' entry\n");
  2093. entry = debugfs_create_file("README", 0644, d_tracer,
  2094. NULL, &tracing_readme_fops);
  2095. if (!entry)
  2096. pr_warning("Could not create debugfs 'README' entry\n");
  2097. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2098. NULL, &tracing_pipe_fops);
  2099. if (!entry)
  2100. pr_warning("Could not create debugfs "
  2101. "'tracing_threash' entry\n");
  2102. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2103. &global_trace, &tracing_entries_fops);
  2104. if (!entry)
  2105. pr_warning("Could not create debugfs "
  2106. "'tracing_threash' entry\n");
  2107. #ifdef CONFIG_DYNAMIC_FTRACE
  2108. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2109. &ftrace_update_tot_cnt,
  2110. &tracing_read_long_fops);
  2111. if (!entry)
  2112. pr_warning("Could not create debugfs "
  2113. "'dyn_ftrace_total_info' entry\n");
  2114. #endif
  2115. }
  2116. static int trace_alloc_page(void)
  2117. {
  2118. struct trace_array_cpu *data;
  2119. struct page *page, *tmp;
  2120. LIST_HEAD(pages);
  2121. void *array;
  2122. int i;
  2123. /* first allocate a page for each CPU */
  2124. for_each_possible_cpu(i) {
  2125. array = (void *)__get_free_page(GFP_KERNEL);
  2126. if (array == NULL) {
  2127. printk(KERN_ERR "tracer: failed to allocate page"
  2128. "for trace buffer!\n");
  2129. goto free_pages;
  2130. }
  2131. page = virt_to_page(array);
  2132. list_add(&page->lru, &pages);
  2133. /* Only allocate if we are actually using the max trace */
  2134. #ifdef CONFIG_TRACER_MAX_TRACE
  2135. array = (void *)__get_free_page(GFP_KERNEL);
  2136. if (array == NULL) {
  2137. printk(KERN_ERR "tracer: failed to allocate page"
  2138. "for trace buffer!\n");
  2139. goto free_pages;
  2140. }
  2141. page = virt_to_page(array);
  2142. list_add(&page->lru, &pages);
  2143. #endif
  2144. }
  2145. /* Now that we successfully allocate a page per CPU, add them */
  2146. for_each_possible_cpu(i) {
  2147. data = global_trace.data[i];
  2148. page = list_entry(pages.next, struct page, lru);
  2149. list_del_init(&page->lru);
  2150. list_add_tail(&page->lru, &data->trace_pages);
  2151. ClearPageLRU(page);
  2152. #ifdef CONFIG_TRACER_MAX_TRACE
  2153. data = max_tr.data[i];
  2154. page = list_entry(pages.next, struct page, lru);
  2155. list_del_init(&page->lru);
  2156. list_add_tail(&page->lru, &data->trace_pages);
  2157. SetPageLRU(page);
  2158. #endif
  2159. }
  2160. global_trace.entries += ENTRIES_PER_PAGE;
  2161. return 0;
  2162. free_pages:
  2163. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2164. list_del_init(&page->lru);
  2165. __free_page(page);
  2166. }
  2167. return -ENOMEM;
  2168. }
  2169. static int trace_free_page(void)
  2170. {
  2171. struct trace_array_cpu *data;
  2172. struct page *page;
  2173. struct list_head *p;
  2174. int i;
  2175. int ret = 0;
  2176. /* free one page from each buffer */
  2177. for_each_possible_cpu(i) {
  2178. data = global_trace.data[i];
  2179. p = data->trace_pages.next;
  2180. if (p == &data->trace_pages) {
  2181. /* should never happen */
  2182. WARN_ON(1);
  2183. tracing_disabled = 1;
  2184. ret = -1;
  2185. break;
  2186. }
  2187. page = list_entry(p, struct page, lru);
  2188. ClearPageLRU(page);
  2189. list_del(&page->lru);
  2190. __free_page(page);
  2191. tracing_reset(data);
  2192. #ifdef CONFIG_TRACER_MAX_TRACE
  2193. data = max_tr.data[i];
  2194. p = data->trace_pages.next;
  2195. if (p == &data->trace_pages) {
  2196. /* should never happen */
  2197. WARN_ON(1);
  2198. tracing_disabled = 1;
  2199. ret = -1;
  2200. break;
  2201. }
  2202. page = list_entry(p, struct page, lru);
  2203. ClearPageLRU(page);
  2204. list_del(&page->lru);
  2205. __free_page(page);
  2206. tracing_reset(data);
  2207. #endif
  2208. }
  2209. global_trace.entries -= ENTRIES_PER_PAGE;
  2210. return ret;
  2211. }
  2212. __init static int tracer_alloc_buffers(void)
  2213. {
  2214. struct trace_array_cpu *data;
  2215. void *array;
  2216. struct page *page;
  2217. int pages = 0;
  2218. int ret = -ENOMEM;
  2219. int i;
  2220. global_trace.ctrl = tracer_enabled;
  2221. /* Allocate the first page for all buffers */
  2222. for_each_possible_cpu(i) {
  2223. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2224. max_tr.data[i] = &per_cpu(max_data, i);
  2225. array = (void *)__get_free_page(GFP_KERNEL);
  2226. if (array == NULL) {
  2227. printk(KERN_ERR "tracer: failed to allocate page"
  2228. "for trace buffer!\n");
  2229. goto free_buffers;
  2230. }
  2231. /* set the array to the list */
  2232. INIT_LIST_HEAD(&data->trace_pages);
  2233. page = virt_to_page(array);
  2234. list_add(&page->lru, &data->trace_pages);
  2235. /* use the LRU flag to differentiate the two buffers */
  2236. ClearPageLRU(page);
  2237. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2238. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2239. /* Only allocate if we are actually using the max trace */
  2240. #ifdef CONFIG_TRACER_MAX_TRACE
  2241. array = (void *)__get_free_page(GFP_KERNEL);
  2242. if (array == NULL) {
  2243. printk(KERN_ERR "tracer: failed to allocate page"
  2244. "for trace buffer!\n");
  2245. goto free_buffers;
  2246. }
  2247. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2248. page = virt_to_page(array);
  2249. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2250. SetPageLRU(page);
  2251. #endif
  2252. }
  2253. /*
  2254. * Since we allocate by orders of pages, we may be able to
  2255. * round up a bit.
  2256. */
  2257. global_trace.entries = ENTRIES_PER_PAGE;
  2258. pages++;
  2259. while (global_trace.entries < trace_nr_entries) {
  2260. if (trace_alloc_page())
  2261. break;
  2262. pages++;
  2263. }
  2264. max_tr.entries = global_trace.entries;
  2265. pr_info("tracer: %d pages allocated for %ld",
  2266. pages, trace_nr_entries);
  2267. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2268. pr_info(" actual entries %ld\n", global_trace.entries);
  2269. tracer_init_debugfs();
  2270. trace_init_cmdlines();
  2271. register_tracer(&no_tracer);
  2272. current_trace = &no_tracer;
  2273. /* All seems OK, enable tracing */
  2274. tracing_disabled = 0;
  2275. return 0;
  2276. free_buffers:
  2277. for (i-- ; i >= 0; i--) {
  2278. struct page *page, *tmp;
  2279. struct trace_array_cpu *data = global_trace.data[i];
  2280. if (data) {
  2281. list_for_each_entry_safe(page, tmp,
  2282. &data->trace_pages, lru) {
  2283. list_del_init(&page->lru);
  2284. __free_page(page);
  2285. }
  2286. }
  2287. #ifdef CONFIG_TRACER_MAX_TRACE
  2288. data = max_tr.data[i];
  2289. if (data) {
  2290. list_for_each_entry_safe(page, tmp,
  2291. &data->trace_pages, lru) {
  2292. list_del_init(&page->lru);
  2293. __free_page(page);
  2294. }
  2295. }
  2296. #endif
  2297. }
  2298. return ret;
  2299. }
  2300. fs_initcall(tracer_alloc_buffers);