trace-event-parse.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  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. unsigned long long
  1454. raw_field_value(struct event *event, const char *name, void *data)
  1455. {
  1456. struct format_field *field;
  1457. field = find_any_field(event, name);
  1458. if (!field)
  1459. return 0ULL;
  1460. return read_size(data + field->offset, field->size);
  1461. }
  1462. void *raw_field_ptr(struct event *event, const char *name, void *data)
  1463. {
  1464. struct format_field *field;
  1465. field = find_any_field(event, name);
  1466. if (!field)
  1467. return NULL;
  1468. return data + field->offset;
  1469. }
  1470. static int get_common_info(const char *type, int *offset, int *size)
  1471. {
  1472. struct event *event;
  1473. struct format_field *field;
  1474. /*
  1475. * All events should have the same common elements.
  1476. * Pick any event to find where the type is;
  1477. */
  1478. if (!event_list)
  1479. die("no event_list!");
  1480. event = event_list;
  1481. field = find_common_field(event, type);
  1482. if (!field)
  1483. die("field '%s' not found", type);
  1484. *offset = field->offset;
  1485. *size = field->size;
  1486. return 0;
  1487. }
  1488. int trace_parse_common_type(void *data)
  1489. {
  1490. static int type_offset;
  1491. static int type_size;
  1492. int ret;
  1493. if (!type_size) {
  1494. ret = get_common_info("common_type",
  1495. &type_offset,
  1496. &type_size);
  1497. if (ret < 0)
  1498. return ret;
  1499. }
  1500. return read_size(data + type_offset, type_size);
  1501. }
  1502. static int parse_common_pid(void *data)
  1503. {
  1504. static int pid_offset;
  1505. static int pid_size;
  1506. int ret;
  1507. if (!pid_size) {
  1508. ret = get_common_info("common_pid",
  1509. &pid_offset,
  1510. &pid_size);
  1511. if (ret < 0)
  1512. return ret;
  1513. }
  1514. return read_size(data + pid_offset, pid_size);
  1515. }
  1516. struct event *trace_find_event(int id)
  1517. {
  1518. struct event *event;
  1519. for (event = event_list; event; event = event->next) {
  1520. if (event->id == id)
  1521. break;
  1522. }
  1523. return event;
  1524. }
  1525. static unsigned long long eval_num_arg(void *data, int size,
  1526. struct event *event, struct print_arg *arg)
  1527. {
  1528. unsigned long long val = 0;
  1529. unsigned long long left, right;
  1530. switch (arg->type) {
  1531. case PRINT_NULL:
  1532. /* ?? */
  1533. return 0;
  1534. case PRINT_ATOM:
  1535. return strtoull(arg->atom.atom, NULL, 0);
  1536. case PRINT_FIELD:
  1537. if (!arg->field.field) {
  1538. arg->field.field = find_any_field(event, arg->field.name);
  1539. if (!arg->field.field)
  1540. die("field %s not found", arg->field.name);
  1541. }
  1542. /* must be a number */
  1543. val = read_size(data + arg->field.field->offset,
  1544. arg->field.field->size);
  1545. break;
  1546. case PRINT_FLAGS:
  1547. case PRINT_SYMBOL:
  1548. break;
  1549. case PRINT_TYPE:
  1550. return eval_num_arg(data, size, event, arg->typecast.item);
  1551. case PRINT_STRING:
  1552. return 0;
  1553. break;
  1554. case PRINT_OP:
  1555. left = eval_num_arg(data, size, event, arg->op.left);
  1556. right = eval_num_arg(data, size, event, arg->op.right);
  1557. switch (arg->op.op[0]) {
  1558. case '|':
  1559. if (arg->op.op[1])
  1560. val = left || right;
  1561. else
  1562. val = left | right;
  1563. break;
  1564. case '&':
  1565. if (arg->op.op[1])
  1566. val = left && right;
  1567. else
  1568. val = left & right;
  1569. break;
  1570. case '<':
  1571. switch (arg->op.op[1]) {
  1572. case 0:
  1573. val = left < right;
  1574. break;
  1575. case '<':
  1576. val = left << right;
  1577. break;
  1578. case '=':
  1579. val = left <= right;
  1580. break;
  1581. default:
  1582. die("unknown op '%s'", arg->op.op);
  1583. }
  1584. break;
  1585. case '>':
  1586. switch (arg->op.op[1]) {
  1587. case 0:
  1588. val = left > right;
  1589. break;
  1590. case '>':
  1591. val = left >> right;
  1592. break;
  1593. case '=':
  1594. val = left >= right;
  1595. break;
  1596. default:
  1597. die("unknown op '%s'", arg->op.op);
  1598. }
  1599. break;
  1600. case '=':
  1601. if (arg->op.op[1] != '=')
  1602. die("unknown op '%s'", arg->op.op);
  1603. val = left == right;
  1604. break;
  1605. default:
  1606. die("unknown op '%s'", arg->op.op);
  1607. }
  1608. break;
  1609. default: /* not sure what to do there */
  1610. return 0;
  1611. }
  1612. return val;
  1613. }
  1614. struct flag {
  1615. const char *name;
  1616. unsigned long long value;
  1617. };
  1618. static const struct flag flags[] = {
  1619. { "HI_SOFTIRQ", 0 },
  1620. { "TIMER_SOFTIRQ", 1 },
  1621. { "NET_TX_SOFTIRQ", 2 },
  1622. { "NET_RX_SOFTIRQ", 3 },
  1623. { "BLOCK_SOFTIRQ", 4 },
  1624. { "TASKLET_SOFTIRQ", 5 },
  1625. { "SCHED_SOFTIRQ", 6 },
  1626. { "HRTIMER_SOFTIRQ", 7 },
  1627. { "RCU_SOFTIRQ", 8 },
  1628. { "HRTIMER_NORESTART", 0 },
  1629. { "HRTIMER_RESTART", 1 },
  1630. };
  1631. static unsigned long long eval_flag(const char *flag)
  1632. {
  1633. int i;
  1634. /*
  1635. * Some flags in the format files do not get converted.
  1636. * If the flag is not numeric, see if it is something that
  1637. * we already know about.
  1638. */
  1639. if (isdigit(flag[0]))
  1640. return strtoull(flag, NULL, 0);
  1641. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1642. if (strcmp(flags[i].name, flag) == 0)
  1643. return flags[i].value;
  1644. return 0;
  1645. }
  1646. static void print_str_arg(void *data, int size,
  1647. struct event *event, struct print_arg *arg)
  1648. {
  1649. struct print_flag_sym *flag;
  1650. unsigned long long val, fval;
  1651. char *str;
  1652. int print;
  1653. switch (arg->type) {
  1654. case PRINT_NULL:
  1655. /* ?? */
  1656. return;
  1657. case PRINT_ATOM:
  1658. printf("%s", arg->atom.atom);
  1659. return;
  1660. case PRINT_FIELD:
  1661. if (!arg->field.field) {
  1662. arg->field.field = find_any_field(event, arg->field.name);
  1663. if (!arg->field.field)
  1664. die("field %s not found", arg->field.name);
  1665. }
  1666. str = malloc_or_die(arg->field.field->size + 1);
  1667. memcpy(str, data + arg->field.field->offset,
  1668. arg->field.field->size);
  1669. str[arg->field.field->size] = 0;
  1670. printf("%s", str);
  1671. free(str);
  1672. break;
  1673. case PRINT_FLAGS:
  1674. val = eval_num_arg(data, size, event, arg->flags.field);
  1675. print = 0;
  1676. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1677. fval = eval_flag(flag->value);
  1678. if (!val && !fval) {
  1679. printf("%s", flag->str);
  1680. break;
  1681. }
  1682. if (fval && (val & fval) == fval) {
  1683. if (print && arg->flags.delim)
  1684. printf("%s", arg->flags.delim);
  1685. printf("%s", flag->str);
  1686. print = 1;
  1687. val &= ~fval;
  1688. }
  1689. }
  1690. break;
  1691. case PRINT_SYMBOL:
  1692. val = eval_num_arg(data, size, event, arg->symbol.field);
  1693. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1694. fval = eval_flag(flag->value);
  1695. if (val == fval) {
  1696. printf("%s", flag->str);
  1697. break;
  1698. }
  1699. }
  1700. break;
  1701. case PRINT_TYPE:
  1702. break;
  1703. case PRINT_STRING: {
  1704. int str_offset;
  1705. if (arg->string.offset == -1) {
  1706. struct format_field *f;
  1707. f = find_any_field(event, arg->string.string);
  1708. arg->string.offset = f->offset;
  1709. }
  1710. str_offset = *(int *)(data + arg->string.offset);
  1711. str_offset &= 0xffff;
  1712. printf("%s", ((char *)data) + str_offset);
  1713. break;
  1714. }
  1715. case PRINT_OP:
  1716. /*
  1717. * The only op for string should be ? :
  1718. */
  1719. if (arg->op.op[0] != '?')
  1720. return;
  1721. val = eval_num_arg(data, size, event, arg->op.left);
  1722. if (val)
  1723. print_str_arg(data, size, event, arg->op.right->op.left);
  1724. else
  1725. print_str_arg(data, size, event, arg->op.right->op.right);
  1726. break;
  1727. default:
  1728. /* well... */
  1729. break;
  1730. }
  1731. }
  1732. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1733. {
  1734. static struct format_field *field, *ip_field;
  1735. struct print_arg *args, *arg, **next;
  1736. unsigned long long ip, val;
  1737. char *ptr;
  1738. void *bptr;
  1739. if (!field) {
  1740. field = find_field(event, "buf");
  1741. if (!field)
  1742. die("can't find buffer field for binary printk");
  1743. ip_field = find_field(event, "ip");
  1744. if (!ip_field)
  1745. die("can't find ip field for binary printk");
  1746. }
  1747. ip = read_size(data + ip_field->offset, ip_field->size);
  1748. /*
  1749. * The first arg is the IP pointer.
  1750. */
  1751. args = malloc_or_die(sizeof(*args));
  1752. arg = args;
  1753. arg->next = NULL;
  1754. next = &arg->next;
  1755. arg->type = PRINT_ATOM;
  1756. arg->atom.atom = malloc_or_die(32);
  1757. sprintf(arg->atom.atom, "%lld", ip);
  1758. /* skip the first "%pf : " */
  1759. for (ptr = fmt + 6, bptr = data + field->offset;
  1760. bptr < data + size && *ptr; ptr++) {
  1761. int ls = 0;
  1762. if (*ptr == '%') {
  1763. process_again:
  1764. ptr++;
  1765. switch (*ptr) {
  1766. case '%':
  1767. break;
  1768. case 'l':
  1769. ls++;
  1770. goto process_again;
  1771. case 'L':
  1772. ls = 2;
  1773. goto process_again;
  1774. case '0' ... '9':
  1775. goto process_again;
  1776. case 'p':
  1777. ls = 1;
  1778. /* fall through */
  1779. case 'd':
  1780. case 'u':
  1781. case 'x':
  1782. case 'i':
  1783. bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
  1784. ~(long_size - 1));
  1785. switch (ls) {
  1786. case 0:
  1787. case 1:
  1788. ls = long_size;
  1789. break;
  1790. case 2:
  1791. ls = 8;
  1792. default:
  1793. break;
  1794. }
  1795. val = read_size(bptr, ls);
  1796. bptr += ls;
  1797. arg = malloc_or_die(sizeof(*arg));
  1798. arg->next = NULL;
  1799. arg->type = PRINT_ATOM;
  1800. arg->atom.atom = malloc_or_die(32);
  1801. sprintf(arg->atom.atom, "%lld", val);
  1802. *next = arg;
  1803. next = &arg->next;
  1804. break;
  1805. case 's':
  1806. arg = malloc_or_die(sizeof(*arg));
  1807. arg->next = NULL;
  1808. arg->type = PRINT_STRING;
  1809. arg->string.string = strdup(bptr);
  1810. bptr += strlen(bptr) + 1;
  1811. *next = arg;
  1812. next = &arg->next;
  1813. default:
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. return args;
  1819. }
  1820. static void free_args(struct print_arg *args)
  1821. {
  1822. struct print_arg *next;
  1823. while (args) {
  1824. next = args->next;
  1825. if (args->type == PRINT_ATOM)
  1826. free(args->atom.atom);
  1827. else
  1828. free(args->string.string);
  1829. free(args);
  1830. args = next;
  1831. }
  1832. }
  1833. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1834. {
  1835. unsigned long long addr;
  1836. static struct format_field *field;
  1837. struct printk_map *printk;
  1838. char *format;
  1839. char *p;
  1840. if (!field) {
  1841. field = find_field(event, "fmt");
  1842. if (!field)
  1843. die("can't find format field for binary printk");
  1844. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1845. }
  1846. addr = read_size(data + field->offset, field->size);
  1847. printk = find_printk(addr);
  1848. if (!printk) {
  1849. format = malloc_or_die(45);
  1850. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  1851. addr);
  1852. return format;
  1853. }
  1854. p = printk->printk;
  1855. /* Remove any quotes. */
  1856. if (*p == '"')
  1857. p++;
  1858. format = malloc_or_die(strlen(p) + 10);
  1859. sprintf(format, "%s : %s", "%pf", p);
  1860. /* remove ending quotes and new line since we will add one too */
  1861. p = format + strlen(format) - 1;
  1862. if (*p == '"')
  1863. *p = 0;
  1864. p -= 2;
  1865. if (strcmp(p, "\\n") == 0)
  1866. *p = 0;
  1867. return format;
  1868. }
  1869. static void pretty_print(void *data, int size, struct event *event)
  1870. {
  1871. struct print_fmt *print_fmt = &event->print_fmt;
  1872. struct print_arg *arg = print_fmt->args;
  1873. struct print_arg *args = NULL;
  1874. const char *ptr = print_fmt->format;
  1875. unsigned long long val;
  1876. struct func_map *func;
  1877. const char *saveptr;
  1878. char *bprint_fmt = NULL;
  1879. char format[32];
  1880. int show_func;
  1881. int len;
  1882. int ls;
  1883. if (event->flags & EVENT_FL_ISFUNC)
  1884. ptr = " %pF <-- %pF";
  1885. if (event->flags & EVENT_FL_ISBPRINT) {
  1886. bprint_fmt = get_bprint_format(data, size, event);
  1887. args = make_bprint_args(bprint_fmt, data, size, event);
  1888. arg = args;
  1889. ptr = bprint_fmt;
  1890. }
  1891. for (; *ptr; ptr++) {
  1892. ls = 0;
  1893. if (*ptr == '%') {
  1894. saveptr = ptr;
  1895. show_func = 0;
  1896. cont_process:
  1897. ptr++;
  1898. switch (*ptr) {
  1899. case '%':
  1900. printf("%%");
  1901. break;
  1902. case 'l':
  1903. ls++;
  1904. goto cont_process;
  1905. case 'L':
  1906. ls = 2;
  1907. goto cont_process;
  1908. case 'z':
  1909. case 'Z':
  1910. case '0' ... '9':
  1911. goto cont_process;
  1912. case 'p':
  1913. if (long_size == 4)
  1914. ls = 1;
  1915. else
  1916. ls = 2;
  1917. if (*(ptr+1) == 'F' ||
  1918. *(ptr+1) == 'f') {
  1919. ptr++;
  1920. show_func = *ptr;
  1921. }
  1922. /* fall through */
  1923. case 'd':
  1924. case 'i':
  1925. case 'x':
  1926. case 'X':
  1927. case 'u':
  1928. if (!arg)
  1929. die("no argument match");
  1930. len = ((unsigned long)ptr + 1) -
  1931. (unsigned long)saveptr;
  1932. /* should never happen */
  1933. if (len > 32)
  1934. die("bad format!");
  1935. memcpy(format, saveptr, len);
  1936. format[len] = 0;
  1937. val = eval_num_arg(data, size, event, arg);
  1938. arg = arg->next;
  1939. if (show_func) {
  1940. func = find_func(val);
  1941. if (func) {
  1942. printf("%s", func->func);
  1943. if (show_func == 'F')
  1944. printf("+0x%llx",
  1945. val - func->addr);
  1946. break;
  1947. }
  1948. }
  1949. switch (ls) {
  1950. case 0:
  1951. printf(format, (int)val);
  1952. break;
  1953. case 1:
  1954. printf(format, (long)val);
  1955. break;
  1956. case 2:
  1957. printf(format, (long long)val);
  1958. break;
  1959. default:
  1960. die("bad count (%d)", ls);
  1961. }
  1962. break;
  1963. case 's':
  1964. if (!arg)
  1965. die("no matching argument");
  1966. print_str_arg(data, size, event, arg);
  1967. arg = arg->next;
  1968. break;
  1969. default:
  1970. printf(">%c<", *ptr);
  1971. }
  1972. } else
  1973. printf("%c", *ptr);
  1974. }
  1975. if (args) {
  1976. free_args(args);
  1977. free(bprint_fmt);
  1978. }
  1979. }
  1980. static inline int log10_cpu(int nb)
  1981. {
  1982. if (nb / 100)
  1983. return 3;
  1984. if (nb / 10)
  1985. return 2;
  1986. return 1;
  1987. }
  1988. /* taken from Linux, written by Frederic Weisbecker */
  1989. static void print_graph_cpu(int cpu)
  1990. {
  1991. int i;
  1992. int log10_this = log10_cpu(cpu);
  1993. int log10_all = log10_cpu(cpus);
  1994. /*
  1995. * Start with a space character - to make it stand out
  1996. * to the right a bit when trace output is pasted into
  1997. * email:
  1998. */
  1999. printf(" ");
  2000. /*
  2001. * Tricky - we space the CPU field according to the max
  2002. * number of online CPUs. On a 2-cpu system it would take
  2003. * a maximum of 1 digit - on a 128 cpu system it would
  2004. * take up to 3 digits:
  2005. */
  2006. for (i = 0; i < log10_all - log10_this; i++)
  2007. printf(" ");
  2008. printf("%d) ", cpu);
  2009. }
  2010. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  2011. #define TRACE_GRAPH_INDENT 2
  2012. static void print_graph_proc(int pid, const char *comm)
  2013. {
  2014. /* sign + log10(MAX_INT) + '\0' */
  2015. char pid_str[11];
  2016. int spaces = 0;
  2017. int len;
  2018. int i;
  2019. sprintf(pid_str, "%d", pid);
  2020. /* 1 stands for the "-" character */
  2021. len = strlen(comm) + strlen(pid_str) + 1;
  2022. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  2023. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  2024. /* First spaces to align center */
  2025. for (i = 0; i < spaces / 2; i++)
  2026. printf(" ");
  2027. printf("%s-%s", comm, pid_str);
  2028. /* Last spaces to align center */
  2029. for (i = 0; i < spaces - (spaces / 2); i++)
  2030. printf(" ");
  2031. }
  2032. static struct record *
  2033. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2034. struct record *next)
  2035. {
  2036. struct format_field *field;
  2037. struct event *event;
  2038. unsigned long val;
  2039. int type;
  2040. int pid;
  2041. type = trace_parse_common_type(next->data);
  2042. event = trace_find_event(type);
  2043. if (!event)
  2044. return NULL;
  2045. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2046. return NULL;
  2047. pid = parse_common_pid(next->data);
  2048. field = find_field(event, "func");
  2049. if (!field)
  2050. die("function return does not have field func");
  2051. val = read_size(next->data + field->offset, field->size);
  2052. if (cur_pid != pid || cur_func != val)
  2053. return NULL;
  2054. /* this is a leaf, now advance the iterator */
  2055. return trace_read_data(cpu);
  2056. }
  2057. /* Signal a overhead of time execution to the output */
  2058. static void print_graph_overhead(unsigned long long duration)
  2059. {
  2060. /* Non nested entry or return */
  2061. if (duration == ~0ULL)
  2062. return (void)printf(" ");
  2063. /* Duration exceeded 100 msecs */
  2064. if (duration > 100000ULL)
  2065. return (void)printf("! ");
  2066. /* Duration exceeded 10 msecs */
  2067. if (duration > 10000ULL)
  2068. return (void)printf("+ ");
  2069. printf(" ");
  2070. }
  2071. static void print_graph_duration(unsigned long long duration)
  2072. {
  2073. unsigned long usecs = duration / 1000;
  2074. unsigned long nsecs_rem = duration % 1000;
  2075. /* log10(ULONG_MAX) + '\0' */
  2076. char msecs_str[21];
  2077. char nsecs_str[5];
  2078. int len;
  2079. int i;
  2080. sprintf(msecs_str, "%lu", usecs);
  2081. /* Print msecs */
  2082. len = printf("%lu", usecs);
  2083. /* Print nsecs (we don't want to exceed 7 numbers) */
  2084. if (len < 7) {
  2085. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2086. len += printf(".%s", nsecs_str);
  2087. }
  2088. printf(" us ");
  2089. /* Print remaining spaces to fit the row's width */
  2090. for (i = len; i < 7; i++)
  2091. printf(" ");
  2092. printf("| ");
  2093. }
  2094. static void
  2095. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2096. {
  2097. unsigned long long rettime, calltime;
  2098. unsigned long long duration, depth;
  2099. unsigned long long val;
  2100. struct format_field *field;
  2101. struct func_map *func;
  2102. struct event *ret_event;
  2103. int type;
  2104. int i;
  2105. type = trace_parse_common_type(ret_rec->data);
  2106. ret_event = trace_find_event(type);
  2107. field = find_field(ret_event, "rettime");
  2108. if (!field)
  2109. die("can't find rettime in return graph");
  2110. rettime = read_size(ret_rec->data + field->offset, field->size);
  2111. field = find_field(ret_event, "calltime");
  2112. if (!field)
  2113. die("can't find rettime in return graph");
  2114. calltime = read_size(ret_rec->data + field->offset, field->size);
  2115. duration = rettime - calltime;
  2116. /* Overhead */
  2117. print_graph_overhead(duration);
  2118. /* Duration */
  2119. print_graph_duration(duration);
  2120. field = find_field(event, "depth");
  2121. if (!field)
  2122. die("can't find depth in entry graph");
  2123. depth = read_size(data + field->offset, field->size);
  2124. /* Function */
  2125. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2126. printf(" ");
  2127. field = find_field(event, "func");
  2128. if (!field)
  2129. die("can't find func in entry graph");
  2130. val = read_size(data + field->offset, field->size);
  2131. func = find_func(val);
  2132. if (func)
  2133. printf("%s();", func->func);
  2134. else
  2135. printf("%llx();", val);
  2136. }
  2137. static void print_graph_nested(struct event *event, void *data)
  2138. {
  2139. struct format_field *field;
  2140. unsigned long long depth;
  2141. unsigned long long val;
  2142. struct func_map *func;
  2143. int i;
  2144. /* No overhead */
  2145. print_graph_overhead(-1);
  2146. /* No time */
  2147. printf(" | ");
  2148. field = find_field(event, "depth");
  2149. if (!field)
  2150. die("can't find depth in entry graph");
  2151. depth = read_size(data + field->offset, field->size);
  2152. /* Function */
  2153. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2154. printf(" ");
  2155. field = find_field(event, "func");
  2156. if (!field)
  2157. die("can't find func in entry graph");
  2158. val = read_size(data + field->offset, field->size);
  2159. func = find_func(val);
  2160. if (func)
  2161. printf("%s() {", func->func);
  2162. else
  2163. printf("%llx() {", val);
  2164. }
  2165. static void
  2166. pretty_print_func_ent(void *data, int size, struct event *event,
  2167. int cpu, int pid, const char *comm,
  2168. unsigned long secs, unsigned long usecs)
  2169. {
  2170. struct format_field *field;
  2171. struct record *rec;
  2172. void *copy_data;
  2173. unsigned long val;
  2174. printf("%5lu.%06lu | ", secs, usecs);
  2175. print_graph_cpu(cpu);
  2176. print_graph_proc(pid, comm);
  2177. printf(" | ");
  2178. field = find_field(event, "func");
  2179. if (!field)
  2180. die("function entry does not have func field");
  2181. val = read_size(data + field->offset, field->size);
  2182. /*
  2183. * peek_data may unmap the data pointer. Copy it first.
  2184. */
  2185. copy_data = malloc_or_die(size);
  2186. memcpy(copy_data, data, size);
  2187. data = copy_data;
  2188. rec = trace_peek_data(cpu);
  2189. if (rec) {
  2190. rec = get_return_for_leaf(cpu, pid, val, rec);
  2191. if (rec) {
  2192. print_graph_entry_leaf(event, data, rec);
  2193. goto out_free;
  2194. }
  2195. }
  2196. print_graph_nested(event, data);
  2197. out_free:
  2198. free(data);
  2199. }
  2200. static void
  2201. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2202. int cpu, int pid, const char *comm,
  2203. unsigned long secs, unsigned long usecs)
  2204. {
  2205. unsigned long long rettime, calltime;
  2206. unsigned long long duration, depth;
  2207. struct format_field *field;
  2208. int i;
  2209. printf("%5lu.%06lu | ", secs, usecs);
  2210. print_graph_cpu(cpu);
  2211. print_graph_proc(pid, comm);
  2212. printf(" | ");
  2213. field = find_field(event, "rettime");
  2214. if (!field)
  2215. die("can't find rettime in return graph");
  2216. rettime = read_size(data + field->offset, field->size);
  2217. field = find_field(event, "calltime");
  2218. if (!field)
  2219. die("can't find calltime in return graph");
  2220. calltime = read_size(data + field->offset, field->size);
  2221. duration = rettime - calltime;
  2222. /* Overhead */
  2223. print_graph_overhead(duration);
  2224. /* Duration */
  2225. print_graph_duration(duration);
  2226. field = find_field(event, "depth");
  2227. if (!field)
  2228. die("can't find depth in entry graph");
  2229. depth = read_size(data + field->offset, field->size);
  2230. /* Function */
  2231. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2232. printf(" ");
  2233. printf("}");
  2234. }
  2235. static void
  2236. pretty_print_func_graph(void *data, int size, struct event *event,
  2237. int cpu, int pid, const char *comm,
  2238. unsigned long secs, unsigned long usecs)
  2239. {
  2240. if (event->flags & EVENT_FL_ISFUNCENT)
  2241. pretty_print_func_ent(data, size, event,
  2242. cpu, pid, comm, secs, usecs);
  2243. else if (event->flags & EVENT_FL_ISFUNCRET)
  2244. pretty_print_func_ret(data, size, event,
  2245. cpu, pid, comm, secs, usecs);
  2246. printf("\n");
  2247. }
  2248. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2249. char *comm)
  2250. {
  2251. struct event *event;
  2252. unsigned long secs;
  2253. unsigned long usecs;
  2254. int type;
  2255. int pid;
  2256. secs = nsecs / NSECS_PER_SEC;
  2257. nsecs -= secs * NSECS_PER_SEC;
  2258. usecs = nsecs / NSECS_PER_USEC;
  2259. type = trace_parse_common_type(data);
  2260. event = trace_find_event(type);
  2261. if (!event) {
  2262. printf("ug! no event found for type %d\n", type);
  2263. return;
  2264. }
  2265. pid = parse_common_pid(data);
  2266. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2267. return pretty_print_func_graph(data, size, event, cpu,
  2268. pid, comm, secs, usecs);
  2269. printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
  2270. comm, pid, cpu,
  2271. secs, nsecs, event->name);
  2272. pretty_print(data, size, event);
  2273. printf("\n");
  2274. }
  2275. static void print_fields(struct print_flag_sym *field)
  2276. {
  2277. printf("{ %s, %s }", field->value, field->str);
  2278. if (field->next) {
  2279. printf(", ");
  2280. print_fields(field->next);
  2281. }
  2282. }
  2283. static void print_args(struct print_arg *args)
  2284. {
  2285. int print_paren = 1;
  2286. switch (args->type) {
  2287. case PRINT_NULL:
  2288. printf("null");
  2289. break;
  2290. case PRINT_ATOM:
  2291. printf("%s", args->atom.atom);
  2292. break;
  2293. case PRINT_FIELD:
  2294. printf("REC->%s", args->field.name);
  2295. break;
  2296. case PRINT_FLAGS:
  2297. printf("__print_flags(");
  2298. print_args(args->flags.field);
  2299. printf(", %s, ", args->flags.delim);
  2300. print_fields(args->flags.flags);
  2301. printf(")");
  2302. break;
  2303. case PRINT_SYMBOL:
  2304. printf("__print_symbolic(");
  2305. print_args(args->symbol.field);
  2306. printf(", ");
  2307. print_fields(args->symbol.symbols);
  2308. printf(")");
  2309. break;
  2310. case PRINT_STRING:
  2311. printf("__get_str(%s)", args->string.string);
  2312. break;
  2313. case PRINT_TYPE:
  2314. printf("(%s)", args->typecast.type);
  2315. print_args(args->typecast.item);
  2316. break;
  2317. case PRINT_OP:
  2318. if (strcmp(args->op.op, ":") == 0)
  2319. print_paren = 0;
  2320. if (print_paren)
  2321. printf("(");
  2322. print_args(args->op.left);
  2323. printf(" %s ", args->op.op);
  2324. print_args(args->op.right);
  2325. if (print_paren)
  2326. printf(")");
  2327. break;
  2328. default:
  2329. /* we should warn... */
  2330. return;
  2331. }
  2332. if (args->next) {
  2333. printf("\n");
  2334. print_args(args->next);
  2335. }
  2336. }
  2337. static void parse_header_field(char *type,
  2338. int *offset, int *size)
  2339. {
  2340. char *token;
  2341. if (read_expected(EVENT_ITEM, (char *)"field") < 0)
  2342. return;
  2343. if (read_expected(EVENT_OP, (char *)":") < 0)
  2344. return;
  2345. /* type */
  2346. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2347. return;
  2348. free_token(token);
  2349. if (read_expected(EVENT_ITEM, type) < 0)
  2350. return;
  2351. if (read_expected(EVENT_OP, (char *)";") < 0)
  2352. return;
  2353. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  2354. return;
  2355. if (read_expected(EVENT_OP, (char *)":") < 0)
  2356. return;
  2357. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2358. return;
  2359. *offset = atoi(token);
  2360. free_token(token);
  2361. if (read_expected(EVENT_OP, (char *)";") < 0)
  2362. return;
  2363. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  2364. return;
  2365. if (read_expected(EVENT_OP, (char *)":") < 0)
  2366. return;
  2367. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2368. return;
  2369. *size = atoi(token);
  2370. free_token(token);
  2371. if (read_expected(EVENT_OP, (char *)";") < 0)
  2372. return;
  2373. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  2374. return;
  2375. free_token(token);
  2376. }
  2377. int parse_header_page(char *buf, unsigned long size)
  2378. {
  2379. init_input_buf(buf, size);
  2380. parse_header_field((char *)"timestamp", &header_page_ts_offset,
  2381. &header_page_ts_size);
  2382. parse_header_field((char *)"commit", &header_page_size_offset,
  2383. &header_page_size_size);
  2384. parse_header_field((char *)"data", &header_page_data_offset,
  2385. &header_page_data_size);
  2386. return 0;
  2387. }
  2388. int parse_ftrace_file(char *buf, unsigned long size)
  2389. {
  2390. struct format_field *field;
  2391. struct print_arg *arg, **list;
  2392. struct event *event;
  2393. int ret;
  2394. init_input_buf(buf, size);
  2395. event = alloc_event();
  2396. if (!event)
  2397. return -ENOMEM;
  2398. event->flags |= EVENT_FL_ISFTRACE;
  2399. event->name = event_read_name();
  2400. if (!event->name)
  2401. die("failed to read ftrace event name");
  2402. if (strcmp(event->name, "function") == 0)
  2403. event->flags |= EVENT_FL_ISFUNC;
  2404. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2405. event->flags |= EVENT_FL_ISFUNCENT;
  2406. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2407. event->flags |= EVENT_FL_ISFUNCRET;
  2408. else if (strcmp(event->name, "bprint") == 0)
  2409. event->flags |= EVENT_FL_ISBPRINT;
  2410. event->id = event_read_id();
  2411. if (event->id < 0)
  2412. die("failed to read ftrace event id");
  2413. add_event(event);
  2414. ret = event_read_format(event);
  2415. if (ret < 0)
  2416. die("failed to read ftrace event format");
  2417. ret = event_read_print(event);
  2418. if (ret < 0)
  2419. die("failed to read ftrace event print fmt");
  2420. /*
  2421. * The arguments for ftrace files are parsed by the fields.
  2422. * Set up the fields as their arguments.
  2423. */
  2424. list = &event->print_fmt.args;
  2425. for (field = event->format.fields; field; field = field->next) {
  2426. arg = malloc_or_die(sizeof(*arg));
  2427. memset(arg, 0, sizeof(*arg));
  2428. *list = arg;
  2429. list = &arg->next;
  2430. arg->type = PRINT_FIELD;
  2431. arg->field.name = field->name;
  2432. arg->field.field = field;
  2433. }
  2434. return 0;
  2435. }
  2436. int parse_event_file(char *buf, unsigned long size, char *system__unused __unused)
  2437. {
  2438. struct event *event;
  2439. int ret;
  2440. init_input_buf(buf, size);
  2441. event = alloc_event();
  2442. if (!event)
  2443. return -ENOMEM;
  2444. event->name = event_read_name();
  2445. if (!event->name)
  2446. die("failed to read event name");
  2447. event->id = event_read_id();
  2448. if (event->id < 0)
  2449. die("failed to read event id");
  2450. ret = event_read_format(event);
  2451. if (ret < 0)
  2452. die("failed to read event format");
  2453. ret = event_read_print(event);
  2454. if (ret < 0)
  2455. die("failed to read event print fmt");
  2456. #define PRINT_ARGS 0
  2457. if (PRINT_ARGS && event->print_fmt.args)
  2458. print_args(event->print_fmt.args);
  2459. add_event(event);
  2460. return 0;
  2461. }
  2462. void parse_set_info(int nr_cpus, int long_sz)
  2463. {
  2464. cpus = nr_cpus;
  2465. long_size = long_sz;
  2466. }