trace-event-parse.c 57 KB

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