trace-event-parse.c 60 KB

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