trace-event-parse.c 59 KB

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