trace-event-parse.c 60 KB

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