trace-event-parse.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. /*
  2. * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License (not later!)
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. *
  21. * The parts for function graph printing was taken and modified from the
  22. * Linux Kernel that were written by Frederic Weisbecker.
  23. */
  24. #define _GNU_SOURCE
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #undef _GNU_SOURCE
  31. #include "../perf.h"
  32. #include "util.h"
  33. #include "trace-event.h"
  34. int header_page_ts_offset;
  35. int header_page_ts_size;
  36. int header_page_size_offset;
  37. int header_page_size_size;
  38. int header_page_data_offset;
  39. int header_page_data_size;
  40. static char *input_buf;
  41. static unsigned long long input_buf_ptr;
  42. static unsigned long long input_buf_siz;
  43. static int cpus;
  44. static int long_size;
  45. static void init_input_buf(char *buf, unsigned long long size)
  46. {
  47. input_buf = buf;
  48. input_buf_siz = size;
  49. input_buf_ptr = 0;
  50. }
  51. struct cmdline {
  52. char *comm;
  53. int pid;
  54. };
  55. static struct cmdline *cmdlines;
  56. static int cmdline_count;
  57. static int cmdline_cmp(const void *a, const void *b)
  58. {
  59. const struct cmdline *ca = a;
  60. const struct cmdline *cb = b;
  61. if (ca->pid < cb->pid)
  62. return -1;
  63. if (ca->pid > cb->pid)
  64. return 1;
  65. return 0;
  66. }
  67. void parse_cmdlines(char *file, int size __unused)
  68. {
  69. struct cmdline_list {
  70. struct cmdline_list *next;
  71. char *comm;
  72. int pid;
  73. } *list = NULL, *item;
  74. char *line;
  75. char *next = NULL;
  76. int i;
  77. line = strtok_r(file, "\n", &next);
  78. while (line) {
  79. item = malloc_or_die(sizeof(*item));
  80. sscanf(line, "%d %as", &item->pid,
  81. (float *)(void *)&item->comm); /* workaround gcc warning */
  82. item->next = list;
  83. list = item;
  84. line = strtok_r(NULL, "\n", &next);
  85. cmdline_count++;
  86. }
  87. cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
  88. i = 0;
  89. while (list) {
  90. cmdlines[i].pid = list->pid;
  91. cmdlines[i].comm = list->comm;
  92. i++;
  93. item = list;
  94. list = list->next;
  95. free(item);
  96. }
  97. qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  98. }
  99. static struct func_map {
  100. unsigned long long addr;
  101. char *func;
  102. char *mod;
  103. } *func_list;
  104. static unsigned int func_count;
  105. static int func_cmp(const void *a, const void *b)
  106. {
  107. const struct func_map *fa = a;
  108. const struct func_map *fb = b;
  109. if (fa->addr < fb->addr)
  110. return -1;
  111. if (fa->addr > fb->addr)
  112. return 1;
  113. return 0;
  114. }
  115. void parse_proc_kallsyms(char *file, unsigned int size __unused)
  116. {
  117. struct func_list {
  118. struct func_list *next;
  119. unsigned long long addr;
  120. char *func;
  121. char *mod;
  122. } *list = NULL, *item;
  123. char *line;
  124. char *next = NULL;
  125. char *addr_str;
  126. char ch;
  127. int ret;
  128. int i;
  129. line = strtok_r(file, "\n", &next);
  130. while (line) {
  131. item = malloc_or_die(sizeof(*item));
  132. item->mod = NULL;
  133. ret = sscanf(line, "%as %c %as\t[%as",
  134. (float *)(void *)&addr_str, /* workaround gcc warning */
  135. &ch,
  136. (float *)(void *)&item->func,
  137. (float *)(void *)&item->mod);
  138. item->addr = strtoull(addr_str, NULL, 16);
  139. free(addr_str);
  140. /* truncate the extra ']' */
  141. if (item->mod)
  142. item->mod[strlen(item->mod) - 1] = 0;
  143. item->next = list;
  144. list = item;
  145. line = strtok_r(NULL, "\n", &next);
  146. func_count++;
  147. }
  148. func_list = malloc_or_die(sizeof(*func_list) * func_count + 1);
  149. i = 0;
  150. while (list) {
  151. func_list[i].func = list->func;
  152. func_list[i].addr = list->addr;
  153. func_list[i].mod = list->mod;
  154. i++;
  155. item = list;
  156. list = list->next;
  157. free(item);
  158. }
  159. qsort(func_list, func_count, sizeof(*func_list), func_cmp);
  160. /*
  161. * Add a special record at the end.
  162. */
  163. func_list[func_count].func = NULL;
  164. func_list[func_count].addr = 0;
  165. func_list[func_count].mod = NULL;
  166. }
  167. /*
  168. * We are searching for a record in between, not an exact
  169. * match.
  170. */
  171. static int func_bcmp(const void *a, const void *b)
  172. {
  173. const struct func_map *fa = a;
  174. const struct func_map *fb = b;
  175. if ((fa->addr == fb->addr) ||
  176. (fa->addr > fb->addr &&
  177. fa->addr < (fb+1)->addr))
  178. return 0;
  179. if (fa->addr < fb->addr)
  180. return -1;
  181. return 1;
  182. }
  183. static struct func_map *find_func(unsigned long long addr)
  184. {
  185. struct func_map *func;
  186. struct func_map key;
  187. key.addr = addr;
  188. func = bsearch(&key, func_list, func_count, sizeof(*func_list),
  189. func_bcmp);
  190. return func;
  191. }
  192. void print_funcs(void)
  193. {
  194. int i;
  195. for (i = 0; i < (int)func_count; i++) {
  196. printf("%016llx %s",
  197. func_list[i].addr,
  198. func_list[i].func);
  199. if (func_list[i].mod)
  200. printf(" [%s]\n", func_list[i].mod);
  201. else
  202. printf("\n");
  203. }
  204. }
  205. static struct printk_map {
  206. unsigned long long addr;
  207. char *printk;
  208. } *printk_list;
  209. static unsigned int printk_count;
  210. static int printk_cmp(const void *a, const void *b)
  211. {
  212. const struct func_map *fa = a;
  213. const struct func_map *fb = b;
  214. if (fa->addr < fb->addr)
  215. return -1;
  216. if (fa->addr > fb->addr)
  217. return 1;
  218. return 0;
  219. }
  220. static struct printk_map *find_printk(unsigned long long addr)
  221. {
  222. struct printk_map *printk;
  223. struct printk_map key;
  224. key.addr = addr;
  225. printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
  226. printk_cmp);
  227. return printk;
  228. }
  229. void parse_ftrace_printk(char *file, unsigned int size __unused)
  230. {
  231. struct printk_list {
  232. struct printk_list *next;
  233. unsigned long long addr;
  234. char *printk;
  235. } *list = NULL, *item;
  236. char *line;
  237. char *next = NULL;
  238. char *addr_str;
  239. int ret;
  240. int i;
  241. line = strtok_r(file, "\n", &next);
  242. while (line) {
  243. item = malloc_or_die(sizeof(*item));
  244. ret = sscanf(line, "%as : %as",
  245. (float *)(void *)&addr_str, /* workaround gcc warning */
  246. (float *)(void *)&item->printk);
  247. item->addr = strtoull(addr_str, NULL, 16);
  248. free(addr_str);
  249. item->next = list;
  250. list = item;
  251. line = strtok_r(NULL, "\n", &next);
  252. printk_count++;
  253. }
  254. printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
  255. i = 0;
  256. while (list) {
  257. printk_list[i].printk = list->printk;
  258. printk_list[i].addr = list->addr;
  259. i++;
  260. item = list;
  261. list = list->next;
  262. free(item);
  263. }
  264. qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
  265. }
  266. void print_printk(void)
  267. {
  268. int i;
  269. for (i = 0; i < (int)printk_count; i++) {
  270. printf("%016llx %s\n",
  271. printk_list[i].addr,
  272. printk_list[i].printk);
  273. }
  274. }
  275. static struct event *alloc_event(void)
  276. {
  277. struct event *event;
  278. event = malloc_or_die(sizeof(*event));
  279. memset(event, 0, sizeof(*event));
  280. return event;
  281. }
  282. enum event_type {
  283. EVENT_ERROR,
  284. EVENT_NONE,
  285. EVENT_SPACE,
  286. EVENT_NEWLINE,
  287. EVENT_OP,
  288. EVENT_DELIM,
  289. EVENT_ITEM,
  290. EVENT_DQUOTE,
  291. EVENT_SQUOTE,
  292. };
  293. static struct event *event_list;
  294. static void add_event(struct event *event)
  295. {
  296. event->next = event_list;
  297. event_list = event;
  298. }
  299. static int event_item_type(enum event_type type)
  300. {
  301. switch (type) {
  302. case EVENT_ITEM ... EVENT_SQUOTE:
  303. return 1;
  304. case EVENT_ERROR ... EVENT_DELIM:
  305. default:
  306. return 0;
  307. }
  308. }
  309. static void free_arg(struct print_arg *arg)
  310. {
  311. if (!arg)
  312. return;
  313. switch (arg->type) {
  314. case PRINT_ATOM:
  315. if (arg->atom.atom)
  316. free(arg->atom.atom);
  317. break;
  318. case PRINT_NULL:
  319. case PRINT_FIELD ... PRINT_OP:
  320. default:
  321. /* todo */
  322. break;
  323. }
  324. free(arg);
  325. }
  326. static enum event_type get_type(int ch)
  327. {
  328. if (ch == '\n')
  329. return EVENT_NEWLINE;
  330. if (isspace(ch))
  331. return EVENT_SPACE;
  332. if (isalnum(ch) || ch == '_')
  333. return EVENT_ITEM;
  334. if (ch == '\'')
  335. return EVENT_SQUOTE;
  336. if (ch == '"')
  337. return EVENT_DQUOTE;
  338. if (!isprint(ch))
  339. return EVENT_NONE;
  340. if (ch == '(' || ch == ')' || ch == ',')
  341. return EVENT_DELIM;
  342. return EVENT_OP;
  343. }
  344. static int __read_char(void)
  345. {
  346. if (input_buf_ptr >= input_buf_siz)
  347. return -1;
  348. return input_buf[input_buf_ptr++];
  349. }
  350. static int __peek_char(void)
  351. {
  352. if (input_buf_ptr >= input_buf_siz)
  353. return -1;
  354. return input_buf[input_buf_ptr];
  355. }
  356. static enum event_type __read_token(char **tok)
  357. {
  358. char buf[BUFSIZ];
  359. int ch, last_ch, quote_ch, next_ch;
  360. int i = 0;
  361. int tok_size = 0;
  362. enum event_type type;
  363. *tok = NULL;
  364. ch = __read_char();
  365. if (ch < 0)
  366. return EVENT_NONE;
  367. type = get_type(ch);
  368. if (type == EVENT_NONE)
  369. return type;
  370. buf[i++] = ch;
  371. switch (type) {
  372. case EVENT_NEWLINE:
  373. case EVENT_DELIM:
  374. *tok = malloc_or_die(2);
  375. (*tok)[0] = ch;
  376. (*tok)[1] = 0;
  377. return type;
  378. case EVENT_OP:
  379. switch (ch) {
  380. case '-':
  381. next_ch = __peek_char();
  382. if (next_ch == '>') {
  383. buf[i++] = __read_char();
  384. break;
  385. }
  386. /* fall through */
  387. case '+':
  388. case '|':
  389. case '&':
  390. case '>':
  391. case '<':
  392. last_ch = ch;
  393. ch = __peek_char();
  394. if (ch != last_ch)
  395. goto test_equal;
  396. buf[i++] = __read_char();
  397. switch (last_ch) {
  398. case '>':
  399. case '<':
  400. goto test_equal;
  401. default:
  402. break;
  403. }
  404. break;
  405. case '!':
  406. case '=':
  407. goto test_equal;
  408. default: /* what should we do instead? */
  409. break;
  410. }
  411. buf[i] = 0;
  412. *tok = strdup(buf);
  413. return type;
  414. test_equal:
  415. ch = __peek_char();
  416. if (ch == '=')
  417. buf[i++] = __read_char();
  418. break;
  419. case EVENT_DQUOTE:
  420. case EVENT_SQUOTE:
  421. /* don't keep quotes */
  422. i--;
  423. quote_ch = ch;
  424. last_ch = 0;
  425. do {
  426. if (i == (BUFSIZ - 1)) {
  427. buf[i] = 0;
  428. if (*tok) {
  429. *tok = realloc(*tok, tok_size + BUFSIZ);
  430. if (!*tok)
  431. return EVENT_NONE;
  432. strcat(*tok, buf);
  433. } else
  434. *tok = strdup(buf);
  435. if (!*tok)
  436. return EVENT_NONE;
  437. tok_size += BUFSIZ;
  438. i = 0;
  439. }
  440. last_ch = ch;
  441. ch = __read_char();
  442. buf[i++] = ch;
  443. } while (ch != quote_ch && last_ch != '\\');
  444. /* remove the last quote */
  445. i--;
  446. goto out;
  447. case EVENT_ERROR ... EVENT_SPACE:
  448. case EVENT_ITEM:
  449. default:
  450. break;
  451. }
  452. while (get_type(__peek_char()) == type) {
  453. if (i == (BUFSIZ - 1)) {
  454. buf[i] = 0;
  455. if (*tok) {
  456. *tok = realloc(*tok, tok_size + BUFSIZ);
  457. if (!*tok)
  458. return EVENT_NONE;
  459. strcat(*tok, buf);
  460. } else
  461. *tok = strdup(buf);
  462. if (!*tok)
  463. return EVENT_NONE;
  464. tok_size += BUFSIZ;
  465. i = 0;
  466. }
  467. ch = __read_char();
  468. buf[i++] = ch;
  469. }
  470. out:
  471. buf[i] = 0;
  472. if (*tok) {
  473. *tok = realloc(*tok, tok_size + i);
  474. if (!*tok)
  475. return EVENT_NONE;
  476. strcat(*tok, buf);
  477. } else
  478. *tok = strdup(buf);
  479. if (!*tok)
  480. return EVENT_NONE;
  481. return type;
  482. }
  483. static void free_token(char *tok)
  484. {
  485. if (tok)
  486. free(tok);
  487. }
  488. static enum event_type read_token(char **tok)
  489. {
  490. enum event_type type;
  491. for (;;) {
  492. type = __read_token(tok);
  493. if (type != EVENT_SPACE)
  494. return type;
  495. free_token(*tok);
  496. }
  497. /* not reached */
  498. return EVENT_NONE;
  499. }
  500. /* no newline */
  501. static enum event_type read_token_item(char **tok)
  502. {
  503. enum event_type type;
  504. for (;;) {
  505. type = __read_token(tok);
  506. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  507. return type;
  508. free_token(*tok);
  509. }
  510. /* not reached */
  511. return EVENT_NONE;
  512. }
  513. static int test_type(enum event_type type, enum event_type expect)
  514. {
  515. if (type != expect) {
  516. die("Error: expected type %d but read %d",
  517. expect, type);
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. static int test_type_token(enum event_type type, char *token,
  523. enum event_type expect, char *expect_tok)
  524. {
  525. if (type != expect) {
  526. die("Error: expected type %d but read %d",
  527. expect, type);
  528. return -1;
  529. }
  530. if (strcmp(token, expect_tok) != 0) {
  531. die("Error: expected '%s' but read '%s'",
  532. expect_tok, token);
  533. return -1;
  534. }
  535. return 0;
  536. }
  537. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  538. {
  539. enum event_type type;
  540. if (newline_ok)
  541. type = read_token(tok);
  542. else
  543. type = read_token_item(tok);
  544. return test_type(type, expect);
  545. }
  546. static int read_expect_type(enum event_type expect, char **tok)
  547. {
  548. return __read_expect_type(expect, tok, 1);
  549. }
  550. static int __read_expected(enum event_type expect, char *str, int newline_ok)
  551. {
  552. enum event_type type;
  553. char *token;
  554. int ret;
  555. if (newline_ok)
  556. type = read_token(&token);
  557. else
  558. type = read_token_item(&token);
  559. ret = test_type_token(type, token, expect, str);
  560. free_token(token);
  561. return 0;
  562. }
  563. static int read_expected(enum event_type expect, char *str)
  564. {
  565. return __read_expected(expect, str, 1);
  566. }
  567. static int read_expected_item(enum event_type expect, char *str)
  568. {
  569. return __read_expected(expect, str, 0);
  570. }
  571. static char *event_read_name(void)
  572. {
  573. char *token;
  574. if (read_expected(EVENT_ITEM, (char *)"name") < 0)
  575. return NULL;
  576. if (read_expected(EVENT_OP, (char *)":") < 0)
  577. return NULL;
  578. if (read_expect_type(EVENT_ITEM, &token) < 0)
  579. goto fail;
  580. return token;
  581. fail:
  582. free_token(token);
  583. return NULL;
  584. }
  585. static int event_read_id(void)
  586. {
  587. char *token;
  588. int id;
  589. if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
  590. return -1;
  591. if (read_expected(EVENT_OP, (char *)":") < 0)
  592. return -1;
  593. if (read_expect_type(EVENT_ITEM, &token) < 0)
  594. goto fail;
  595. id = strtoul(token, NULL, 0);
  596. free_token(token);
  597. return id;
  598. fail:
  599. free_token(token);
  600. return -1;
  601. }
  602. static int event_read_fields(struct event *event, struct format_field **fields)
  603. {
  604. struct format_field *field = NULL;
  605. enum event_type type;
  606. char *token;
  607. char *last_token;
  608. int count = 0;
  609. do {
  610. type = read_token(&token);
  611. if (type == EVENT_NEWLINE) {
  612. free_token(token);
  613. return count;
  614. }
  615. count++;
  616. if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
  617. goto fail;
  618. free_token(token);
  619. type = read_token(&token);
  620. /*
  621. * The ftrace fields may still use the "special" name.
  622. * Just ignore it.
  623. */
  624. if (event->flags & EVENT_FL_ISFTRACE &&
  625. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  626. free_token(token);
  627. type = read_token(&token);
  628. }
  629. if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
  630. return -1;
  631. if (read_expect_type(EVENT_ITEM, &token) < 0)
  632. goto fail;
  633. last_token = token;
  634. field = malloc_or_die(sizeof(*field));
  635. memset(field, 0, sizeof(*field));
  636. /* read the rest of the type */
  637. for (;;) {
  638. type = read_token(&token);
  639. if (type == EVENT_ITEM ||
  640. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  641. /*
  642. * Some of the ftrace fields are broken and have
  643. * an illegal "." in them.
  644. */
  645. (event->flags & EVENT_FL_ISFTRACE &&
  646. type == EVENT_OP && strcmp(token, ".") == 0)) {
  647. if (strcmp(token, "*") == 0)
  648. field->flags |= FIELD_IS_POINTER;
  649. if (field->type) {
  650. field->type = realloc(field->type,
  651. strlen(field->type) +
  652. strlen(last_token) + 2);
  653. strcat(field->type, " ");
  654. strcat(field->type, last_token);
  655. } else
  656. field->type = last_token;
  657. last_token = token;
  658. continue;
  659. }
  660. break;
  661. }
  662. if (!field->type) {
  663. die("no type found");
  664. goto fail;
  665. }
  666. field->name = last_token;
  667. if (test_type(type, EVENT_OP))
  668. goto fail;
  669. if (strcmp(token, "[") == 0) {
  670. enum event_type last_type = type;
  671. char *brackets = token;
  672. int len;
  673. field->flags |= FIELD_IS_ARRAY;
  674. type = read_token(&token);
  675. while (strcmp(token, "]") != 0) {
  676. if (last_type == EVENT_ITEM &&
  677. type == EVENT_ITEM)
  678. len = 2;
  679. else
  680. len = 1;
  681. last_type = type;
  682. brackets = realloc(brackets,
  683. strlen(brackets) +
  684. strlen(token) + len);
  685. if (len == 2)
  686. strcat(brackets, " ");
  687. strcat(brackets, token);
  688. free_token(token);
  689. type = read_token(&token);
  690. if (type == EVENT_NONE) {
  691. die("failed to find token");
  692. goto fail;
  693. }
  694. }
  695. free_token(token);
  696. brackets = realloc(brackets, strlen(brackets) + 2);
  697. strcat(brackets, "]");
  698. /* add brackets to type */
  699. type = read_token(&token);
  700. /*
  701. * If the next token is not an OP, then it is of
  702. * the format: type [] item;
  703. */
  704. if (type == EVENT_ITEM) {
  705. field->type = realloc(field->type,
  706. strlen(field->type) +
  707. strlen(field->name) +
  708. strlen(brackets) + 2);
  709. strcat(field->type, " ");
  710. strcat(field->type, field->name);
  711. free_token(field->name);
  712. strcat(field->type, brackets);
  713. field->name = token;
  714. type = read_token(&token);
  715. } else {
  716. field->type = realloc(field->type,
  717. strlen(field->type) +
  718. strlen(brackets) + 1);
  719. strcat(field->type, brackets);
  720. }
  721. free(brackets);
  722. }
  723. if (test_type_token(type, token, EVENT_OP, (char *)";"))
  724. goto fail;
  725. free_token(token);
  726. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  727. goto fail_expect;
  728. if (read_expected(EVENT_OP, (char *)":") < 0)
  729. goto fail_expect;
  730. if (read_expect_type(EVENT_ITEM, &token))
  731. goto fail;
  732. field->offset = strtoul(token, NULL, 0);
  733. free_token(token);
  734. if (read_expected(EVENT_OP, (char *)";") < 0)
  735. goto fail_expect;
  736. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  737. goto fail_expect;
  738. if (read_expected(EVENT_OP, (char *)":") < 0)
  739. goto fail_expect;
  740. if (read_expect_type(EVENT_ITEM, &token))
  741. goto fail;
  742. field->size = strtoul(token, NULL, 0);
  743. free_token(token);
  744. if (read_expected(EVENT_OP, (char *)";") < 0)
  745. goto fail_expect;
  746. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  747. goto fail;
  748. free_token(token);
  749. *fields = field;
  750. fields = &field->next;
  751. } while (1);
  752. return 0;
  753. fail:
  754. free_token(token);
  755. fail_expect:
  756. if (field)
  757. free(field);
  758. return -1;
  759. }
  760. static int event_read_format(struct event *event)
  761. {
  762. char *token;
  763. int ret;
  764. if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
  765. return -1;
  766. if (read_expected(EVENT_OP, (char *)":") < 0)
  767. return -1;
  768. if (read_expect_type(EVENT_NEWLINE, &token))
  769. goto fail;
  770. free_token(token);
  771. ret = event_read_fields(event, &event->format.common_fields);
  772. if (ret < 0)
  773. return ret;
  774. event->format.nr_common = ret;
  775. ret = event_read_fields(event, &event->format.fields);
  776. if (ret < 0)
  777. return ret;
  778. event->format.nr_fields = ret;
  779. return 0;
  780. fail:
  781. free_token(token);
  782. return -1;
  783. }
  784. enum event_type
  785. process_arg_token(struct event *event, struct print_arg *arg,
  786. char **tok, enum event_type type);
  787. static enum event_type
  788. process_arg(struct event *event, struct print_arg *arg, char **tok)
  789. {
  790. enum event_type type;
  791. char *token;
  792. type = read_token(&token);
  793. *tok = token;
  794. return process_arg_token(event, arg, tok, type);
  795. }
  796. static enum event_type
  797. process_cond(struct event *event, struct print_arg *top, char **tok)
  798. {
  799. struct print_arg *arg, *left, *right;
  800. enum event_type type;
  801. char *token = NULL;
  802. arg = malloc_or_die(sizeof(*arg));
  803. memset(arg, 0, sizeof(*arg));
  804. left = malloc_or_die(sizeof(*left));
  805. right = malloc_or_die(sizeof(*right));
  806. arg->type = PRINT_OP;
  807. arg->op.left = left;
  808. arg->op.right = right;
  809. *tok = NULL;
  810. type = process_arg(event, left, &token);
  811. if (test_type_token(type, token, EVENT_OP, (char *)":"))
  812. goto out_free;
  813. arg->op.op = token;
  814. type = process_arg(event, right, &token);
  815. top->op.right = arg;
  816. *tok = token;
  817. return type;
  818. out_free:
  819. free_token(*tok);
  820. free(right);
  821. free(left);
  822. free_arg(arg);
  823. return EVENT_ERROR;
  824. }
  825. static int get_op_prio(char *op)
  826. {
  827. if (!op[1]) {
  828. switch (op[0]) {
  829. case '*':
  830. case '/':
  831. case '%':
  832. return 6;
  833. case '+':
  834. case '-':
  835. return 7;
  836. /* '>>' and '<<' are 8 */
  837. case '<':
  838. case '>':
  839. return 9;
  840. /* '==' and '!=' are 10 */
  841. case '&':
  842. return 11;
  843. case '^':
  844. return 12;
  845. case '|':
  846. return 13;
  847. case '?':
  848. return 16;
  849. default:
  850. die("unknown op '%c'", op[0]);
  851. return -1;
  852. }
  853. } else {
  854. if (strcmp(op, "++") == 0 ||
  855. strcmp(op, "--") == 0) {
  856. return 3;
  857. } else if (strcmp(op, ">>") == 0 ||
  858. strcmp(op, "<<") == 0) {
  859. return 8;
  860. } else if (strcmp(op, ">=") == 0 ||
  861. strcmp(op, "<=") == 0) {
  862. return 9;
  863. } else if (strcmp(op, "==") == 0 ||
  864. strcmp(op, "!=") == 0) {
  865. return 10;
  866. } else if (strcmp(op, "&&") == 0) {
  867. return 14;
  868. } else if (strcmp(op, "||") == 0) {
  869. return 15;
  870. } else {
  871. die("unknown op '%s'", op);
  872. return -1;
  873. }
  874. }
  875. }
  876. static void set_op_prio(struct print_arg *arg)
  877. {
  878. /* single ops are the greatest */
  879. if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
  880. arg->op.prio = 0;
  881. return;
  882. }
  883. arg->op.prio = get_op_prio(arg->op.op);
  884. }
  885. static enum event_type
  886. process_op(struct event *event, struct print_arg *arg, char **tok)
  887. {
  888. struct print_arg *left, *right = NULL;
  889. enum event_type type;
  890. char *token;
  891. /* the op is passed in via tok */
  892. token = *tok;
  893. if (arg->type == PRINT_OP && !arg->op.left) {
  894. /* handle single op */
  895. if (token[1]) {
  896. die("bad op token %s", token);
  897. return EVENT_ERROR;
  898. }
  899. switch (token[0]) {
  900. case '!':
  901. case '+':
  902. case '-':
  903. break;
  904. default:
  905. die("bad op token %s", token);
  906. return EVENT_ERROR;
  907. }
  908. /* make an empty left */
  909. left = malloc_or_die(sizeof(*left));
  910. left->type = PRINT_NULL;
  911. arg->op.left = left;
  912. right = malloc_or_die(sizeof(*right));
  913. arg->op.right = right;
  914. type = process_arg(event, right, tok);
  915. } else if (strcmp(token, "?") == 0) {
  916. left = malloc_or_die(sizeof(*left));
  917. /* copy the top arg to the left */
  918. *left = *arg;
  919. arg->type = PRINT_OP;
  920. arg->op.op = token;
  921. arg->op.left = left;
  922. arg->op.prio = 0;
  923. type = process_cond(event, arg, tok);
  924. } else if (strcmp(token, ">>") == 0 ||
  925. strcmp(token, "<<") == 0 ||
  926. strcmp(token, "&") == 0 ||
  927. strcmp(token, "|") == 0 ||
  928. strcmp(token, "&&") == 0 ||
  929. strcmp(token, "||") == 0 ||
  930. strcmp(token, "-") == 0 ||
  931. strcmp(token, "+") == 0 ||
  932. strcmp(token, "*") == 0 ||
  933. strcmp(token, "^") == 0 ||
  934. strcmp(token, "/") == 0 ||
  935. strcmp(token, "==") == 0 ||
  936. strcmp(token, "!=") == 0) {
  937. left = malloc_or_die(sizeof(*left));
  938. /* copy the top arg to the left */
  939. *left = *arg;
  940. arg->type = PRINT_OP;
  941. arg->op.op = token;
  942. arg->op.left = left;
  943. set_op_prio(arg);
  944. right = malloc_or_die(sizeof(*right));
  945. type = process_arg(event, right, tok);
  946. arg->op.right = right;
  947. } else {
  948. die("unknown op '%s'", token);
  949. /* the arg is now the left side */
  950. return EVENT_NONE;
  951. }
  952. if (type == EVENT_OP) {
  953. int prio;
  954. /* higher prios need to be closer to the root */
  955. prio = get_op_prio(*tok);
  956. if (prio > arg->op.prio)
  957. return process_op(event, arg, tok);
  958. return process_op(event, right, tok);
  959. }
  960. return type;
  961. }
  962. static enum event_type
  963. process_entry(struct event *event __unused, struct print_arg *arg,
  964. char **tok)
  965. {
  966. enum event_type type;
  967. char *field;
  968. char *token;
  969. if (read_expected(EVENT_OP, (char *)"->") < 0)
  970. return EVENT_ERROR;
  971. if (read_expect_type(EVENT_ITEM, &token) < 0)
  972. goto fail;
  973. field = token;
  974. arg->type = PRINT_FIELD;
  975. arg->field.name = field;
  976. type = read_token(&token);
  977. *tok = token;
  978. return type;
  979. fail:
  980. free_token(token);
  981. return EVENT_ERROR;
  982. }
  983. static char *arg_eval (struct print_arg *arg);
  984. static long long arg_num_eval(struct print_arg *arg)
  985. {
  986. long long left, right;
  987. long long val = 0;
  988. switch (arg->type) {
  989. case PRINT_ATOM:
  990. val = strtoll(arg->atom.atom, NULL, 0);
  991. break;
  992. case PRINT_TYPE:
  993. val = arg_num_eval(arg->typecast.item);
  994. break;
  995. case PRINT_OP:
  996. switch (arg->op.op[0]) {
  997. case '|':
  998. left = arg_num_eval(arg->op.left);
  999. right = arg_num_eval(arg->op.right);
  1000. if (arg->op.op[1])
  1001. val = left || right;
  1002. else
  1003. val = left | right;
  1004. break;
  1005. case '&':
  1006. left = arg_num_eval(arg->op.left);
  1007. right = arg_num_eval(arg->op.right);
  1008. if (arg->op.op[1])
  1009. val = left && right;
  1010. else
  1011. val = left & right;
  1012. break;
  1013. case '<':
  1014. left = arg_num_eval(arg->op.left);
  1015. right = arg_num_eval(arg->op.right);
  1016. switch (arg->op.op[1]) {
  1017. case 0:
  1018. val = left < right;
  1019. break;
  1020. case '<':
  1021. val = left << right;
  1022. break;
  1023. case '=':
  1024. val = left <= right;
  1025. break;
  1026. default:
  1027. die("unknown op '%s'", arg->op.op);
  1028. }
  1029. break;
  1030. case '>':
  1031. left = arg_num_eval(arg->op.left);
  1032. right = arg_num_eval(arg->op.right);
  1033. switch (arg->op.op[1]) {
  1034. case 0:
  1035. val = left > right;
  1036. break;
  1037. case '>':
  1038. val = left >> right;
  1039. break;
  1040. case '=':
  1041. val = left >= right;
  1042. break;
  1043. default:
  1044. die("unknown op '%s'", arg->op.op);
  1045. }
  1046. break;
  1047. case '=':
  1048. left = arg_num_eval(arg->op.left);
  1049. right = arg_num_eval(arg->op.right);
  1050. if (arg->op.op[1] != '=')
  1051. die("unknown op '%s'", arg->op.op);
  1052. val = left == right;
  1053. break;
  1054. case '!':
  1055. left = arg_num_eval(arg->op.left);
  1056. right = arg_num_eval(arg->op.right);
  1057. switch (arg->op.op[1]) {
  1058. case '=':
  1059. val = left != right;
  1060. break;
  1061. default:
  1062. die("unknown op '%s'", arg->op.op);
  1063. }
  1064. break;
  1065. default:
  1066. die("unknown op '%s'", arg->op.op);
  1067. }
  1068. break;
  1069. case PRINT_NULL:
  1070. case PRINT_FIELD ... PRINT_SYMBOL:
  1071. case PRINT_STRING:
  1072. default:
  1073. die("invalid eval type %d", arg->type);
  1074. }
  1075. return val;
  1076. }
  1077. static char *arg_eval (struct print_arg *arg)
  1078. {
  1079. long long val;
  1080. static char buf[20];
  1081. switch (arg->type) {
  1082. case PRINT_ATOM:
  1083. return arg->atom.atom;
  1084. case PRINT_TYPE:
  1085. return arg_eval(arg->typecast.item);
  1086. case PRINT_OP:
  1087. val = arg_num_eval(arg);
  1088. sprintf(buf, "%lld", val);
  1089. return buf;
  1090. case PRINT_NULL:
  1091. case PRINT_FIELD ... PRINT_SYMBOL:
  1092. case PRINT_STRING:
  1093. default:
  1094. die("invalid eval type %d", arg->type);
  1095. break;
  1096. }
  1097. return NULL;
  1098. }
  1099. static enum event_type
  1100. process_fields(struct event *event, struct print_flag_sym **list, char **tok)
  1101. {
  1102. enum event_type type;
  1103. struct print_arg *arg = NULL;
  1104. struct print_flag_sym *field;
  1105. char *token = NULL;
  1106. char *value;
  1107. do {
  1108. free_token(token);
  1109. type = read_token_item(&token);
  1110. if (test_type_token(type, token, EVENT_OP, (char *)"{"))
  1111. break;
  1112. arg = malloc_or_die(sizeof(*arg));
  1113. free_token(token);
  1114. type = process_arg(event, arg, &token);
  1115. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1116. goto out_free;
  1117. field = malloc_or_die(sizeof(*field));
  1118. memset(field, 0, sizeof(field));
  1119. value = arg_eval(arg);
  1120. field->value = strdup(value);
  1121. free_token(token);
  1122. type = process_arg(event, arg, &token);
  1123. if (test_type_token(type, token, EVENT_OP, (char *)"}"))
  1124. goto out_free;
  1125. value = arg_eval(arg);
  1126. field->str = strdup(value);
  1127. free_arg(arg);
  1128. arg = NULL;
  1129. *list = field;
  1130. list = &field->next;
  1131. free_token(token);
  1132. type = read_token_item(&token);
  1133. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1134. *tok = token;
  1135. return type;
  1136. out_free:
  1137. free_arg(arg);
  1138. free_token(token);
  1139. return EVENT_ERROR;
  1140. }
  1141. static enum event_type
  1142. process_flags(struct event *event, struct print_arg *arg, char **tok)
  1143. {
  1144. struct print_arg *field;
  1145. enum event_type type;
  1146. char *token;
  1147. memset(arg, 0, sizeof(*arg));
  1148. arg->type = PRINT_FLAGS;
  1149. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1150. return EVENT_ERROR;
  1151. field = malloc_or_die(sizeof(*field));
  1152. type = process_arg(event, field, &token);
  1153. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1154. goto out_free;
  1155. arg->flags.field = field;
  1156. type = read_token_item(&token);
  1157. if (event_item_type(type)) {
  1158. arg->flags.delim = token;
  1159. type = read_token_item(&token);
  1160. }
  1161. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1162. goto out_free;
  1163. type = process_fields(event, &arg->flags.flags, &token);
  1164. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1165. goto out_free;
  1166. free_token(token);
  1167. type = read_token_item(tok);
  1168. return type;
  1169. out_free:
  1170. free_token(token);
  1171. return EVENT_ERROR;
  1172. }
  1173. static enum event_type
  1174. process_symbols(struct event *event, struct print_arg *arg, char **tok)
  1175. {
  1176. struct print_arg *field;
  1177. enum event_type type;
  1178. char *token;
  1179. memset(arg, 0, sizeof(*arg));
  1180. arg->type = PRINT_SYMBOL;
  1181. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1182. return EVENT_ERROR;
  1183. field = malloc_or_die(sizeof(*field));
  1184. type = process_arg(event, field, &token);
  1185. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1186. goto out_free;
  1187. arg->symbol.field = field;
  1188. type = process_fields(event, &arg->symbol.symbols, &token);
  1189. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1190. goto out_free;
  1191. free_token(token);
  1192. type = read_token_item(tok);
  1193. return type;
  1194. out_free:
  1195. free_token(token);
  1196. return EVENT_ERROR;
  1197. }
  1198. static enum event_type
  1199. process_paren(struct event *event, struct print_arg *arg, char **tok)
  1200. {
  1201. struct print_arg *item_arg;
  1202. enum event_type type;
  1203. int ptr_cast = 0;
  1204. char *token;
  1205. type = process_arg(event, arg, &token);
  1206. if (type == EVENT_ERROR)
  1207. return EVENT_ERROR;
  1208. if (type == EVENT_OP) {
  1209. /* handle the ptr casts */
  1210. if (!strcmp(token, "*")) {
  1211. /*
  1212. * FIXME: should we zapp whitespaces before ')' ?
  1213. * (may require a peek_token_item())
  1214. */
  1215. if (__peek_char() == ')') {
  1216. ptr_cast = 1;
  1217. free_token(token);
  1218. type = read_token_item(&token);
  1219. }
  1220. }
  1221. if (!ptr_cast) {
  1222. type = process_op(event, arg, &token);
  1223. if (type == EVENT_ERROR)
  1224. return EVENT_ERROR;
  1225. }
  1226. }
  1227. if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
  1228. free_token(token);
  1229. return EVENT_ERROR;
  1230. }
  1231. free_token(token);
  1232. type = read_token_item(&token);
  1233. /*
  1234. * If the next token is an item or another open paren, then
  1235. * this was a typecast.
  1236. */
  1237. if (event_item_type(type) ||
  1238. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1239. /* make this a typecast and contine */
  1240. /* prevous must be an atom */
  1241. if (arg->type != PRINT_ATOM)
  1242. die("previous needed to be PRINT_ATOM");
  1243. item_arg = malloc_or_die(sizeof(*item_arg));
  1244. arg->type = PRINT_TYPE;
  1245. if (ptr_cast) {
  1246. char *old = arg->atom.atom;
  1247. arg->atom.atom = malloc_or_die(strlen(old + 3));
  1248. sprintf(arg->atom.atom, "%s *", old);
  1249. free(old);
  1250. }
  1251. arg->typecast.type = arg->atom.atom;
  1252. arg->typecast.item = item_arg;
  1253. type = process_arg_token(event, item_arg, &token, type);
  1254. }
  1255. *tok = token;
  1256. return type;
  1257. }
  1258. static enum event_type
  1259. process_str(struct event *event __unused, struct print_arg *arg, char **tok)
  1260. {
  1261. enum event_type type;
  1262. char *token;
  1263. if (read_expected(EVENT_DELIM, (char *)"(") < 0)
  1264. return EVENT_ERROR;
  1265. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1266. goto fail;
  1267. arg->type = PRINT_STRING;
  1268. arg->string.string = token;
  1269. arg->string.offset = -1;
  1270. if (read_expected(EVENT_DELIM, (char *)")") < 0)
  1271. return EVENT_ERROR;
  1272. type = read_token(&token);
  1273. *tok = token;
  1274. return type;
  1275. fail:
  1276. free_token(token);
  1277. return EVENT_ERROR;
  1278. }
  1279. enum event_type
  1280. process_arg_token(struct event *event, struct print_arg *arg,
  1281. char **tok, enum event_type type)
  1282. {
  1283. char *token;
  1284. char *atom;
  1285. token = *tok;
  1286. switch (type) {
  1287. case EVENT_ITEM:
  1288. if (strcmp(token, "REC") == 0) {
  1289. free_token(token);
  1290. type = process_entry(event, arg, &token);
  1291. } else if (strcmp(token, "__print_flags") == 0) {
  1292. free_token(token);
  1293. type = process_flags(event, arg, &token);
  1294. } else if (strcmp(token, "__print_symbolic") == 0) {
  1295. free_token(token);
  1296. type = process_symbols(event, arg, &token);
  1297. } else if (strcmp(token, "__get_str") == 0) {
  1298. free_token(token);
  1299. type = process_str(event, arg, &token);
  1300. } else {
  1301. atom = token;
  1302. /* test the next token */
  1303. type = read_token_item(&token);
  1304. /* atoms can be more than one token long */
  1305. while (type == EVENT_ITEM) {
  1306. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  1307. strcat(atom, " ");
  1308. strcat(atom, token);
  1309. free_token(token);
  1310. type = read_token_item(&token);
  1311. }
  1312. /* todo, test for function */
  1313. arg->type = PRINT_ATOM;
  1314. arg->atom.atom = atom;
  1315. }
  1316. break;
  1317. case EVENT_DQUOTE:
  1318. case EVENT_SQUOTE:
  1319. arg->type = PRINT_ATOM;
  1320. arg->atom.atom = token;
  1321. type = read_token_item(&token);
  1322. break;
  1323. case EVENT_DELIM:
  1324. if (strcmp(token, "(") == 0) {
  1325. free_token(token);
  1326. type = process_paren(event, arg, &token);
  1327. break;
  1328. }
  1329. case EVENT_OP:
  1330. /* handle single ops */
  1331. arg->type = PRINT_OP;
  1332. arg->op.op = token;
  1333. arg->op.left = NULL;
  1334. type = process_op(event, arg, &token);
  1335. break;
  1336. case EVENT_ERROR ... EVENT_NEWLINE:
  1337. default:
  1338. die("unexpected type %d", type);
  1339. }
  1340. *tok = token;
  1341. return type;
  1342. }
  1343. static int event_read_print_args(struct event *event, struct print_arg **list)
  1344. {
  1345. enum event_type type;
  1346. struct print_arg *arg;
  1347. char *token;
  1348. int args = 0;
  1349. do {
  1350. arg = malloc_or_die(sizeof(*arg));
  1351. memset(arg, 0, sizeof(*arg));
  1352. type = process_arg(event, arg, &token);
  1353. if (type == EVENT_ERROR) {
  1354. free_arg(arg);
  1355. return -1;
  1356. }
  1357. *list = arg;
  1358. args++;
  1359. if (type == EVENT_OP) {
  1360. type = process_op(event, arg, &token);
  1361. list = &arg->next;
  1362. continue;
  1363. }
  1364. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  1365. free_token(token);
  1366. *list = arg;
  1367. list = &arg->next;
  1368. continue;
  1369. }
  1370. break;
  1371. } while (type != EVENT_NONE);
  1372. if (type != EVENT_NONE)
  1373. free_token(token);
  1374. return args;
  1375. }
  1376. static int event_read_print(struct event *event)
  1377. {
  1378. enum event_type type;
  1379. char *token;
  1380. int ret;
  1381. if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
  1382. return -1;
  1383. if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
  1384. return -1;
  1385. if (read_expected(EVENT_OP, (char *)":") < 0)
  1386. return -1;
  1387. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  1388. goto fail;
  1389. event->print_fmt.format = token;
  1390. event->print_fmt.args = NULL;
  1391. /* ok to have no arg */
  1392. type = read_token_item(&token);
  1393. if (type == EVENT_NONE)
  1394. return 0;
  1395. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1396. goto fail;
  1397. free_token(token);
  1398. ret = event_read_print_args(event, &event->print_fmt.args);
  1399. if (ret < 0)
  1400. return -1;
  1401. return 0;
  1402. fail:
  1403. free_token(token);
  1404. return -1;
  1405. }
  1406. static struct format_field *
  1407. find_common_field(struct event *event, const char *name)
  1408. {
  1409. struct format_field *format;
  1410. for (format = event->format.common_fields;
  1411. format; format = format->next) {
  1412. if (strcmp(format->name, name) == 0)
  1413. break;
  1414. }
  1415. return format;
  1416. }
  1417. static struct format_field *
  1418. find_field(struct event *event, const char *name)
  1419. {
  1420. struct format_field *format;
  1421. for (format = event->format.fields;
  1422. format; format = format->next) {
  1423. if (strcmp(format->name, name) == 0)
  1424. break;
  1425. }
  1426. return format;
  1427. }
  1428. static struct format_field *
  1429. find_any_field(struct event *event, const char *name)
  1430. {
  1431. struct format_field *format;
  1432. format = find_common_field(event, name);
  1433. if (format)
  1434. return format;
  1435. return find_field(event, name);
  1436. }
  1437. static unsigned long long read_size(void *ptr, int size)
  1438. {
  1439. switch (size) {
  1440. case 1:
  1441. return *(unsigned char *)ptr;
  1442. case 2:
  1443. return data2host2(ptr);
  1444. case 4:
  1445. return data2host4(ptr);
  1446. case 8:
  1447. return data2host8(ptr);
  1448. default:
  1449. /* BUG! */
  1450. return 0;
  1451. }
  1452. }
  1453. static int get_common_info(const char *type, int *offset, int *size)
  1454. {
  1455. struct event *event;
  1456. struct format_field *field;
  1457. /*
  1458. * All events should have the same common elements.
  1459. * Pick any event to find where the type is;
  1460. */
  1461. if (!event_list)
  1462. die("no event_list!");
  1463. event = event_list;
  1464. field = find_common_field(event, type);
  1465. if (!field)
  1466. die("field '%s' not found", type);
  1467. *offset = field->offset;
  1468. *size = field->size;
  1469. return 0;
  1470. }
  1471. static int parse_common_type(void *data)
  1472. {
  1473. static int type_offset;
  1474. static int type_size;
  1475. int ret;
  1476. if (!type_size) {
  1477. ret = get_common_info("common_type",
  1478. &type_offset,
  1479. &type_size);
  1480. if (ret < 0)
  1481. return ret;
  1482. }
  1483. return read_size(data + type_offset, type_size);
  1484. }
  1485. static int parse_common_pid(void *data)
  1486. {
  1487. static int pid_offset;
  1488. static int pid_size;
  1489. int ret;
  1490. if (!pid_size) {
  1491. ret = get_common_info("common_pid",
  1492. &pid_offset,
  1493. &pid_size);
  1494. if (ret < 0)
  1495. return ret;
  1496. }
  1497. return read_size(data + pid_offset, pid_size);
  1498. }
  1499. static struct event *find_event(int id)
  1500. {
  1501. struct event *event;
  1502. for (event = event_list; event; event = event->next) {
  1503. if (event->id == id)
  1504. break;
  1505. }
  1506. return event;
  1507. }
  1508. static unsigned long long eval_num_arg(void *data, int size,
  1509. struct event *event, struct print_arg *arg)
  1510. {
  1511. unsigned long long val = 0;
  1512. unsigned long long left, right;
  1513. switch (arg->type) {
  1514. case PRINT_NULL:
  1515. /* ?? */
  1516. return 0;
  1517. case PRINT_ATOM:
  1518. return strtoull(arg->atom.atom, NULL, 0);
  1519. case PRINT_FIELD:
  1520. if (!arg->field.field) {
  1521. arg->field.field = find_any_field(event, arg->field.name);
  1522. if (!arg->field.field)
  1523. die("field %s not found", arg->field.name);
  1524. }
  1525. /* must be a number */
  1526. val = read_size(data + arg->field.field->offset,
  1527. arg->field.field->size);
  1528. break;
  1529. case PRINT_FLAGS:
  1530. case PRINT_SYMBOL:
  1531. break;
  1532. case PRINT_TYPE:
  1533. return eval_num_arg(data, size, event, arg->typecast.item);
  1534. case PRINT_STRING:
  1535. return 0;
  1536. break;
  1537. case PRINT_OP:
  1538. left = eval_num_arg(data, size, event, arg->op.left);
  1539. right = eval_num_arg(data, size, event, arg->op.right);
  1540. switch (arg->op.op[0]) {
  1541. case '|':
  1542. if (arg->op.op[1])
  1543. val = left || right;
  1544. else
  1545. val = left | right;
  1546. break;
  1547. case '&':
  1548. if (arg->op.op[1])
  1549. val = left && right;
  1550. else
  1551. val = left & right;
  1552. break;
  1553. case '<':
  1554. switch (arg->op.op[1]) {
  1555. case 0:
  1556. val = left < right;
  1557. break;
  1558. case '<':
  1559. val = left << right;
  1560. break;
  1561. case '=':
  1562. val = left <= right;
  1563. break;
  1564. default:
  1565. die("unknown op '%s'", arg->op.op);
  1566. }
  1567. break;
  1568. case '>':
  1569. switch (arg->op.op[1]) {
  1570. case 0:
  1571. val = left > right;
  1572. break;
  1573. case '>':
  1574. val = left >> right;
  1575. break;
  1576. case '=':
  1577. val = left >= right;
  1578. break;
  1579. default:
  1580. die("unknown op '%s'", arg->op.op);
  1581. }
  1582. break;
  1583. case '=':
  1584. if (arg->op.op[1] != '=')
  1585. die("unknown op '%s'", arg->op.op);
  1586. val = left == right;
  1587. break;
  1588. default:
  1589. die("unknown op '%s'", arg->op.op);
  1590. }
  1591. break;
  1592. default: /* not sure what to do there */
  1593. return 0;
  1594. }
  1595. return val;
  1596. }
  1597. struct flag {
  1598. const char *name;
  1599. unsigned long long value;
  1600. };
  1601. static const struct flag flags[] = {
  1602. { "HI_SOFTIRQ", 0 },
  1603. { "TIMER_SOFTIRQ", 1 },
  1604. { "NET_TX_SOFTIRQ", 2 },
  1605. { "NET_RX_SOFTIRQ", 3 },
  1606. { "BLOCK_SOFTIRQ", 4 },
  1607. { "TASKLET_SOFTIRQ", 5 },
  1608. { "SCHED_SOFTIRQ", 6 },
  1609. { "HRTIMER_SOFTIRQ", 7 },
  1610. { "RCU_SOFTIRQ", 8 },
  1611. { "HRTIMER_NORESTART", 0 },
  1612. { "HRTIMER_RESTART", 1 },
  1613. };
  1614. static unsigned long long eval_flag(const char *flag)
  1615. {
  1616. int i;
  1617. /*
  1618. * Some flags in the format files do not get converted.
  1619. * If the flag is not numeric, see if it is something that
  1620. * we already know about.
  1621. */
  1622. if (isdigit(flag[0]))
  1623. return strtoull(flag, NULL, 0);
  1624. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1625. if (strcmp(flags[i].name, flag) == 0)
  1626. return flags[i].value;
  1627. return 0;
  1628. }
  1629. static void print_str_arg(void *data, int size,
  1630. struct event *event, struct print_arg *arg)
  1631. {
  1632. struct print_flag_sym *flag;
  1633. unsigned long long val, fval;
  1634. char *str;
  1635. int print;
  1636. switch (arg->type) {
  1637. case PRINT_NULL:
  1638. /* ?? */
  1639. return;
  1640. case PRINT_ATOM:
  1641. printf("%s", arg->atom.atom);
  1642. return;
  1643. case PRINT_FIELD:
  1644. if (!arg->field.field) {
  1645. arg->field.field = find_any_field(event, arg->field.name);
  1646. if (!arg->field.field)
  1647. die("field %s not found", arg->field.name);
  1648. }
  1649. str = malloc_or_die(arg->field.field->size + 1);
  1650. memcpy(str, data + arg->field.field->offset,
  1651. arg->field.field->size);
  1652. str[arg->field.field->size] = 0;
  1653. printf("%s", str);
  1654. free(str);
  1655. break;
  1656. case PRINT_FLAGS:
  1657. val = eval_num_arg(data, size, event, arg->flags.field);
  1658. print = 0;
  1659. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1660. fval = eval_flag(flag->value);
  1661. if (!val && !fval) {
  1662. printf("%s", flag->str);
  1663. break;
  1664. }
  1665. if (fval && (val & fval) == fval) {
  1666. if (print && arg->flags.delim)
  1667. printf("%s", arg->flags.delim);
  1668. printf("%s", flag->str);
  1669. print = 1;
  1670. val &= ~fval;
  1671. }
  1672. }
  1673. break;
  1674. case PRINT_SYMBOL:
  1675. val = eval_num_arg(data, size, event, arg->symbol.field);
  1676. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1677. fval = eval_flag(flag->value);
  1678. if (val == fval) {
  1679. printf("%s", flag->str);
  1680. break;
  1681. }
  1682. }
  1683. break;
  1684. case PRINT_TYPE:
  1685. break;
  1686. case PRINT_STRING: {
  1687. int str_offset;
  1688. if (arg->string.offset == -1) {
  1689. struct format_field *f;
  1690. f = find_any_field(event, arg->string.string);
  1691. arg->string.offset = f->offset;
  1692. }
  1693. str_offset = *(int *)(data + arg->string.offset);
  1694. str_offset &= 0xffff;
  1695. printf("%s", ((char *)data) + str_offset);
  1696. break;
  1697. }
  1698. case PRINT_OP:
  1699. /*
  1700. * The only op for string should be ? :
  1701. */
  1702. if (arg->op.op[0] != '?')
  1703. return;
  1704. val = eval_num_arg(data, size, event, arg->op.left);
  1705. if (val)
  1706. print_str_arg(data, size, event, arg->op.right->op.left);
  1707. else
  1708. print_str_arg(data, size, event, arg->op.right->op.right);
  1709. break;
  1710. default:
  1711. /* well... */
  1712. break;
  1713. }
  1714. }
  1715. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1716. {
  1717. static struct format_field *field, *ip_field;
  1718. struct print_arg *args, *arg, **next;
  1719. unsigned long long ip, val;
  1720. char *ptr;
  1721. void *bptr;
  1722. if (!field) {
  1723. field = find_field(event, "buf");
  1724. if (!field)
  1725. die("can't find buffer field for binary printk");
  1726. ip_field = find_field(event, "ip");
  1727. if (!ip_field)
  1728. die("can't find ip field for binary printk");
  1729. }
  1730. ip = read_size(data + ip_field->offset, ip_field->size);
  1731. /*
  1732. * The first arg is the IP pointer.
  1733. */
  1734. args = malloc_or_die(sizeof(*args));
  1735. arg = args;
  1736. arg->next = NULL;
  1737. next = &arg->next;
  1738. arg->type = PRINT_ATOM;
  1739. arg->atom.atom = malloc_or_die(32);
  1740. sprintf(arg->atom.atom, "%lld", ip);
  1741. /* skip the first "%pf : " */
  1742. for (ptr = fmt + 6, bptr = data + field->offset;
  1743. bptr < data + size && *ptr; ptr++) {
  1744. int ls = 0;
  1745. if (*ptr == '%') {
  1746. process_again:
  1747. ptr++;
  1748. switch (*ptr) {
  1749. case '%':
  1750. break;
  1751. case 'l':
  1752. ls++;
  1753. goto process_again;
  1754. case 'L':
  1755. ls = 2;
  1756. goto process_again;
  1757. case '0' ... '9':
  1758. goto process_again;
  1759. case 'p':
  1760. ls = 1;
  1761. /* fall through */
  1762. case 'd':
  1763. case 'u':
  1764. case 'x':
  1765. case 'i':
  1766. bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
  1767. ~(long_size - 1));
  1768. switch (ls) {
  1769. case 0:
  1770. case 1:
  1771. ls = long_size;
  1772. break;
  1773. case 2:
  1774. ls = 8;
  1775. default:
  1776. break;
  1777. }
  1778. val = read_size(bptr, ls);
  1779. bptr += ls;
  1780. arg = malloc_or_die(sizeof(*arg));
  1781. arg->next = NULL;
  1782. arg->type = PRINT_ATOM;
  1783. arg->atom.atom = malloc_or_die(32);
  1784. sprintf(arg->atom.atom, "%lld", val);
  1785. *next = arg;
  1786. next = &arg->next;
  1787. break;
  1788. case 's':
  1789. arg = malloc_or_die(sizeof(*arg));
  1790. arg->next = NULL;
  1791. arg->type = PRINT_STRING;
  1792. arg->string.string = strdup(bptr);
  1793. bptr += strlen(bptr) + 1;
  1794. *next = arg;
  1795. next = &arg->next;
  1796. default:
  1797. break;
  1798. }
  1799. }
  1800. }
  1801. return args;
  1802. }
  1803. static void free_args(struct print_arg *args)
  1804. {
  1805. struct print_arg *next;
  1806. while (args) {
  1807. next = args->next;
  1808. if (args->type == PRINT_ATOM)
  1809. free(args->atom.atom);
  1810. else
  1811. free(args->string.string);
  1812. free(args);
  1813. args = next;
  1814. }
  1815. }
  1816. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1817. {
  1818. unsigned long long addr;
  1819. static struct format_field *field;
  1820. struct printk_map *printk;
  1821. char *format;
  1822. char *p;
  1823. if (!field) {
  1824. field = find_field(event, "fmt");
  1825. if (!field)
  1826. die("can't find format field for binary printk");
  1827. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1828. }
  1829. addr = read_size(data + field->offset, field->size);
  1830. printk = find_printk(addr);
  1831. if (!printk) {
  1832. format = malloc_or_die(45);
  1833. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  1834. addr);
  1835. return format;
  1836. }
  1837. p = printk->printk;
  1838. /* Remove any quotes. */
  1839. if (*p == '"')
  1840. p++;
  1841. format = malloc_or_die(strlen(p) + 10);
  1842. sprintf(format, "%s : %s", "%pf", p);
  1843. /* remove ending quotes and new line since we will add one too */
  1844. p = format + strlen(format) - 1;
  1845. if (*p == '"')
  1846. *p = 0;
  1847. p -= 2;
  1848. if (strcmp(p, "\\n") == 0)
  1849. *p = 0;
  1850. return format;
  1851. }
  1852. static void pretty_print(void *data, int size, struct event *event)
  1853. {
  1854. struct print_fmt *print_fmt = &event->print_fmt;
  1855. struct print_arg *arg = print_fmt->args;
  1856. struct print_arg *args = NULL;
  1857. const char *ptr = print_fmt->format;
  1858. unsigned long long val;
  1859. struct func_map *func;
  1860. const char *saveptr;
  1861. char *bprint_fmt = NULL;
  1862. char format[32];
  1863. int show_func;
  1864. int len;
  1865. int ls;
  1866. if (event->flags & EVENT_FL_ISFUNC)
  1867. ptr = " %pF <-- %pF";
  1868. if (event->flags & EVENT_FL_ISBPRINT) {
  1869. bprint_fmt = get_bprint_format(data, size, event);
  1870. args = make_bprint_args(bprint_fmt, data, size, event);
  1871. arg = args;
  1872. ptr = bprint_fmt;
  1873. }
  1874. for (; *ptr; ptr++) {
  1875. ls = 0;
  1876. if (*ptr == '%') {
  1877. saveptr = ptr;
  1878. show_func = 0;
  1879. cont_process:
  1880. ptr++;
  1881. switch (*ptr) {
  1882. case '%':
  1883. printf("%%");
  1884. break;
  1885. case 'l':
  1886. ls++;
  1887. goto cont_process;
  1888. case 'L':
  1889. ls = 2;
  1890. goto cont_process;
  1891. case 'z':
  1892. case 'Z':
  1893. case '0' ... '9':
  1894. goto cont_process;
  1895. case 'p':
  1896. if (long_size == 4)
  1897. ls = 1;
  1898. else
  1899. ls = 2;
  1900. if (*(ptr+1) == 'F' ||
  1901. *(ptr+1) == 'f') {
  1902. ptr++;
  1903. show_func = *ptr;
  1904. }
  1905. /* fall through */
  1906. case 'd':
  1907. case 'i':
  1908. case 'x':
  1909. case 'X':
  1910. case 'u':
  1911. if (!arg)
  1912. die("no argument match");
  1913. len = ((unsigned long)ptr + 1) -
  1914. (unsigned long)saveptr;
  1915. /* should never happen */
  1916. if (len > 32)
  1917. die("bad format!");
  1918. memcpy(format, saveptr, len);
  1919. format[len] = 0;
  1920. val = eval_num_arg(data, size, event, arg);
  1921. arg = arg->next;
  1922. if (show_func) {
  1923. func = find_func(val);
  1924. if (func) {
  1925. printf("%s", func->func);
  1926. if (show_func == 'F')
  1927. printf("+0x%llx",
  1928. val - func->addr);
  1929. break;
  1930. }
  1931. }
  1932. switch (ls) {
  1933. case 0:
  1934. printf(format, (int)val);
  1935. break;
  1936. case 1:
  1937. printf(format, (long)val);
  1938. break;
  1939. case 2:
  1940. printf(format, (long long)val);
  1941. break;
  1942. default:
  1943. die("bad count (%d)", ls);
  1944. }
  1945. break;
  1946. case 's':
  1947. if (!arg)
  1948. die("no matching argument");
  1949. print_str_arg(data, size, event, arg);
  1950. arg = arg->next;
  1951. break;
  1952. default:
  1953. printf(">%c<", *ptr);
  1954. }
  1955. } else
  1956. printf("%c", *ptr);
  1957. }
  1958. if (args) {
  1959. free_args(args);
  1960. free(bprint_fmt);
  1961. }
  1962. }
  1963. static inline int log10_cpu(int nb)
  1964. {
  1965. if (nb / 100)
  1966. return 3;
  1967. if (nb / 10)
  1968. return 2;
  1969. return 1;
  1970. }
  1971. /* taken from Linux, written by Frederic Weisbecker */
  1972. static void print_graph_cpu(int cpu)
  1973. {
  1974. int i;
  1975. int log10_this = log10_cpu(cpu);
  1976. int log10_all = log10_cpu(cpus);
  1977. /*
  1978. * Start with a space character - to make it stand out
  1979. * to the right a bit when trace output is pasted into
  1980. * email:
  1981. */
  1982. printf(" ");
  1983. /*
  1984. * Tricky - we space the CPU field according to the max
  1985. * number of online CPUs. On a 2-cpu system it would take
  1986. * a maximum of 1 digit - on a 128 cpu system it would
  1987. * take up to 3 digits:
  1988. */
  1989. for (i = 0; i < log10_all - log10_this; i++)
  1990. printf(" ");
  1991. printf("%d) ", cpu);
  1992. }
  1993. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  1994. #define TRACE_GRAPH_INDENT 2
  1995. static void print_graph_proc(int pid, const char *comm)
  1996. {
  1997. /* sign + log10(MAX_INT) + '\0' */
  1998. char pid_str[11];
  1999. int spaces = 0;
  2000. int len;
  2001. int i;
  2002. sprintf(pid_str, "%d", pid);
  2003. /* 1 stands for the "-" character */
  2004. len = strlen(comm) + strlen(pid_str) + 1;
  2005. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  2006. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  2007. /* First spaces to align center */
  2008. for (i = 0; i < spaces / 2; i++)
  2009. printf(" ");
  2010. printf("%s-%s", comm, pid_str);
  2011. /* Last spaces to align center */
  2012. for (i = 0; i < spaces - (spaces / 2); i++)
  2013. printf(" ");
  2014. }
  2015. static struct record *
  2016. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2017. struct record *next)
  2018. {
  2019. struct format_field *field;
  2020. struct event *event;
  2021. unsigned long val;
  2022. int type;
  2023. int pid;
  2024. type = parse_common_type(next->data);
  2025. event = find_event(type);
  2026. if (!event)
  2027. return NULL;
  2028. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2029. return NULL;
  2030. pid = parse_common_pid(next->data);
  2031. field = find_field(event, "func");
  2032. if (!field)
  2033. die("function return does not have field func");
  2034. val = read_size(next->data + field->offset, field->size);
  2035. if (cur_pid != pid || cur_func != val)
  2036. return NULL;
  2037. /* this is a leaf, now advance the iterator */
  2038. return trace_read_data(cpu);
  2039. }
  2040. /* Signal a overhead of time execution to the output */
  2041. static void print_graph_overhead(unsigned long long duration)
  2042. {
  2043. /* Non nested entry or return */
  2044. if (duration == ~0ULL)
  2045. return (void)printf(" ");
  2046. /* Duration exceeded 100 msecs */
  2047. if (duration > 100000ULL)
  2048. return (void)printf("! ");
  2049. /* Duration exceeded 10 msecs */
  2050. if (duration > 10000ULL)
  2051. return (void)printf("+ ");
  2052. printf(" ");
  2053. }
  2054. static void print_graph_duration(unsigned long long duration)
  2055. {
  2056. unsigned long usecs = duration / 1000;
  2057. unsigned long nsecs_rem = duration % 1000;
  2058. /* log10(ULONG_MAX) + '\0' */
  2059. char msecs_str[21];
  2060. char nsecs_str[5];
  2061. int len;
  2062. int i;
  2063. sprintf(msecs_str, "%lu", usecs);
  2064. /* Print msecs */
  2065. len = printf("%lu", usecs);
  2066. /* Print nsecs (we don't want to exceed 7 numbers) */
  2067. if (len < 7) {
  2068. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2069. len += printf(".%s", nsecs_str);
  2070. }
  2071. printf(" us ");
  2072. /* Print remaining spaces to fit the row's width */
  2073. for (i = len; i < 7; i++)
  2074. printf(" ");
  2075. printf("| ");
  2076. }
  2077. static void
  2078. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2079. {
  2080. unsigned long long rettime, calltime;
  2081. unsigned long long duration, depth;
  2082. unsigned long long val;
  2083. struct format_field *field;
  2084. struct func_map *func;
  2085. struct event *ret_event;
  2086. int type;
  2087. int i;
  2088. type = parse_common_type(ret_rec->data);
  2089. ret_event = find_event(type);
  2090. field = find_field(ret_event, "rettime");
  2091. if (!field)
  2092. die("can't find rettime in return graph");
  2093. rettime = read_size(ret_rec->data + field->offset, field->size);
  2094. field = find_field(ret_event, "calltime");
  2095. if (!field)
  2096. die("can't find rettime in return graph");
  2097. calltime = read_size(ret_rec->data + field->offset, field->size);
  2098. duration = rettime - calltime;
  2099. /* Overhead */
  2100. print_graph_overhead(duration);
  2101. /* Duration */
  2102. print_graph_duration(duration);
  2103. field = find_field(event, "depth");
  2104. if (!field)
  2105. die("can't find depth in entry graph");
  2106. depth = read_size(data + field->offset, field->size);
  2107. /* Function */
  2108. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2109. printf(" ");
  2110. field = find_field(event, "func");
  2111. if (!field)
  2112. die("can't find func in entry graph");
  2113. val = read_size(data + field->offset, field->size);
  2114. func = find_func(val);
  2115. if (func)
  2116. printf("%s();", func->func);
  2117. else
  2118. printf("%llx();", val);
  2119. }
  2120. static void print_graph_nested(struct event *event, void *data)
  2121. {
  2122. struct format_field *field;
  2123. unsigned long long depth;
  2124. unsigned long long val;
  2125. struct func_map *func;
  2126. int i;
  2127. /* No overhead */
  2128. print_graph_overhead(-1);
  2129. /* No time */
  2130. printf(" | ");
  2131. field = find_field(event, "depth");
  2132. if (!field)
  2133. die("can't find depth in entry graph");
  2134. depth = read_size(data + field->offset, field->size);
  2135. /* Function */
  2136. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2137. printf(" ");
  2138. field = find_field(event, "func");
  2139. if (!field)
  2140. die("can't find func in entry graph");
  2141. val = read_size(data + field->offset, field->size);
  2142. func = find_func(val);
  2143. if (func)
  2144. printf("%s() {", func->func);
  2145. else
  2146. printf("%llx() {", val);
  2147. }
  2148. static void
  2149. pretty_print_func_ent(void *data, int size, struct event *event,
  2150. int cpu, int pid, const char *comm,
  2151. unsigned long secs, unsigned long usecs)
  2152. {
  2153. struct format_field *field;
  2154. struct record *rec;
  2155. void *copy_data;
  2156. unsigned long val;
  2157. printf("%5lu.%06lu | ", secs, usecs);
  2158. print_graph_cpu(cpu);
  2159. print_graph_proc(pid, comm);
  2160. printf(" | ");
  2161. field = find_field(event, "func");
  2162. if (!field)
  2163. die("function entry does not have func field");
  2164. val = read_size(data + field->offset, field->size);
  2165. /*
  2166. * peek_data may unmap the data pointer. Copy it first.
  2167. */
  2168. copy_data = malloc_or_die(size);
  2169. memcpy(copy_data, data, size);
  2170. data = copy_data;
  2171. rec = trace_peek_data(cpu);
  2172. if (rec) {
  2173. rec = get_return_for_leaf(cpu, pid, val, rec);
  2174. if (rec) {
  2175. print_graph_entry_leaf(event, data, rec);
  2176. goto out_free;
  2177. }
  2178. }
  2179. print_graph_nested(event, data);
  2180. out_free:
  2181. free(data);
  2182. }
  2183. static void
  2184. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2185. int cpu, int pid, const char *comm,
  2186. unsigned long secs, unsigned long usecs)
  2187. {
  2188. unsigned long long rettime, calltime;
  2189. unsigned long long duration, depth;
  2190. struct format_field *field;
  2191. int i;
  2192. printf("%5lu.%06lu | ", secs, usecs);
  2193. print_graph_cpu(cpu);
  2194. print_graph_proc(pid, comm);
  2195. printf(" | ");
  2196. field = find_field(event, "rettime");
  2197. if (!field)
  2198. die("can't find rettime in return graph");
  2199. rettime = read_size(data + field->offset, field->size);
  2200. field = find_field(event, "calltime");
  2201. if (!field)
  2202. die("can't find calltime in return graph");
  2203. calltime = read_size(data + field->offset, field->size);
  2204. duration = rettime - calltime;
  2205. /* Overhead */
  2206. print_graph_overhead(duration);
  2207. /* Duration */
  2208. print_graph_duration(duration);
  2209. field = find_field(event, "depth");
  2210. if (!field)
  2211. die("can't find depth in entry graph");
  2212. depth = read_size(data + field->offset, field->size);
  2213. /* Function */
  2214. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2215. printf(" ");
  2216. printf("}");
  2217. }
  2218. static void
  2219. pretty_print_func_graph(void *data, int size, struct event *event,
  2220. int cpu, int pid, const char *comm,
  2221. unsigned long secs, unsigned long usecs)
  2222. {
  2223. if (event->flags & EVENT_FL_ISFUNCENT)
  2224. pretty_print_func_ent(data, size, event,
  2225. cpu, pid, comm, secs, usecs);
  2226. else if (event->flags & EVENT_FL_ISFUNCRET)
  2227. pretty_print_func_ret(data, size, event,
  2228. cpu, pid, comm, secs, usecs);
  2229. printf("\n");
  2230. }
  2231. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2232. char *comm)
  2233. {
  2234. struct event *event;
  2235. unsigned long secs;
  2236. unsigned long usecs;
  2237. int type;
  2238. int pid;
  2239. secs = nsecs / NSECS_PER_SEC;
  2240. nsecs -= secs * NSECS_PER_SEC;
  2241. usecs = nsecs / NSECS_PER_USEC;
  2242. type = parse_common_type(data);
  2243. event = find_event(type);
  2244. if (!event)
  2245. die("ug! no event found for type %d", type);
  2246. pid = parse_common_pid(data);
  2247. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2248. return pretty_print_func_graph(data, size, event, cpu,
  2249. pid, comm, secs, usecs);
  2250. printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
  2251. comm, pid, cpu,
  2252. secs, nsecs, event->name);
  2253. pretty_print(data, size, event);
  2254. printf("\n");
  2255. }
  2256. static void print_fields(struct print_flag_sym *field)
  2257. {
  2258. printf("{ %s, %s }", field->value, field->str);
  2259. if (field->next) {
  2260. printf(", ");
  2261. print_fields(field->next);
  2262. }
  2263. }
  2264. static void print_args(struct print_arg *args)
  2265. {
  2266. int print_paren = 1;
  2267. switch (args->type) {
  2268. case PRINT_NULL:
  2269. printf("null");
  2270. break;
  2271. case PRINT_ATOM:
  2272. printf("%s", args->atom.atom);
  2273. break;
  2274. case PRINT_FIELD:
  2275. printf("REC->%s", args->field.name);
  2276. break;
  2277. case PRINT_FLAGS:
  2278. printf("__print_flags(");
  2279. print_args(args->flags.field);
  2280. printf(", %s, ", args->flags.delim);
  2281. print_fields(args->flags.flags);
  2282. printf(")");
  2283. break;
  2284. case PRINT_SYMBOL:
  2285. printf("__print_symbolic(");
  2286. print_args(args->symbol.field);
  2287. printf(", ");
  2288. print_fields(args->symbol.symbols);
  2289. printf(")");
  2290. break;
  2291. case PRINT_STRING:
  2292. printf("__get_str(%s)", args->string.string);
  2293. break;
  2294. case PRINT_TYPE:
  2295. printf("(%s)", args->typecast.type);
  2296. print_args(args->typecast.item);
  2297. break;
  2298. case PRINT_OP:
  2299. if (strcmp(args->op.op, ":") == 0)
  2300. print_paren = 0;
  2301. if (print_paren)
  2302. printf("(");
  2303. print_args(args->op.left);
  2304. printf(" %s ", args->op.op);
  2305. print_args(args->op.right);
  2306. if (print_paren)
  2307. printf(")");
  2308. break;
  2309. default:
  2310. /* we should warn... */
  2311. return;
  2312. }
  2313. if (args->next) {
  2314. printf("\n");
  2315. print_args(args->next);
  2316. }
  2317. }
  2318. static void parse_header_field(char *type,
  2319. int *offset, int *size)
  2320. {
  2321. char *token;
  2322. if (read_expected(EVENT_ITEM, (char *)"field") < 0)
  2323. return;
  2324. if (read_expected(EVENT_OP, (char *)":") < 0)
  2325. return;
  2326. /* type */
  2327. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2328. return;
  2329. free_token(token);
  2330. if (read_expected(EVENT_ITEM, type) < 0)
  2331. return;
  2332. if (read_expected(EVENT_OP, (char *)";") < 0)
  2333. return;
  2334. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  2335. return;
  2336. if (read_expected(EVENT_OP, (char *)":") < 0)
  2337. return;
  2338. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2339. return;
  2340. *offset = atoi(token);
  2341. free_token(token);
  2342. if (read_expected(EVENT_OP, (char *)";") < 0)
  2343. return;
  2344. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  2345. return;
  2346. if (read_expected(EVENT_OP, (char *)":") < 0)
  2347. return;
  2348. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2349. return;
  2350. *size = atoi(token);
  2351. free_token(token);
  2352. if (read_expected(EVENT_OP, (char *)";") < 0)
  2353. return;
  2354. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  2355. return;
  2356. free_token(token);
  2357. }
  2358. int parse_header_page(char *buf, unsigned long size)
  2359. {
  2360. init_input_buf(buf, size);
  2361. parse_header_field((char *)"timestamp", &header_page_ts_offset,
  2362. &header_page_ts_size);
  2363. parse_header_field((char *)"commit", &header_page_size_offset,
  2364. &header_page_size_size);
  2365. parse_header_field((char *)"data", &header_page_data_offset,
  2366. &header_page_data_size);
  2367. return 0;
  2368. }
  2369. int parse_ftrace_file(char *buf, unsigned long size)
  2370. {
  2371. struct format_field *field;
  2372. struct print_arg *arg, **list;
  2373. struct event *event;
  2374. int ret;
  2375. init_input_buf(buf, size);
  2376. event = alloc_event();
  2377. if (!event)
  2378. return -ENOMEM;
  2379. event->flags |= EVENT_FL_ISFTRACE;
  2380. event->name = event_read_name();
  2381. if (!event->name)
  2382. die("failed to read ftrace event name");
  2383. if (strcmp(event->name, "function") == 0)
  2384. event->flags |= EVENT_FL_ISFUNC;
  2385. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2386. event->flags |= EVENT_FL_ISFUNCENT;
  2387. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2388. event->flags |= EVENT_FL_ISFUNCRET;
  2389. else if (strcmp(event->name, "bprint") == 0)
  2390. event->flags |= EVENT_FL_ISBPRINT;
  2391. event->id = event_read_id();
  2392. if (event->id < 0)
  2393. die("failed to read ftrace event id");
  2394. add_event(event);
  2395. ret = event_read_format(event);
  2396. if (ret < 0)
  2397. die("failed to read ftrace event format");
  2398. ret = event_read_print(event);
  2399. if (ret < 0)
  2400. die("failed to read ftrace event print fmt");
  2401. /*
  2402. * The arguments for ftrace files are parsed by the fields.
  2403. * Set up the fields as their arguments.
  2404. */
  2405. list = &event->print_fmt.args;
  2406. for (field = event->format.fields; field; field = field->next) {
  2407. arg = malloc_or_die(sizeof(*arg));
  2408. memset(arg, 0, sizeof(*arg));
  2409. *list = arg;
  2410. list = &arg->next;
  2411. arg->type = PRINT_FIELD;
  2412. arg->field.name = field->name;
  2413. arg->field.field = field;
  2414. }
  2415. return 0;
  2416. }
  2417. int parse_event_file(char *buf, unsigned long size, char *system__unused __unused)
  2418. {
  2419. struct event *event;
  2420. int ret;
  2421. init_input_buf(buf, size);
  2422. event = alloc_event();
  2423. if (!event)
  2424. return -ENOMEM;
  2425. event->name = event_read_name();
  2426. if (!event->name)
  2427. die("failed to read event name");
  2428. event->id = event_read_id();
  2429. if (event->id < 0)
  2430. die("failed to read event id");
  2431. ret = event_read_format(event);
  2432. if (ret < 0)
  2433. die("failed to read event format");
  2434. ret = event_read_print(event);
  2435. if (ret < 0)
  2436. die("failed to read event print fmt");
  2437. #define PRINT_ARGS 0
  2438. if (PRINT_ARGS && event->print_fmt.args)
  2439. print_args(event->print_fmt.args);
  2440. add_event(event);
  2441. return 0;
  2442. }
  2443. void parse_set_info(int nr_cpus, int long_sz)
  2444. {
  2445. cpus = nr_cpus;
  2446. long_size = long_sz;
  2447. }