trace-event-parse.c 62 KB

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