trace-event-parse.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057
  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. /* the '\' '\' will cancel itself */
  444. if (ch == '\\' && last_ch == '\\')
  445. last_ch = 0;
  446. } while (ch != quote_ch || last_ch == '\\');
  447. /* remove the last quote */
  448. i--;
  449. goto out;
  450. case EVENT_ERROR ... EVENT_SPACE:
  451. case EVENT_ITEM:
  452. default:
  453. break;
  454. }
  455. while (get_type(__peek_char()) == type) {
  456. if (i == (BUFSIZ - 1)) {
  457. buf[i] = 0;
  458. if (*tok) {
  459. *tok = realloc(*tok, tok_size + BUFSIZ);
  460. if (!*tok)
  461. return EVENT_NONE;
  462. strcat(*tok, buf);
  463. } else
  464. *tok = strdup(buf);
  465. if (!*tok)
  466. return EVENT_NONE;
  467. tok_size += BUFSIZ;
  468. i = 0;
  469. }
  470. ch = __read_char();
  471. buf[i++] = ch;
  472. }
  473. out:
  474. buf[i] = 0;
  475. if (*tok) {
  476. *tok = realloc(*tok, tok_size + i);
  477. if (!*tok)
  478. return EVENT_NONE;
  479. strcat(*tok, buf);
  480. } else
  481. *tok = strdup(buf);
  482. if (!*tok)
  483. return EVENT_NONE;
  484. return type;
  485. }
  486. static void free_token(char *tok)
  487. {
  488. if (tok)
  489. free(tok);
  490. }
  491. static enum event_type read_token(char **tok)
  492. {
  493. enum event_type type;
  494. for (;;) {
  495. type = __read_token(tok);
  496. if (type != EVENT_SPACE)
  497. return type;
  498. free_token(*tok);
  499. }
  500. /* not reached */
  501. return EVENT_NONE;
  502. }
  503. /* no newline */
  504. static enum event_type read_token_item(char **tok)
  505. {
  506. enum event_type type;
  507. for (;;) {
  508. type = __read_token(tok);
  509. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  510. return type;
  511. free_token(*tok);
  512. }
  513. /* not reached */
  514. return EVENT_NONE;
  515. }
  516. static int test_type(enum event_type type, enum event_type expect)
  517. {
  518. if (type != expect) {
  519. die("Error: expected type %d but read %d",
  520. expect, type);
  521. return -1;
  522. }
  523. return 0;
  524. }
  525. static int test_type_token(enum event_type type, char *token,
  526. enum event_type expect, const char *expect_tok)
  527. {
  528. if (type != expect) {
  529. die("Error: expected type %d but read %d",
  530. expect, type);
  531. return -1;
  532. }
  533. if (strcmp(token, expect_tok) != 0) {
  534. die("Error: expected '%s' but read '%s'",
  535. expect_tok, token);
  536. return -1;
  537. }
  538. return 0;
  539. }
  540. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  541. {
  542. enum event_type type;
  543. if (newline_ok)
  544. type = read_token(tok);
  545. else
  546. type = read_token_item(tok);
  547. return test_type(type, expect);
  548. }
  549. static int read_expect_type(enum event_type expect, char **tok)
  550. {
  551. return __read_expect_type(expect, tok, 1);
  552. }
  553. static int __read_expected(enum event_type expect, const char *str, int newline_ok)
  554. {
  555. enum event_type type;
  556. char *token;
  557. int ret;
  558. if (newline_ok)
  559. type = read_token(&token);
  560. else
  561. type = read_token_item(&token);
  562. ret = test_type_token(type, token, expect, str);
  563. free_token(token);
  564. return 0;
  565. }
  566. static int read_expected(enum event_type expect, const char *str)
  567. {
  568. return __read_expected(expect, str, 1);
  569. }
  570. static int read_expected_item(enum event_type expect, const char *str)
  571. {
  572. return __read_expected(expect, str, 0);
  573. }
  574. static char *event_read_name(void)
  575. {
  576. char *token;
  577. if (read_expected(EVENT_ITEM, (char *)"name") < 0)
  578. return NULL;
  579. if (read_expected(EVENT_OP, (char *)":") < 0)
  580. return NULL;
  581. if (read_expect_type(EVENT_ITEM, &token) < 0)
  582. goto fail;
  583. return token;
  584. fail:
  585. free_token(token);
  586. return NULL;
  587. }
  588. static int event_read_id(void)
  589. {
  590. char *token;
  591. int id;
  592. if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
  593. return -1;
  594. if (read_expected(EVENT_OP, (char *)":") < 0)
  595. return -1;
  596. if (read_expect_type(EVENT_ITEM, &token) < 0)
  597. goto fail;
  598. id = strtoul(token, NULL, 0);
  599. free_token(token);
  600. return id;
  601. fail:
  602. free_token(token);
  603. return -1;
  604. }
  605. static int field_is_string(struct format_field *field)
  606. {
  607. if ((field->flags & FIELD_IS_ARRAY) &&
  608. (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
  609. !strstr(field->type, "s8")))
  610. return 1;
  611. return 0;
  612. }
  613. static int field_is_dynamic(struct format_field *field)
  614. {
  615. if (!strcmp(field->type, "__data_loc"))
  616. return 1;
  617. return 0;
  618. }
  619. static int event_read_fields(struct event *event, struct format_field **fields)
  620. {
  621. struct format_field *field = NULL;
  622. enum event_type type;
  623. char *token;
  624. char *last_token;
  625. int count = 0;
  626. do {
  627. type = read_token(&token);
  628. if (type == EVENT_NEWLINE) {
  629. free_token(token);
  630. return count;
  631. }
  632. count++;
  633. if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
  634. goto fail;
  635. free_token(token);
  636. type = read_token(&token);
  637. /*
  638. * The ftrace fields may still use the "special" name.
  639. * Just ignore it.
  640. */
  641. if (event->flags & EVENT_FL_ISFTRACE &&
  642. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  643. free_token(token);
  644. type = read_token(&token);
  645. }
  646. if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
  647. return -1;
  648. if (read_expect_type(EVENT_ITEM, &token) < 0)
  649. goto fail;
  650. last_token = token;
  651. field = malloc_or_die(sizeof(*field));
  652. memset(field, 0, sizeof(*field));
  653. /* read the rest of the type */
  654. for (;;) {
  655. type = read_token(&token);
  656. if (type == EVENT_ITEM ||
  657. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  658. /*
  659. * Some of the ftrace fields are broken and have
  660. * an illegal "." in them.
  661. */
  662. (event->flags & EVENT_FL_ISFTRACE &&
  663. type == EVENT_OP && strcmp(token, ".") == 0)) {
  664. if (strcmp(token, "*") == 0)
  665. field->flags |= FIELD_IS_POINTER;
  666. if (field->type) {
  667. field->type = realloc(field->type,
  668. strlen(field->type) +
  669. strlen(last_token) + 2);
  670. strcat(field->type, " ");
  671. strcat(field->type, last_token);
  672. } else
  673. field->type = last_token;
  674. last_token = token;
  675. continue;
  676. }
  677. break;
  678. }
  679. if (!field->type) {
  680. die("no type found");
  681. goto fail;
  682. }
  683. field->name = last_token;
  684. if (test_type(type, EVENT_OP))
  685. goto fail;
  686. if (strcmp(token, "[") == 0) {
  687. enum event_type last_type = type;
  688. char *brackets = token;
  689. int len;
  690. field->flags |= FIELD_IS_ARRAY;
  691. type = read_token(&token);
  692. while (strcmp(token, "]") != 0) {
  693. if (last_type == EVENT_ITEM &&
  694. type == EVENT_ITEM)
  695. len = 2;
  696. else
  697. len = 1;
  698. last_type = type;
  699. brackets = realloc(brackets,
  700. strlen(brackets) +
  701. strlen(token) + len);
  702. if (len == 2)
  703. strcat(brackets, " ");
  704. strcat(brackets, token);
  705. free_token(token);
  706. type = read_token(&token);
  707. if (type == EVENT_NONE) {
  708. die("failed to find token");
  709. goto fail;
  710. }
  711. }
  712. free_token(token);
  713. brackets = realloc(brackets, strlen(brackets) + 2);
  714. strcat(brackets, "]");
  715. /* add brackets to type */
  716. type = read_token(&token);
  717. /*
  718. * If the next token is not an OP, then it is of
  719. * the format: type [] item;
  720. */
  721. if (type == EVENT_ITEM) {
  722. field->type = realloc(field->type,
  723. strlen(field->type) +
  724. strlen(field->name) +
  725. strlen(brackets) + 2);
  726. strcat(field->type, " ");
  727. strcat(field->type, field->name);
  728. free_token(field->name);
  729. strcat(field->type, brackets);
  730. field->name = token;
  731. type = read_token(&token);
  732. } else {
  733. field->type = realloc(field->type,
  734. strlen(field->type) +
  735. strlen(brackets) + 1);
  736. strcat(field->type, brackets);
  737. }
  738. free(brackets);
  739. }
  740. if (field_is_string(field)) {
  741. field->flags |= FIELD_IS_STRING;
  742. if (field_is_dynamic(field))
  743. field->flags |= FIELD_IS_DYNAMIC;
  744. }
  745. if (test_type_token(type, token, EVENT_OP, (char *)";"))
  746. goto fail;
  747. free_token(token);
  748. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  749. goto fail_expect;
  750. if (read_expected(EVENT_OP, (char *)":") < 0)
  751. goto fail_expect;
  752. if (read_expect_type(EVENT_ITEM, &token))
  753. goto fail;
  754. field->offset = strtoul(token, NULL, 0);
  755. free_token(token);
  756. if (read_expected(EVENT_OP, (char *)";") < 0)
  757. goto fail_expect;
  758. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  759. goto fail_expect;
  760. if (read_expected(EVENT_OP, (char *)":") < 0)
  761. goto fail_expect;
  762. if (read_expect_type(EVENT_ITEM, &token))
  763. goto fail;
  764. field->size = strtoul(token, NULL, 0);
  765. free_token(token);
  766. if (read_expected(EVENT_OP, (char *)";") < 0)
  767. goto fail_expect;
  768. if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
  769. goto fail_expect;
  770. if (read_expected(EVENT_OP, (char *)":") < 0)
  771. goto fail_expect;
  772. if (read_expect_type(EVENT_ITEM, &token))
  773. goto fail;
  774. if (strtoul(token, NULL, 0))
  775. field->flags |= FIELD_IS_SIGNED;
  776. free_token(token);
  777. if (read_expected(EVENT_OP, (char *)";") < 0)
  778. goto fail_expect;
  779. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  780. goto fail;
  781. free_token(token);
  782. *fields = field;
  783. fields = &field->next;
  784. } while (1);
  785. return 0;
  786. fail:
  787. free_token(token);
  788. fail_expect:
  789. if (field)
  790. free(field);
  791. return -1;
  792. }
  793. static int event_read_format(struct event *event)
  794. {
  795. char *token;
  796. int ret;
  797. if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
  798. return -1;
  799. if (read_expected(EVENT_OP, (char *)":") < 0)
  800. return -1;
  801. if (read_expect_type(EVENT_NEWLINE, &token))
  802. goto fail;
  803. free_token(token);
  804. ret = event_read_fields(event, &event->format.common_fields);
  805. if (ret < 0)
  806. return ret;
  807. event->format.nr_common = ret;
  808. ret = event_read_fields(event, &event->format.fields);
  809. if (ret < 0)
  810. return ret;
  811. event->format.nr_fields = ret;
  812. return 0;
  813. fail:
  814. free_token(token);
  815. return -1;
  816. }
  817. enum event_type
  818. process_arg_token(struct event *event, struct print_arg *arg,
  819. char **tok, enum event_type type);
  820. static enum event_type
  821. process_arg(struct event *event, struct print_arg *arg, char **tok)
  822. {
  823. enum event_type type;
  824. char *token;
  825. type = read_token(&token);
  826. *tok = token;
  827. return process_arg_token(event, arg, tok, type);
  828. }
  829. static enum event_type
  830. process_cond(struct event *event, struct print_arg *top, char **tok)
  831. {
  832. struct print_arg *arg, *left, *right;
  833. enum event_type type;
  834. char *token = NULL;
  835. arg = malloc_or_die(sizeof(*arg));
  836. memset(arg, 0, sizeof(*arg));
  837. left = malloc_or_die(sizeof(*left));
  838. right = malloc_or_die(sizeof(*right));
  839. arg->type = PRINT_OP;
  840. arg->op.left = left;
  841. arg->op.right = right;
  842. *tok = NULL;
  843. type = process_arg(event, left, &token);
  844. if (test_type_token(type, token, EVENT_OP, (char *)":"))
  845. goto out_free;
  846. arg->op.op = token;
  847. type = process_arg(event, right, &token);
  848. top->op.right = arg;
  849. *tok = token;
  850. return type;
  851. out_free:
  852. free_token(*tok);
  853. free(right);
  854. free(left);
  855. free_arg(arg);
  856. return EVENT_ERROR;
  857. }
  858. static int get_op_prio(char *op)
  859. {
  860. if (!op[1]) {
  861. switch (op[0]) {
  862. case '*':
  863. case '/':
  864. case '%':
  865. return 6;
  866. case '+':
  867. case '-':
  868. return 7;
  869. /* '>>' and '<<' are 8 */
  870. case '<':
  871. case '>':
  872. return 9;
  873. /* '==' and '!=' are 10 */
  874. case '&':
  875. return 11;
  876. case '^':
  877. return 12;
  878. case '|':
  879. return 13;
  880. case '?':
  881. return 16;
  882. default:
  883. die("unknown op '%c'", op[0]);
  884. return -1;
  885. }
  886. } else {
  887. if (strcmp(op, "++") == 0 ||
  888. strcmp(op, "--") == 0) {
  889. return 3;
  890. } else if (strcmp(op, ">>") == 0 ||
  891. strcmp(op, "<<") == 0) {
  892. return 8;
  893. } else if (strcmp(op, ">=") == 0 ||
  894. strcmp(op, "<=") == 0) {
  895. return 9;
  896. } else if (strcmp(op, "==") == 0 ||
  897. strcmp(op, "!=") == 0) {
  898. return 10;
  899. } else if (strcmp(op, "&&") == 0) {
  900. return 14;
  901. } else if (strcmp(op, "||") == 0) {
  902. return 15;
  903. } else {
  904. die("unknown op '%s'", op);
  905. return -1;
  906. }
  907. }
  908. }
  909. static void set_op_prio(struct print_arg *arg)
  910. {
  911. /* single ops are the greatest */
  912. if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
  913. arg->op.prio = 0;
  914. return;
  915. }
  916. arg->op.prio = get_op_prio(arg->op.op);
  917. }
  918. static enum event_type
  919. process_op(struct event *event, struct print_arg *arg, char **tok)
  920. {
  921. struct print_arg *left, *right = NULL;
  922. enum event_type type;
  923. char *token;
  924. /* the op is passed in via tok */
  925. token = *tok;
  926. if (arg->type == PRINT_OP && !arg->op.left) {
  927. /* handle single op */
  928. if (token[1]) {
  929. die("bad op token %s", token);
  930. return EVENT_ERROR;
  931. }
  932. switch (token[0]) {
  933. case '!':
  934. case '+':
  935. case '-':
  936. break;
  937. default:
  938. die("bad op token %s", token);
  939. return EVENT_ERROR;
  940. }
  941. /* make an empty left */
  942. left = malloc_or_die(sizeof(*left));
  943. left->type = PRINT_NULL;
  944. arg->op.left = left;
  945. right = malloc_or_die(sizeof(*right));
  946. arg->op.right = right;
  947. type = process_arg(event, right, tok);
  948. } else if (strcmp(token, "?") == 0) {
  949. left = malloc_or_die(sizeof(*left));
  950. /* copy the top arg to the left */
  951. *left = *arg;
  952. arg->type = PRINT_OP;
  953. arg->op.op = token;
  954. arg->op.left = left;
  955. arg->op.prio = 0;
  956. type = process_cond(event, arg, tok);
  957. } else if (strcmp(token, ">>") == 0 ||
  958. strcmp(token, "<<") == 0 ||
  959. strcmp(token, "&") == 0 ||
  960. strcmp(token, "|") == 0 ||
  961. strcmp(token, "&&") == 0 ||
  962. strcmp(token, "||") == 0 ||
  963. strcmp(token, "-") == 0 ||
  964. strcmp(token, "+") == 0 ||
  965. strcmp(token, "*") == 0 ||
  966. strcmp(token, "^") == 0 ||
  967. strcmp(token, "/") == 0 ||
  968. strcmp(token, "==") == 0 ||
  969. strcmp(token, "!=") == 0) {
  970. left = malloc_or_die(sizeof(*left));
  971. /* copy the top arg to the left */
  972. *left = *arg;
  973. arg->type = PRINT_OP;
  974. arg->op.op = token;
  975. arg->op.left = left;
  976. set_op_prio(arg);
  977. right = malloc_or_die(sizeof(*right));
  978. type = process_arg(event, right, tok);
  979. arg->op.right = right;
  980. } else {
  981. die("unknown op '%s'", token);
  982. /* the arg is now the left side */
  983. return EVENT_NONE;
  984. }
  985. if (type == EVENT_OP) {
  986. int prio;
  987. /* higher prios need to be closer to the root */
  988. prio = get_op_prio(*tok);
  989. if (prio > arg->op.prio)
  990. return process_op(event, arg, tok);
  991. return process_op(event, right, tok);
  992. }
  993. return type;
  994. }
  995. static enum event_type
  996. process_entry(struct event *event __unused, struct print_arg *arg,
  997. char **tok)
  998. {
  999. enum event_type type;
  1000. char *field;
  1001. char *token;
  1002. if (read_expected(EVENT_OP, (char *)"->") < 0)
  1003. return EVENT_ERROR;
  1004. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1005. goto fail;
  1006. field = token;
  1007. arg->type = PRINT_FIELD;
  1008. arg->field.name = field;
  1009. type = read_token(&token);
  1010. *tok = token;
  1011. return type;
  1012. fail:
  1013. free_token(token);
  1014. return EVENT_ERROR;
  1015. }
  1016. static char *arg_eval (struct print_arg *arg);
  1017. static long long arg_num_eval(struct print_arg *arg)
  1018. {
  1019. long long left, right;
  1020. long long val = 0;
  1021. switch (arg->type) {
  1022. case PRINT_ATOM:
  1023. val = strtoll(arg->atom.atom, NULL, 0);
  1024. break;
  1025. case PRINT_TYPE:
  1026. val = arg_num_eval(arg->typecast.item);
  1027. break;
  1028. case PRINT_OP:
  1029. switch (arg->op.op[0]) {
  1030. case '|':
  1031. left = arg_num_eval(arg->op.left);
  1032. right = arg_num_eval(arg->op.right);
  1033. if (arg->op.op[1])
  1034. val = left || right;
  1035. else
  1036. val = left | right;
  1037. break;
  1038. case '&':
  1039. left = arg_num_eval(arg->op.left);
  1040. right = arg_num_eval(arg->op.right);
  1041. if (arg->op.op[1])
  1042. val = left && right;
  1043. else
  1044. val = left & right;
  1045. break;
  1046. case '<':
  1047. left = arg_num_eval(arg->op.left);
  1048. right = arg_num_eval(arg->op.right);
  1049. switch (arg->op.op[1]) {
  1050. case 0:
  1051. val = left < right;
  1052. break;
  1053. case '<':
  1054. val = left << right;
  1055. break;
  1056. case '=':
  1057. val = left <= right;
  1058. break;
  1059. default:
  1060. die("unknown op '%s'", arg->op.op);
  1061. }
  1062. break;
  1063. case '>':
  1064. left = arg_num_eval(arg->op.left);
  1065. right = arg_num_eval(arg->op.right);
  1066. switch (arg->op.op[1]) {
  1067. case 0:
  1068. val = left > right;
  1069. break;
  1070. case '>':
  1071. val = left >> right;
  1072. break;
  1073. case '=':
  1074. val = left >= right;
  1075. break;
  1076. default:
  1077. die("unknown op '%s'", arg->op.op);
  1078. }
  1079. break;
  1080. case '=':
  1081. left = arg_num_eval(arg->op.left);
  1082. right = arg_num_eval(arg->op.right);
  1083. if (arg->op.op[1] != '=')
  1084. die("unknown op '%s'", arg->op.op);
  1085. val = left == right;
  1086. break;
  1087. case '!':
  1088. left = arg_num_eval(arg->op.left);
  1089. right = arg_num_eval(arg->op.right);
  1090. switch (arg->op.op[1]) {
  1091. case '=':
  1092. val = left != right;
  1093. break;
  1094. default:
  1095. die("unknown op '%s'", arg->op.op);
  1096. }
  1097. break;
  1098. default:
  1099. die("unknown op '%s'", arg->op.op);
  1100. }
  1101. break;
  1102. case PRINT_NULL:
  1103. case PRINT_FIELD ... PRINT_SYMBOL:
  1104. case PRINT_STRING:
  1105. default:
  1106. die("invalid eval type %d", arg->type);
  1107. }
  1108. return val;
  1109. }
  1110. static char *arg_eval (struct print_arg *arg)
  1111. {
  1112. long long val;
  1113. static char buf[20];
  1114. switch (arg->type) {
  1115. case PRINT_ATOM:
  1116. return arg->atom.atom;
  1117. case PRINT_TYPE:
  1118. return arg_eval(arg->typecast.item);
  1119. case PRINT_OP:
  1120. val = arg_num_eval(arg);
  1121. sprintf(buf, "%lld", val);
  1122. return buf;
  1123. case PRINT_NULL:
  1124. case PRINT_FIELD ... PRINT_SYMBOL:
  1125. case PRINT_STRING:
  1126. default:
  1127. die("invalid eval type %d", arg->type);
  1128. break;
  1129. }
  1130. return NULL;
  1131. }
  1132. static enum event_type
  1133. process_fields(struct event *event, struct print_flag_sym **list, char **tok)
  1134. {
  1135. enum event_type type;
  1136. struct print_arg *arg = NULL;
  1137. struct print_flag_sym *field;
  1138. char *token = NULL;
  1139. char *value;
  1140. do {
  1141. free_token(token);
  1142. type = read_token_item(&token);
  1143. if (test_type_token(type, token, EVENT_OP, (char *)"{"))
  1144. break;
  1145. arg = malloc_or_die(sizeof(*arg));
  1146. free_token(token);
  1147. type = process_arg(event, arg, &token);
  1148. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1149. goto out_free;
  1150. field = malloc_or_die(sizeof(*field));
  1151. memset(field, 0, sizeof(field));
  1152. value = arg_eval(arg);
  1153. field->value = strdup(value);
  1154. free_token(token);
  1155. type = process_arg(event, arg, &token);
  1156. if (test_type_token(type, token, EVENT_OP, (char *)"}"))
  1157. goto out_free;
  1158. value = arg_eval(arg);
  1159. field->str = strdup(value);
  1160. free_arg(arg);
  1161. arg = NULL;
  1162. *list = field;
  1163. list = &field->next;
  1164. free_token(token);
  1165. type = read_token_item(&token);
  1166. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1167. *tok = token;
  1168. return type;
  1169. out_free:
  1170. free_arg(arg);
  1171. free_token(token);
  1172. return EVENT_ERROR;
  1173. }
  1174. static enum event_type
  1175. process_flags(struct event *event, struct print_arg *arg, char **tok)
  1176. {
  1177. struct print_arg *field;
  1178. enum event_type type;
  1179. char *token;
  1180. memset(arg, 0, sizeof(*arg));
  1181. arg->type = PRINT_FLAGS;
  1182. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1183. return EVENT_ERROR;
  1184. field = malloc_or_die(sizeof(*field));
  1185. type = process_arg(event, field, &token);
  1186. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1187. goto out_free;
  1188. arg->flags.field = field;
  1189. type = read_token_item(&token);
  1190. if (event_item_type(type)) {
  1191. arg->flags.delim = token;
  1192. type = read_token_item(&token);
  1193. }
  1194. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1195. goto out_free;
  1196. type = process_fields(event, &arg->flags.flags, &token);
  1197. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1198. goto out_free;
  1199. free_token(token);
  1200. type = read_token_item(tok);
  1201. return type;
  1202. out_free:
  1203. free_token(token);
  1204. return EVENT_ERROR;
  1205. }
  1206. static enum event_type
  1207. process_symbols(struct event *event, struct print_arg *arg, char **tok)
  1208. {
  1209. struct print_arg *field;
  1210. enum event_type type;
  1211. char *token;
  1212. memset(arg, 0, sizeof(*arg));
  1213. arg->type = PRINT_SYMBOL;
  1214. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1215. return EVENT_ERROR;
  1216. field = malloc_or_die(sizeof(*field));
  1217. type = process_arg(event, field, &token);
  1218. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1219. goto out_free;
  1220. arg->symbol.field = field;
  1221. type = process_fields(event, &arg->symbol.symbols, &token);
  1222. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1223. goto out_free;
  1224. free_token(token);
  1225. type = read_token_item(tok);
  1226. return type;
  1227. out_free:
  1228. free_token(token);
  1229. return EVENT_ERROR;
  1230. }
  1231. static enum event_type
  1232. process_paren(struct event *event, struct print_arg *arg, char **tok)
  1233. {
  1234. struct print_arg *item_arg;
  1235. enum event_type type;
  1236. int ptr_cast = 0;
  1237. char *token;
  1238. type = process_arg(event, arg, &token);
  1239. if (type == EVENT_ERROR)
  1240. return EVENT_ERROR;
  1241. if (type == EVENT_OP) {
  1242. /* handle the ptr casts */
  1243. if (!strcmp(token, "*")) {
  1244. /*
  1245. * FIXME: should we zapp whitespaces before ')' ?
  1246. * (may require a peek_token_item())
  1247. */
  1248. if (__peek_char() == ')') {
  1249. ptr_cast = 1;
  1250. free_token(token);
  1251. type = read_token_item(&token);
  1252. }
  1253. }
  1254. if (!ptr_cast) {
  1255. type = process_op(event, arg, &token);
  1256. if (type == EVENT_ERROR)
  1257. return EVENT_ERROR;
  1258. }
  1259. }
  1260. if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
  1261. free_token(token);
  1262. return EVENT_ERROR;
  1263. }
  1264. free_token(token);
  1265. type = read_token_item(&token);
  1266. /*
  1267. * If the next token is an item or another open paren, then
  1268. * this was a typecast.
  1269. */
  1270. if (event_item_type(type) ||
  1271. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1272. /* make this a typecast and contine */
  1273. /* prevous must be an atom */
  1274. if (arg->type != PRINT_ATOM)
  1275. die("previous needed to be PRINT_ATOM");
  1276. item_arg = malloc_or_die(sizeof(*item_arg));
  1277. arg->type = PRINT_TYPE;
  1278. if (ptr_cast) {
  1279. char *old = arg->atom.atom;
  1280. arg->atom.atom = malloc_or_die(strlen(old + 3));
  1281. sprintf(arg->atom.atom, "%s *", old);
  1282. free(old);
  1283. }
  1284. arg->typecast.type = arg->atom.atom;
  1285. arg->typecast.item = item_arg;
  1286. type = process_arg_token(event, item_arg, &token, type);
  1287. }
  1288. *tok = token;
  1289. return type;
  1290. }
  1291. static enum event_type
  1292. process_str(struct event *event __unused, struct print_arg *arg, char **tok)
  1293. {
  1294. enum event_type type;
  1295. char *token;
  1296. if (read_expected(EVENT_DELIM, (char *)"(") < 0)
  1297. return EVENT_ERROR;
  1298. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1299. goto fail;
  1300. arg->type = PRINT_STRING;
  1301. arg->string.string = token;
  1302. arg->string.offset = -1;
  1303. if (read_expected(EVENT_DELIM, (char *)")") < 0)
  1304. return EVENT_ERROR;
  1305. type = read_token(&token);
  1306. *tok = token;
  1307. return type;
  1308. fail:
  1309. free_token(token);
  1310. return EVENT_ERROR;
  1311. }
  1312. enum event_type
  1313. process_arg_token(struct event *event, struct print_arg *arg,
  1314. char **tok, enum event_type type)
  1315. {
  1316. char *token;
  1317. char *atom;
  1318. token = *tok;
  1319. switch (type) {
  1320. case EVENT_ITEM:
  1321. if (strcmp(token, "REC") == 0) {
  1322. free_token(token);
  1323. type = process_entry(event, arg, &token);
  1324. } else if (strcmp(token, "__print_flags") == 0) {
  1325. free_token(token);
  1326. type = process_flags(event, arg, &token);
  1327. } else if (strcmp(token, "__print_symbolic") == 0) {
  1328. free_token(token);
  1329. type = process_symbols(event, arg, &token);
  1330. } else if (strcmp(token, "__get_str") == 0) {
  1331. free_token(token);
  1332. type = process_str(event, arg, &token);
  1333. } else {
  1334. atom = token;
  1335. /* test the next token */
  1336. type = read_token_item(&token);
  1337. /* atoms can be more than one token long */
  1338. while (type == EVENT_ITEM) {
  1339. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  1340. strcat(atom, " ");
  1341. strcat(atom, token);
  1342. free_token(token);
  1343. type = read_token_item(&token);
  1344. }
  1345. /* todo, test for function */
  1346. arg->type = PRINT_ATOM;
  1347. arg->atom.atom = atom;
  1348. }
  1349. break;
  1350. case EVENT_DQUOTE:
  1351. case EVENT_SQUOTE:
  1352. arg->type = PRINT_ATOM;
  1353. arg->atom.atom = token;
  1354. type = read_token_item(&token);
  1355. break;
  1356. case EVENT_DELIM:
  1357. if (strcmp(token, "(") == 0) {
  1358. free_token(token);
  1359. type = process_paren(event, arg, &token);
  1360. break;
  1361. }
  1362. case EVENT_OP:
  1363. /* handle single ops */
  1364. arg->type = PRINT_OP;
  1365. arg->op.op = token;
  1366. arg->op.left = NULL;
  1367. type = process_op(event, arg, &token);
  1368. break;
  1369. case EVENT_ERROR ... EVENT_NEWLINE:
  1370. default:
  1371. die("unexpected type %d", type);
  1372. }
  1373. *tok = token;
  1374. return type;
  1375. }
  1376. static int event_read_print_args(struct event *event, struct print_arg **list)
  1377. {
  1378. enum event_type type;
  1379. struct print_arg *arg;
  1380. char *token;
  1381. int args = 0;
  1382. do {
  1383. arg = malloc_or_die(sizeof(*arg));
  1384. memset(arg, 0, sizeof(*arg));
  1385. type = process_arg(event, arg, &token);
  1386. if (type == EVENT_ERROR) {
  1387. free_arg(arg);
  1388. return -1;
  1389. }
  1390. *list = arg;
  1391. args++;
  1392. if (type == EVENT_OP) {
  1393. type = process_op(event, arg, &token);
  1394. list = &arg->next;
  1395. continue;
  1396. }
  1397. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  1398. free_token(token);
  1399. *list = arg;
  1400. list = &arg->next;
  1401. continue;
  1402. }
  1403. break;
  1404. } while (type != EVENT_NONE);
  1405. if (type != EVENT_NONE)
  1406. free_token(token);
  1407. return args;
  1408. }
  1409. static int event_read_print(struct event *event)
  1410. {
  1411. enum event_type type;
  1412. char *token;
  1413. int ret;
  1414. if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
  1415. return -1;
  1416. if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
  1417. return -1;
  1418. if (read_expected(EVENT_OP, (char *)":") < 0)
  1419. return -1;
  1420. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  1421. goto fail;
  1422. concat:
  1423. event->print_fmt.format = token;
  1424. event->print_fmt.args = NULL;
  1425. /* ok to have no arg */
  1426. type = read_token_item(&token);
  1427. if (type == EVENT_NONE)
  1428. return 0;
  1429. /* Handle concatination of print lines */
  1430. if (type == EVENT_DQUOTE) {
  1431. char *cat;
  1432. cat = malloc_or_die(strlen(event->print_fmt.format) +
  1433. strlen(token) + 1);
  1434. strcpy(cat, event->print_fmt.format);
  1435. strcat(cat, token);
  1436. free_token(token);
  1437. free_token(event->print_fmt.format);
  1438. event->print_fmt.format = NULL;
  1439. token = cat;
  1440. goto concat;
  1441. }
  1442. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1443. goto fail;
  1444. free_token(token);
  1445. ret = event_read_print_args(event, &event->print_fmt.args);
  1446. if (ret < 0)
  1447. return -1;
  1448. return 0;
  1449. fail:
  1450. free_token(token);
  1451. return -1;
  1452. }
  1453. static struct format_field *
  1454. find_common_field(struct event *event, const char *name)
  1455. {
  1456. struct format_field *format;
  1457. for (format = event->format.common_fields;
  1458. format; format = format->next) {
  1459. if (strcmp(format->name, name) == 0)
  1460. break;
  1461. }
  1462. return format;
  1463. }
  1464. static struct format_field *
  1465. find_field(struct event *event, const char *name)
  1466. {
  1467. struct format_field *format;
  1468. for (format = event->format.fields;
  1469. format; format = format->next) {
  1470. if (strcmp(format->name, name) == 0)
  1471. break;
  1472. }
  1473. return format;
  1474. }
  1475. static struct format_field *
  1476. find_any_field(struct event *event, const char *name)
  1477. {
  1478. struct format_field *format;
  1479. format = find_common_field(event, name);
  1480. if (format)
  1481. return format;
  1482. return find_field(event, name);
  1483. }
  1484. static unsigned long long read_size(void *ptr, int size)
  1485. {
  1486. switch (size) {
  1487. case 1:
  1488. return *(unsigned char *)ptr;
  1489. case 2:
  1490. return data2host2(ptr);
  1491. case 4:
  1492. return data2host4(ptr);
  1493. case 8:
  1494. return data2host8(ptr);
  1495. default:
  1496. /* BUG! */
  1497. return 0;
  1498. }
  1499. }
  1500. unsigned long long
  1501. raw_field_value(struct event *event, const char *name, void *data)
  1502. {
  1503. struct format_field *field;
  1504. field = find_any_field(event, name);
  1505. if (!field)
  1506. return 0ULL;
  1507. return read_size(data + field->offset, field->size);
  1508. }
  1509. void *raw_field_ptr(struct event *event, const char *name, void *data)
  1510. {
  1511. struct format_field *field;
  1512. field = find_any_field(event, name);
  1513. if (!field)
  1514. return NULL;
  1515. return data + field->offset;
  1516. }
  1517. static int get_common_info(const char *type, int *offset, int *size)
  1518. {
  1519. struct event *event;
  1520. struct format_field *field;
  1521. /*
  1522. * All events should have the same common elements.
  1523. * Pick any event to find where the type is;
  1524. */
  1525. if (!event_list)
  1526. die("no event_list!");
  1527. event = event_list;
  1528. field = find_common_field(event, type);
  1529. if (!field)
  1530. die("field '%s' not found", type);
  1531. *offset = field->offset;
  1532. *size = field->size;
  1533. return 0;
  1534. }
  1535. int trace_parse_common_type(void *data)
  1536. {
  1537. static int type_offset;
  1538. static int type_size;
  1539. int ret;
  1540. if (!type_size) {
  1541. ret = get_common_info("common_type",
  1542. &type_offset,
  1543. &type_size);
  1544. if (ret < 0)
  1545. return ret;
  1546. }
  1547. return read_size(data + type_offset, type_size);
  1548. }
  1549. static int parse_common_pid(void *data)
  1550. {
  1551. static int pid_offset;
  1552. static int pid_size;
  1553. int ret;
  1554. if (!pid_size) {
  1555. ret = get_common_info("common_pid",
  1556. &pid_offset,
  1557. &pid_size);
  1558. if (ret < 0)
  1559. return ret;
  1560. }
  1561. return read_size(data + pid_offset, pid_size);
  1562. }
  1563. struct event *trace_find_event(int id)
  1564. {
  1565. struct event *event;
  1566. for (event = event_list; event; event = event->next) {
  1567. if (event->id == id)
  1568. break;
  1569. }
  1570. return event;
  1571. }
  1572. static unsigned long long eval_num_arg(void *data, int size,
  1573. struct event *event, struct print_arg *arg)
  1574. {
  1575. unsigned long long val = 0;
  1576. unsigned long long left, right;
  1577. switch (arg->type) {
  1578. case PRINT_NULL:
  1579. /* ?? */
  1580. return 0;
  1581. case PRINT_ATOM:
  1582. return strtoull(arg->atom.atom, NULL, 0);
  1583. case PRINT_FIELD:
  1584. if (!arg->field.field) {
  1585. arg->field.field = find_any_field(event, arg->field.name);
  1586. if (!arg->field.field)
  1587. die("field %s not found", arg->field.name);
  1588. }
  1589. /* must be a number */
  1590. val = read_size(data + arg->field.field->offset,
  1591. arg->field.field->size);
  1592. break;
  1593. case PRINT_FLAGS:
  1594. case PRINT_SYMBOL:
  1595. break;
  1596. case PRINT_TYPE:
  1597. return eval_num_arg(data, size, event, arg->typecast.item);
  1598. case PRINT_STRING:
  1599. return 0;
  1600. break;
  1601. case PRINT_OP:
  1602. left = eval_num_arg(data, size, event, arg->op.left);
  1603. right = eval_num_arg(data, size, event, arg->op.right);
  1604. switch (arg->op.op[0]) {
  1605. case '|':
  1606. if (arg->op.op[1])
  1607. val = left || right;
  1608. else
  1609. val = left | right;
  1610. break;
  1611. case '&':
  1612. if (arg->op.op[1])
  1613. val = left && right;
  1614. else
  1615. val = left & right;
  1616. break;
  1617. case '<':
  1618. switch (arg->op.op[1]) {
  1619. case 0:
  1620. val = left < right;
  1621. break;
  1622. case '<':
  1623. val = left << right;
  1624. break;
  1625. case '=':
  1626. val = left <= right;
  1627. break;
  1628. default:
  1629. die("unknown op '%s'", arg->op.op);
  1630. }
  1631. break;
  1632. case '>':
  1633. switch (arg->op.op[1]) {
  1634. case 0:
  1635. val = left > right;
  1636. break;
  1637. case '>':
  1638. val = left >> right;
  1639. break;
  1640. case '=':
  1641. val = left >= right;
  1642. break;
  1643. default:
  1644. die("unknown op '%s'", arg->op.op);
  1645. }
  1646. break;
  1647. case '=':
  1648. if (arg->op.op[1] != '=')
  1649. die("unknown op '%s'", arg->op.op);
  1650. val = left == right;
  1651. break;
  1652. default:
  1653. die("unknown op '%s'", arg->op.op);
  1654. }
  1655. break;
  1656. default: /* not sure what to do there */
  1657. return 0;
  1658. }
  1659. return val;
  1660. }
  1661. struct flag {
  1662. const char *name;
  1663. unsigned long long value;
  1664. };
  1665. static const struct flag flags[] = {
  1666. { "HI_SOFTIRQ", 0 },
  1667. { "TIMER_SOFTIRQ", 1 },
  1668. { "NET_TX_SOFTIRQ", 2 },
  1669. { "NET_RX_SOFTIRQ", 3 },
  1670. { "BLOCK_SOFTIRQ", 4 },
  1671. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  1672. { "TASKLET_SOFTIRQ", 6 },
  1673. { "SCHED_SOFTIRQ", 7 },
  1674. { "HRTIMER_SOFTIRQ", 8 },
  1675. { "RCU_SOFTIRQ", 9 },
  1676. { "HRTIMER_NORESTART", 0 },
  1677. { "HRTIMER_RESTART", 1 },
  1678. };
  1679. static unsigned long long eval_flag(const char *flag)
  1680. {
  1681. int i;
  1682. /*
  1683. * Some flags in the format files do not get converted.
  1684. * If the flag is not numeric, see if it is something that
  1685. * we already know about.
  1686. */
  1687. if (isdigit(flag[0]))
  1688. return strtoull(flag, NULL, 0);
  1689. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1690. if (strcmp(flags[i].name, flag) == 0)
  1691. return flags[i].value;
  1692. return 0;
  1693. }
  1694. static void print_str_arg(void *data, int size,
  1695. struct event *event, struct print_arg *arg)
  1696. {
  1697. struct print_flag_sym *flag;
  1698. unsigned long long val, fval;
  1699. char *str;
  1700. int print;
  1701. switch (arg->type) {
  1702. case PRINT_NULL:
  1703. /* ?? */
  1704. return;
  1705. case PRINT_ATOM:
  1706. printf("%s", arg->atom.atom);
  1707. return;
  1708. case PRINT_FIELD:
  1709. if (!arg->field.field) {
  1710. arg->field.field = find_any_field(event, arg->field.name);
  1711. if (!arg->field.field)
  1712. die("field %s not found", arg->field.name);
  1713. }
  1714. str = malloc_or_die(arg->field.field->size + 1);
  1715. memcpy(str, data + arg->field.field->offset,
  1716. arg->field.field->size);
  1717. str[arg->field.field->size] = 0;
  1718. printf("%s", str);
  1719. free(str);
  1720. break;
  1721. case PRINT_FLAGS:
  1722. val = eval_num_arg(data, size, event, arg->flags.field);
  1723. print = 0;
  1724. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1725. fval = eval_flag(flag->value);
  1726. if (!val && !fval) {
  1727. printf("%s", flag->str);
  1728. break;
  1729. }
  1730. if (fval && (val & fval) == fval) {
  1731. if (print && arg->flags.delim)
  1732. printf("%s", arg->flags.delim);
  1733. printf("%s", flag->str);
  1734. print = 1;
  1735. val &= ~fval;
  1736. }
  1737. }
  1738. break;
  1739. case PRINT_SYMBOL:
  1740. val = eval_num_arg(data, size, event, arg->symbol.field);
  1741. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1742. fval = eval_flag(flag->value);
  1743. if (val == fval) {
  1744. printf("%s", flag->str);
  1745. break;
  1746. }
  1747. }
  1748. break;
  1749. case PRINT_TYPE:
  1750. break;
  1751. case PRINT_STRING: {
  1752. int str_offset;
  1753. if (arg->string.offset == -1) {
  1754. struct format_field *f;
  1755. f = find_any_field(event, arg->string.string);
  1756. arg->string.offset = f->offset;
  1757. }
  1758. str_offset = *(int *)(data + arg->string.offset);
  1759. str_offset &= 0xffff;
  1760. printf("%s", ((char *)data) + str_offset);
  1761. break;
  1762. }
  1763. case PRINT_OP:
  1764. /*
  1765. * The only op for string should be ? :
  1766. */
  1767. if (arg->op.op[0] != '?')
  1768. return;
  1769. val = eval_num_arg(data, size, event, arg->op.left);
  1770. if (val)
  1771. print_str_arg(data, size, event, arg->op.right->op.left);
  1772. else
  1773. print_str_arg(data, size, event, arg->op.right->op.right);
  1774. break;
  1775. default:
  1776. /* well... */
  1777. break;
  1778. }
  1779. }
  1780. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1781. {
  1782. static struct format_field *field, *ip_field;
  1783. struct print_arg *args, *arg, **next;
  1784. unsigned long long ip, val;
  1785. char *ptr;
  1786. void *bptr;
  1787. if (!field) {
  1788. field = find_field(event, "buf");
  1789. if (!field)
  1790. die("can't find buffer field for binary printk");
  1791. ip_field = find_field(event, "ip");
  1792. if (!ip_field)
  1793. die("can't find ip field for binary printk");
  1794. }
  1795. ip = read_size(data + ip_field->offset, ip_field->size);
  1796. /*
  1797. * The first arg is the IP pointer.
  1798. */
  1799. args = malloc_or_die(sizeof(*args));
  1800. arg = args;
  1801. arg->next = NULL;
  1802. next = &arg->next;
  1803. arg->type = PRINT_ATOM;
  1804. arg->atom.atom = malloc_or_die(32);
  1805. sprintf(arg->atom.atom, "%lld", ip);
  1806. /* skip the first "%pf : " */
  1807. for (ptr = fmt + 6, bptr = data + field->offset;
  1808. bptr < data + size && *ptr; ptr++) {
  1809. int ls = 0;
  1810. if (*ptr == '%') {
  1811. process_again:
  1812. ptr++;
  1813. switch (*ptr) {
  1814. case '%':
  1815. break;
  1816. case 'l':
  1817. ls++;
  1818. goto process_again;
  1819. case 'L':
  1820. ls = 2;
  1821. goto process_again;
  1822. case '0' ... '9':
  1823. goto process_again;
  1824. case 'p':
  1825. ls = 1;
  1826. /* fall through */
  1827. case 'd':
  1828. case 'u':
  1829. case 'x':
  1830. case 'i':
  1831. bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
  1832. ~(long_size - 1));
  1833. switch (ls) {
  1834. case 0:
  1835. case 1:
  1836. ls = long_size;
  1837. break;
  1838. case 2:
  1839. ls = 8;
  1840. default:
  1841. break;
  1842. }
  1843. val = read_size(bptr, ls);
  1844. bptr += ls;
  1845. arg = malloc_or_die(sizeof(*arg));
  1846. arg->next = NULL;
  1847. arg->type = PRINT_ATOM;
  1848. arg->atom.atom = malloc_or_die(32);
  1849. sprintf(arg->atom.atom, "%lld", val);
  1850. *next = arg;
  1851. next = &arg->next;
  1852. break;
  1853. case 's':
  1854. arg = malloc_or_die(sizeof(*arg));
  1855. arg->next = NULL;
  1856. arg->type = PRINT_STRING;
  1857. arg->string.string = strdup(bptr);
  1858. bptr += strlen(bptr) + 1;
  1859. *next = arg;
  1860. next = &arg->next;
  1861. default:
  1862. break;
  1863. }
  1864. }
  1865. }
  1866. return args;
  1867. }
  1868. static void free_args(struct print_arg *args)
  1869. {
  1870. struct print_arg *next;
  1871. while (args) {
  1872. next = args->next;
  1873. if (args->type == PRINT_ATOM)
  1874. free(args->atom.atom);
  1875. else
  1876. free(args->string.string);
  1877. free(args);
  1878. args = next;
  1879. }
  1880. }
  1881. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1882. {
  1883. unsigned long long addr;
  1884. static struct format_field *field;
  1885. struct printk_map *printk;
  1886. char *format;
  1887. char *p;
  1888. if (!field) {
  1889. field = find_field(event, "fmt");
  1890. if (!field)
  1891. die("can't find format field for binary printk");
  1892. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1893. }
  1894. addr = read_size(data + field->offset, field->size);
  1895. printk = find_printk(addr);
  1896. if (!printk) {
  1897. format = malloc_or_die(45);
  1898. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  1899. addr);
  1900. return format;
  1901. }
  1902. p = printk->printk;
  1903. /* Remove any quotes. */
  1904. if (*p == '"')
  1905. p++;
  1906. format = malloc_or_die(strlen(p) + 10);
  1907. sprintf(format, "%s : %s", "%pf", p);
  1908. /* remove ending quotes and new line since we will add one too */
  1909. p = format + strlen(format) - 1;
  1910. if (*p == '"')
  1911. *p = 0;
  1912. p -= 2;
  1913. if (strcmp(p, "\\n") == 0)
  1914. *p = 0;
  1915. return format;
  1916. }
  1917. static void pretty_print(void *data, int size, struct event *event)
  1918. {
  1919. struct print_fmt *print_fmt = &event->print_fmt;
  1920. struct print_arg *arg = print_fmt->args;
  1921. struct print_arg *args = NULL;
  1922. const char *ptr = print_fmt->format;
  1923. unsigned long long val;
  1924. struct func_map *func;
  1925. const char *saveptr;
  1926. char *bprint_fmt = NULL;
  1927. char format[32];
  1928. int show_func;
  1929. int len;
  1930. int ls;
  1931. if (event->flags & EVENT_FL_ISFUNC)
  1932. ptr = " %pF <-- %pF";
  1933. if (event->flags & EVENT_FL_ISBPRINT) {
  1934. bprint_fmt = get_bprint_format(data, size, event);
  1935. args = make_bprint_args(bprint_fmt, data, size, event);
  1936. arg = args;
  1937. ptr = bprint_fmt;
  1938. }
  1939. for (; *ptr; ptr++) {
  1940. ls = 0;
  1941. if (*ptr == '\\') {
  1942. ptr++;
  1943. switch (*ptr) {
  1944. case 'n':
  1945. printf("\n");
  1946. break;
  1947. case 't':
  1948. printf("\t");
  1949. break;
  1950. case 'r':
  1951. printf("\r");
  1952. break;
  1953. case '\\':
  1954. printf("\\");
  1955. break;
  1956. default:
  1957. printf("%c", *ptr);
  1958. break;
  1959. }
  1960. } else if (*ptr == '%') {
  1961. saveptr = ptr;
  1962. show_func = 0;
  1963. cont_process:
  1964. ptr++;
  1965. switch (*ptr) {
  1966. case '%':
  1967. printf("%%");
  1968. break;
  1969. case 'l':
  1970. ls++;
  1971. goto cont_process;
  1972. case 'L':
  1973. ls = 2;
  1974. goto cont_process;
  1975. case 'z':
  1976. case 'Z':
  1977. case '0' ... '9':
  1978. goto cont_process;
  1979. case 'p':
  1980. if (long_size == 4)
  1981. ls = 1;
  1982. else
  1983. ls = 2;
  1984. if (*(ptr+1) == 'F' ||
  1985. *(ptr+1) == 'f') {
  1986. ptr++;
  1987. show_func = *ptr;
  1988. }
  1989. /* fall through */
  1990. case 'd':
  1991. case 'i':
  1992. case 'x':
  1993. case 'X':
  1994. case 'u':
  1995. if (!arg)
  1996. die("no argument match");
  1997. len = ((unsigned long)ptr + 1) -
  1998. (unsigned long)saveptr;
  1999. /* should never happen */
  2000. if (len > 32)
  2001. die("bad format!");
  2002. memcpy(format, saveptr, len);
  2003. format[len] = 0;
  2004. val = eval_num_arg(data, size, event, arg);
  2005. arg = arg->next;
  2006. if (show_func) {
  2007. func = find_func(val);
  2008. if (func) {
  2009. printf("%s", func->func);
  2010. if (show_func == 'F')
  2011. printf("+0x%llx",
  2012. val - func->addr);
  2013. break;
  2014. }
  2015. }
  2016. switch (ls) {
  2017. case 0:
  2018. printf(format, (int)val);
  2019. break;
  2020. case 1:
  2021. printf(format, (long)val);
  2022. break;
  2023. case 2:
  2024. printf(format, (long long)val);
  2025. break;
  2026. default:
  2027. die("bad count (%d)", ls);
  2028. }
  2029. break;
  2030. case 's':
  2031. if (!arg)
  2032. die("no matching argument");
  2033. print_str_arg(data, size, event, arg);
  2034. arg = arg->next;
  2035. break;
  2036. default:
  2037. printf(">%c<", *ptr);
  2038. }
  2039. } else
  2040. printf("%c", *ptr);
  2041. }
  2042. if (args) {
  2043. free_args(args);
  2044. free(bprint_fmt);
  2045. }
  2046. }
  2047. static inline int log10_cpu(int nb)
  2048. {
  2049. if (nb / 100)
  2050. return 3;
  2051. if (nb / 10)
  2052. return 2;
  2053. return 1;
  2054. }
  2055. /* taken from Linux, written by Frederic Weisbecker */
  2056. static void print_graph_cpu(int cpu)
  2057. {
  2058. int i;
  2059. int log10_this = log10_cpu(cpu);
  2060. int log10_all = log10_cpu(cpus);
  2061. /*
  2062. * Start with a space character - to make it stand out
  2063. * to the right a bit when trace output is pasted into
  2064. * email:
  2065. */
  2066. printf(" ");
  2067. /*
  2068. * Tricky - we space the CPU field according to the max
  2069. * number of online CPUs. On a 2-cpu system it would take
  2070. * a maximum of 1 digit - on a 128 cpu system it would
  2071. * take up to 3 digits:
  2072. */
  2073. for (i = 0; i < log10_all - log10_this; i++)
  2074. printf(" ");
  2075. printf("%d) ", cpu);
  2076. }
  2077. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  2078. #define TRACE_GRAPH_INDENT 2
  2079. static void print_graph_proc(int pid, const char *comm)
  2080. {
  2081. /* sign + log10(MAX_INT) + '\0' */
  2082. char pid_str[11];
  2083. int spaces = 0;
  2084. int len;
  2085. int i;
  2086. sprintf(pid_str, "%d", pid);
  2087. /* 1 stands for the "-" character */
  2088. len = strlen(comm) + strlen(pid_str) + 1;
  2089. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  2090. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  2091. /* First spaces to align center */
  2092. for (i = 0; i < spaces / 2; i++)
  2093. printf(" ");
  2094. printf("%s-%s", comm, pid_str);
  2095. /* Last spaces to align center */
  2096. for (i = 0; i < spaces - (spaces / 2); i++)
  2097. printf(" ");
  2098. }
  2099. static struct record *
  2100. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2101. struct record *next)
  2102. {
  2103. struct format_field *field;
  2104. struct event *event;
  2105. unsigned long val;
  2106. int type;
  2107. int pid;
  2108. type = trace_parse_common_type(next->data);
  2109. event = trace_find_event(type);
  2110. if (!event)
  2111. return NULL;
  2112. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2113. return NULL;
  2114. pid = parse_common_pid(next->data);
  2115. field = find_field(event, "func");
  2116. if (!field)
  2117. die("function return does not have field func");
  2118. val = read_size(next->data + field->offset, field->size);
  2119. if (cur_pid != pid || cur_func != val)
  2120. return NULL;
  2121. /* this is a leaf, now advance the iterator */
  2122. return trace_read_data(cpu);
  2123. }
  2124. /* Signal a overhead of time execution to the output */
  2125. static void print_graph_overhead(unsigned long long duration)
  2126. {
  2127. /* Non nested entry or return */
  2128. if (duration == ~0ULL)
  2129. return (void)printf(" ");
  2130. /* Duration exceeded 100 msecs */
  2131. if (duration > 100000ULL)
  2132. return (void)printf("! ");
  2133. /* Duration exceeded 10 msecs */
  2134. if (duration > 10000ULL)
  2135. return (void)printf("+ ");
  2136. printf(" ");
  2137. }
  2138. static void print_graph_duration(unsigned long long duration)
  2139. {
  2140. unsigned long usecs = duration / 1000;
  2141. unsigned long nsecs_rem = duration % 1000;
  2142. /* log10(ULONG_MAX) + '\0' */
  2143. char msecs_str[21];
  2144. char nsecs_str[5];
  2145. int len;
  2146. int i;
  2147. sprintf(msecs_str, "%lu", usecs);
  2148. /* Print msecs */
  2149. len = printf("%lu", usecs);
  2150. /* Print nsecs (we don't want to exceed 7 numbers) */
  2151. if (len < 7) {
  2152. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2153. len += printf(".%s", nsecs_str);
  2154. }
  2155. printf(" us ");
  2156. /* Print remaining spaces to fit the row's width */
  2157. for (i = len; i < 7; i++)
  2158. printf(" ");
  2159. printf("| ");
  2160. }
  2161. static void
  2162. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2163. {
  2164. unsigned long long rettime, calltime;
  2165. unsigned long long duration, depth;
  2166. unsigned long long val;
  2167. struct format_field *field;
  2168. struct func_map *func;
  2169. struct event *ret_event;
  2170. int type;
  2171. int i;
  2172. type = trace_parse_common_type(ret_rec->data);
  2173. ret_event = trace_find_event(type);
  2174. field = find_field(ret_event, "rettime");
  2175. if (!field)
  2176. die("can't find rettime in return graph");
  2177. rettime = read_size(ret_rec->data + field->offset, field->size);
  2178. field = find_field(ret_event, "calltime");
  2179. if (!field)
  2180. die("can't find rettime in return graph");
  2181. calltime = read_size(ret_rec->data + field->offset, field->size);
  2182. duration = rettime - calltime;
  2183. /* Overhead */
  2184. print_graph_overhead(duration);
  2185. /* Duration */
  2186. print_graph_duration(duration);
  2187. field = find_field(event, "depth");
  2188. if (!field)
  2189. die("can't find depth in entry graph");
  2190. depth = read_size(data + field->offset, field->size);
  2191. /* Function */
  2192. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2193. printf(" ");
  2194. field = find_field(event, "func");
  2195. if (!field)
  2196. die("can't find func in entry graph");
  2197. val = read_size(data + field->offset, field->size);
  2198. func = find_func(val);
  2199. if (func)
  2200. printf("%s();", func->func);
  2201. else
  2202. printf("%llx();", val);
  2203. }
  2204. static void print_graph_nested(struct event *event, void *data)
  2205. {
  2206. struct format_field *field;
  2207. unsigned long long depth;
  2208. unsigned long long val;
  2209. struct func_map *func;
  2210. int i;
  2211. /* No overhead */
  2212. print_graph_overhead(-1);
  2213. /* No time */
  2214. printf(" | ");
  2215. field = find_field(event, "depth");
  2216. if (!field)
  2217. die("can't find depth in entry graph");
  2218. depth = read_size(data + field->offset, field->size);
  2219. /* Function */
  2220. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2221. printf(" ");
  2222. field = find_field(event, "func");
  2223. if (!field)
  2224. die("can't find func in entry graph");
  2225. val = read_size(data + field->offset, field->size);
  2226. func = find_func(val);
  2227. if (func)
  2228. printf("%s() {", func->func);
  2229. else
  2230. printf("%llx() {", val);
  2231. }
  2232. static void
  2233. pretty_print_func_ent(void *data, int size, struct event *event,
  2234. int cpu, int pid, const char *comm,
  2235. unsigned long secs, unsigned long usecs)
  2236. {
  2237. struct format_field *field;
  2238. struct record *rec;
  2239. void *copy_data;
  2240. unsigned long val;
  2241. printf("%5lu.%06lu | ", secs, usecs);
  2242. print_graph_cpu(cpu);
  2243. print_graph_proc(pid, comm);
  2244. printf(" | ");
  2245. field = find_field(event, "func");
  2246. if (!field)
  2247. die("function entry does not have func field");
  2248. val = read_size(data + field->offset, field->size);
  2249. /*
  2250. * peek_data may unmap the data pointer. Copy it first.
  2251. */
  2252. copy_data = malloc_or_die(size);
  2253. memcpy(copy_data, data, size);
  2254. data = copy_data;
  2255. rec = trace_peek_data(cpu);
  2256. if (rec) {
  2257. rec = get_return_for_leaf(cpu, pid, val, rec);
  2258. if (rec) {
  2259. print_graph_entry_leaf(event, data, rec);
  2260. goto out_free;
  2261. }
  2262. }
  2263. print_graph_nested(event, data);
  2264. out_free:
  2265. free(data);
  2266. }
  2267. static void
  2268. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2269. int cpu, int pid, const char *comm,
  2270. unsigned long secs, unsigned long usecs)
  2271. {
  2272. unsigned long long rettime, calltime;
  2273. unsigned long long duration, depth;
  2274. struct format_field *field;
  2275. int i;
  2276. printf("%5lu.%06lu | ", secs, usecs);
  2277. print_graph_cpu(cpu);
  2278. print_graph_proc(pid, comm);
  2279. printf(" | ");
  2280. field = find_field(event, "rettime");
  2281. if (!field)
  2282. die("can't find rettime in return graph");
  2283. rettime = read_size(data + field->offset, field->size);
  2284. field = find_field(event, "calltime");
  2285. if (!field)
  2286. die("can't find calltime in return graph");
  2287. calltime = read_size(data + field->offset, field->size);
  2288. duration = rettime - calltime;
  2289. /* Overhead */
  2290. print_graph_overhead(duration);
  2291. /* Duration */
  2292. print_graph_duration(duration);
  2293. field = find_field(event, "depth");
  2294. if (!field)
  2295. die("can't find depth in entry graph");
  2296. depth = read_size(data + field->offset, field->size);
  2297. /* Function */
  2298. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2299. printf(" ");
  2300. printf("}");
  2301. }
  2302. static void
  2303. pretty_print_func_graph(void *data, int size, struct event *event,
  2304. int cpu, int pid, const char *comm,
  2305. unsigned long secs, unsigned long usecs)
  2306. {
  2307. if (event->flags & EVENT_FL_ISFUNCENT)
  2308. pretty_print_func_ent(data, size, event,
  2309. cpu, pid, comm, secs, usecs);
  2310. else if (event->flags & EVENT_FL_ISFUNCRET)
  2311. pretty_print_func_ret(data, size, event,
  2312. cpu, pid, comm, secs, usecs);
  2313. printf("\n");
  2314. }
  2315. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2316. char *comm)
  2317. {
  2318. struct event *event;
  2319. unsigned long secs;
  2320. unsigned long usecs;
  2321. int type;
  2322. int pid;
  2323. secs = nsecs / NSECS_PER_SEC;
  2324. nsecs -= secs * NSECS_PER_SEC;
  2325. usecs = nsecs / NSECS_PER_USEC;
  2326. type = trace_parse_common_type(data);
  2327. event = trace_find_event(type);
  2328. if (!event) {
  2329. printf("ug! no event found for type %d\n", type);
  2330. return;
  2331. }
  2332. pid = parse_common_pid(data);
  2333. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2334. return pretty_print_func_graph(data, size, event, cpu,
  2335. pid, comm, secs, usecs);
  2336. printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
  2337. comm, pid, cpu,
  2338. secs, nsecs, event->name);
  2339. pretty_print(data, size, event);
  2340. printf("\n");
  2341. }
  2342. static void print_fields(struct print_flag_sym *field)
  2343. {
  2344. printf("{ %s, %s }", field->value, field->str);
  2345. if (field->next) {
  2346. printf(", ");
  2347. print_fields(field->next);
  2348. }
  2349. }
  2350. static void print_args(struct print_arg *args)
  2351. {
  2352. int print_paren = 1;
  2353. switch (args->type) {
  2354. case PRINT_NULL:
  2355. printf("null");
  2356. break;
  2357. case PRINT_ATOM:
  2358. printf("%s", args->atom.atom);
  2359. break;
  2360. case PRINT_FIELD:
  2361. printf("REC->%s", args->field.name);
  2362. break;
  2363. case PRINT_FLAGS:
  2364. printf("__print_flags(");
  2365. print_args(args->flags.field);
  2366. printf(", %s, ", args->flags.delim);
  2367. print_fields(args->flags.flags);
  2368. printf(")");
  2369. break;
  2370. case PRINT_SYMBOL:
  2371. printf("__print_symbolic(");
  2372. print_args(args->symbol.field);
  2373. printf(", ");
  2374. print_fields(args->symbol.symbols);
  2375. printf(")");
  2376. break;
  2377. case PRINT_STRING:
  2378. printf("__get_str(%s)", args->string.string);
  2379. break;
  2380. case PRINT_TYPE:
  2381. printf("(%s)", args->typecast.type);
  2382. print_args(args->typecast.item);
  2383. break;
  2384. case PRINT_OP:
  2385. if (strcmp(args->op.op, ":") == 0)
  2386. print_paren = 0;
  2387. if (print_paren)
  2388. printf("(");
  2389. print_args(args->op.left);
  2390. printf(" %s ", args->op.op);
  2391. print_args(args->op.right);
  2392. if (print_paren)
  2393. printf(")");
  2394. break;
  2395. default:
  2396. /* we should warn... */
  2397. return;
  2398. }
  2399. if (args->next) {
  2400. printf("\n");
  2401. print_args(args->next);
  2402. }
  2403. }
  2404. static void parse_header_field(char *type,
  2405. int *offset, int *size)
  2406. {
  2407. char *token;
  2408. if (read_expected(EVENT_ITEM, (char *)"field") < 0)
  2409. return;
  2410. if (read_expected(EVENT_OP, (char *)":") < 0)
  2411. return;
  2412. /* type */
  2413. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2414. return;
  2415. free_token(token);
  2416. if (read_expected(EVENT_ITEM, type) < 0)
  2417. return;
  2418. if (read_expected(EVENT_OP, (char *)";") < 0)
  2419. return;
  2420. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  2421. return;
  2422. if (read_expected(EVENT_OP, (char *)":") < 0)
  2423. return;
  2424. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2425. return;
  2426. *offset = atoi(token);
  2427. free_token(token);
  2428. if (read_expected(EVENT_OP, (char *)";") < 0)
  2429. return;
  2430. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  2431. return;
  2432. if (read_expected(EVENT_OP, (char *)":") < 0)
  2433. return;
  2434. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2435. return;
  2436. *size = atoi(token);
  2437. free_token(token);
  2438. if (read_expected(EVENT_OP, (char *)";") < 0)
  2439. return;
  2440. if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
  2441. return;
  2442. if (read_expected(EVENT_OP, (char *)":") < 0)
  2443. return;
  2444. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2445. return;
  2446. free_token(token);
  2447. if (read_expected(EVENT_OP, (char *)";") < 0)
  2448. return;
  2449. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  2450. return;
  2451. free_token(token);
  2452. }
  2453. int parse_header_page(char *buf, unsigned long size)
  2454. {
  2455. init_input_buf(buf, size);
  2456. parse_header_field((char *)"timestamp", &header_page_ts_offset,
  2457. &header_page_ts_size);
  2458. parse_header_field((char *)"commit", &header_page_size_offset,
  2459. &header_page_size_size);
  2460. parse_header_field((char *)"data", &header_page_data_offset,
  2461. &header_page_data_size);
  2462. return 0;
  2463. }
  2464. int parse_ftrace_file(char *buf, unsigned long size)
  2465. {
  2466. struct format_field *field;
  2467. struct print_arg *arg, **list;
  2468. struct event *event;
  2469. int ret;
  2470. init_input_buf(buf, size);
  2471. event = alloc_event();
  2472. if (!event)
  2473. return -ENOMEM;
  2474. event->flags |= EVENT_FL_ISFTRACE;
  2475. event->name = event_read_name();
  2476. if (!event->name)
  2477. die("failed to read ftrace event name");
  2478. if (strcmp(event->name, "function") == 0)
  2479. event->flags |= EVENT_FL_ISFUNC;
  2480. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2481. event->flags |= EVENT_FL_ISFUNCENT;
  2482. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2483. event->flags |= EVENT_FL_ISFUNCRET;
  2484. else if (strcmp(event->name, "bprint") == 0)
  2485. event->flags |= EVENT_FL_ISBPRINT;
  2486. event->id = event_read_id();
  2487. if (event->id < 0)
  2488. die("failed to read ftrace event id");
  2489. add_event(event);
  2490. ret = event_read_format(event);
  2491. if (ret < 0)
  2492. die("failed to read ftrace event format");
  2493. ret = event_read_print(event);
  2494. if (ret < 0)
  2495. die("failed to read ftrace event print fmt");
  2496. /*
  2497. * The arguments for ftrace files are parsed by the fields.
  2498. * Set up the fields as their arguments.
  2499. */
  2500. list = &event->print_fmt.args;
  2501. for (field = event->format.fields; field; field = field->next) {
  2502. arg = malloc_or_die(sizeof(*arg));
  2503. memset(arg, 0, sizeof(*arg));
  2504. *list = arg;
  2505. list = &arg->next;
  2506. arg->type = PRINT_FIELD;
  2507. arg->field.name = field->name;
  2508. arg->field.field = field;
  2509. }
  2510. return 0;
  2511. }
  2512. int parse_event_file(char *buf, unsigned long size, char *sys)
  2513. {
  2514. struct event *event;
  2515. int ret;
  2516. init_input_buf(buf, size);
  2517. event = alloc_event();
  2518. if (!event)
  2519. return -ENOMEM;
  2520. event->name = event_read_name();
  2521. if (!event->name)
  2522. die("failed to read event name");
  2523. event->id = event_read_id();
  2524. if (event->id < 0)
  2525. die("failed to read event id");
  2526. ret = event_read_format(event);
  2527. if (ret < 0)
  2528. die("failed to read event format");
  2529. ret = event_read_print(event);
  2530. if (ret < 0)
  2531. die("failed to read event print fmt");
  2532. event->system = strdup(sys);
  2533. #define PRINT_ARGS 0
  2534. if (PRINT_ARGS && event->print_fmt.args)
  2535. print_args(event->print_fmt.args);
  2536. add_event(event);
  2537. return 0;
  2538. }
  2539. void parse_set_info(int nr_cpus, int long_sz)
  2540. {
  2541. cpus = nr_cpus;
  2542. long_size = long_sz;
  2543. }