trace-event-parse.c 62 KB

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