trace-event-parse.c 62 KB

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