trace-event-parse.c 59 KB

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