trace-event-parse.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155
  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. case '+':
  1175. left = arg_num_eval(arg->op.left);
  1176. right = arg_num_eval(arg->op.right);
  1177. val = left + right;
  1178. break;
  1179. default:
  1180. die("unknown op '%s'", arg->op.op);
  1181. }
  1182. break;
  1183. case PRINT_NULL:
  1184. case PRINT_FIELD ... PRINT_SYMBOL:
  1185. case PRINT_STRING:
  1186. default:
  1187. die("invalid eval type %d", arg->type);
  1188. }
  1189. return val;
  1190. }
  1191. static char *arg_eval (struct print_arg *arg)
  1192. {
  1193. long long val;
  1194. static char buf[20];
  1195. switch (arg->type) {
  1196. case PRINT_ATOM:
  1197. return arg->atom.atom;
  1198. case PRINT_TYPE:
  1199. return arg_eval(arg->typecast.item);
  1200. case PRINT_OP:
  1201. val = arg_num_eval(arg);
  1202. sprintf(buf, "%lld", val);
  1203. return buf;
  1204. case PRINT_NULL:
  1205. case PRINT_FIELD ... PRINT_SYMBOL:
  1206. case PRINT_STRING:
  1207. default:
  1208. die("invalid eval type %d", arg->type);
  1209. break;
  1210. }
  1211. return NULL;
  1212. }
  1213. static enum event_type
  1214. process_fields(struct event *event, struct print_flag_sym **list, char **tok)
  1215. {
  1216. enum event_type type;
  1217. struct print_arg *arg = NULL;
  1218. struct print_flag_sym *field;
  1219. char *token = NULL;
  1220. char *value;
  1221. do {
  1222. free_token(token);
  1223. type = read_token_item(&token);
  1224. if (test_type_token(type, token, EVENT_OP, "{"))
  1225. break;
  1226. arg = malloc_or_die(sizeof(*arg));
  1227. free_token(token);
  1228. type = process_arg(event, arg, &token);
  1229. if (type == EVENT_OP)
  1230. type = process_op(event, arg, &token);
  1231. if (type == EVENT_ERROR)
  1232. goto out_free;
  1233. if (test_type_token(type, token, EVENT_DELIM, ","))
  1234. goto out_free;
  1235. field = malloc_or_die(sizeof(*field));
  1236. memset(field, 0, sizeof(*field));
  1237. value = arg_eval(arg);
  1238. field->value = strdup(value);
  1239. free_token(token);
  1240. type = process_arg(event, arg, &token);
  1241. if (test_type_token(type, token, EVENT_OP, "}"))
  1242. goto out_free;
  1243. value = arg_eval(arg);
  1244. field->str = strdup(value);
  1245. free_arg(arg);
  1246. arg = NULL;
  1247. *list = field;
  1248. list = &field->next;
  1249. free_token(token);
  1250. type = read_token_item(&token);
  1251. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1252. *tok = token;
  1253. return type;
  1254. out_free:
  1255. free_arg(arg);
  1256. free_token(token);
  1257. return EVENT_ERROR;
  1258. }
  1259. static enum event_type
  1260. process_flags(struct event *event, struct print_arg *arg, char **tok)
  1261. {
  1262. struct print_arg *field;
  1263. enum event_type type;
  1264. char *token;
  1265. memset(arg, 0, sizeof(*arg));
  1266. arg->type = PRINT_FLAGS;
  1267. if (read_expected_item(EVENT_DELIM, "(") < 0)
  1268. return EVENT_ERROR;
  1269. field = malloc_or_die(sizeof(*field));
  1270. type = process_arg(event, field, &token);
  1271. while (type == EVENT_OP)
  1272. type = process_op(event, field, &token);
  1273. if (test_type_token(type, token, EVENT_DELIM, ","))
  1274. goto out_free;
  1275. arg->flags.field = field;
  1276. type = read_token_item(&token);
  1277. if (event_item_type(type)) {
  1278. arg->flags.delim = token;
  1279. type = read_token_item(&token);
  1280. }
  1281. if (test_type_token(type, token, EVENT_DELIM, ","))
  1282. goto out_free;
  1283. type = process_fields(event, &arg->flags.flags, &token);
  1284. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1285. goto out_free;
  1286. free_token(token);
  1287. type = read_token_item(tok);
  1288. return type;
  1289. out_free:
  1290. free_token(token);
  1291. return EVENT_ERROR;
  1292. }
  1293. static enum event_type
  1294. process_symbols(struct event *event, struct print_arg *arg, char **tok)
  1295. {
  1296. struct print_arg *field;
  1297. enum event_type type;
  1298. char *token;
  1299. memset(arg, 0, sizeof(*arg));
  1300. arg->type = PRINT_SYMBOL;
  1301. if (read_expected_item(EVENT_DELIM, "(") < 0)
  1302. return EVENT_ERROR;
  1303. field = malloc_or_die(sizeof(*field));
  1304. type = process_arg(event, field, &token);
  1305. if (test_type_token(type, token, EVENT_DELIM, ","))
  1306. goto out_free;
  1307. arg->symbol.field = field;
  1308. type = process_fields(event, &arg->symbol.symbols, &token);
  1309. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1310. goto out_free;
  1311. free_token(token);
  1312. type = read_token_item(tok);
  1313. return type;
  1314. out_free:
  1315. free_token(token);
  1316. return EVENT_ERROR;
  1317. }
  1318. static enum event_type
  1319. process_paren(struct event *event, struct print_arg *arg, char **tok)
  1320. {
  1321. struct print_arg *item_arg;
  1322. enum event_type type;
  1323. char *token;
  1324. type = process_arg(event, arg, &token);
  1325. if (type == EVENT_ERROR)
  1326. return EVENT_ERROR;
  1327. if (type == EVENT_OP)
  1328. type = process_op(event, arg, &token);
  1329. if (type == EVENT_ERROR)
  1330. return EVENT_ERROR;
  1331. if (test_type_token(type, token, EVENT_DELIM, ")")) {
  1332. free_token(token);
  1333. return EVENT_ERROR;
  1334. }
  1335. free_token(token);
  1336. type = read_token_item(&token);
  1337. /*
  1338. * If the next token is an item or another open paren, then
  1339. * this was a typecast.
  1340. */
  1341. if (event_item_type(type) ||
  1342. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1343. /* make this a typecast and contine */
  1344. /* prevous must be an atom */
  1345. if (arg->type != PRINT_ATOM)
  1346. die("previous needed to be PRINT_ATOM");
  1347. item_arg = malloc_or_die(sizeof(*item_arg));
  1348. arg->type = PRINT_TYPE;
  1349. arg->typecast.type = arg->atom.atom;
  1350. arg->typecast.item = item_arg;
  1351. type = process_arg_token(event, item_arg, &token, type);
  1352. }
  1353. *tok = token;
  1354. return type;
  1355. }
  1356. static enum event_type
  1357. process_str(struct event *event __unused, struct print_arg *arg, char **tok)
  1358. {
  1359. enum event_type type;
  1360. char *token;
  1361. if (read_expected(EVENT_DELIM, "(") < 0)
  1362. return EVENT_ERROR;
  1363. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1364. goto fail;
  1365. arg->type = PRINT_STRING;
  1366. arg->string.string = token;
  1367. arg->string.offset = -1;
  1368. if (read_expected(EVENT_DELIM, ")") < 0)
  1369. return EVENT_ERROR;
  1370. type = read_token(&token);
  1371. *tok = token;
  1372. return type;
  1373. fail:
  1374. free_token(token);
  1375. return EVENT_ERROR;
  1376. }
  1377. enum event_type
  1378. process_arg_token(struct event *event, struct print_arg *arg,
  1379. char **tok, enum event_type type)
  1380. {
  1381. char *token;
  1382. char *atom;
  1383. token = *tok;
  1384. switch (type) {
  1385. case EVENT_ITEM:
  1386. if (strcmp(token, "REC") == 0) {
  1387. free_token(token);
  1388. type = process_entry(event, arg, &token);
  1389. } else if (strcmp(token, "__print_flags") == 0) {
  1390. free_token(token);
  1391. is_flag_field = 1;
  1392. type = process_flags(event, arg, &token);
  1393. } else if (strcmp(token, "__print_symbolic") == 0) {
  1394. free_token(token);
  1395. is_symbolic_field = 1;
  1396. type = process_symbols(event, arg, &token);
  1397. } else if (strcmp(token, "__get_str") == 0) {
  1398. free_token(token);
  1399. type = process_str(event, arg, &token);
  1400. } else {
  1401. atom = token;
  1402. /* test the next token */
  1403. type = read_token_item(&token);
  1404. /* atoms can be more than one token long */
  1405. while (type == EVENT_ITEM) {
  1406. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  1407. strcat(atom, " ");
  1408. strcat(atom, token);
  1409. free_token(token);
  1410. type = read_token_item(&token);
  1411. }
  1412. /* todo, test for function */
  1413. arg->type = PRINT_ATOM;
  1414. arg->atom.atom = atom;
  1415. }
  1416. break;
  1417. case EVENT_DQUOTE:
  1418. case EVENT_SQUOTE:
  1419. arg->type = PRINT_ATOM;
  1420. arg->atom.atom = token;
  1421. type = read_token_item(&token);
  1422. break;
  1423. case EVENT_DELIM:
  1424. if (strcmp(token, "(") == 0) {
  1425. free_token(token);
  1426. type = process_paren(event, arg, &token);
  1427. break;
  1428. }
  1429. case EVENT_OP:
  1430. /* handle single ops */
  1431. arg->type = PRINT_OP;
  1432. arg->op.op = token;
  1433. arg->op.left = NULL;
  1434. type = process_op(event, arg, &token);
  1435. break;
  1436. case EVENT_ERROR ... EVENT_NEWLINE:
  1437. default:
  1438. die("unexpected type %d", type);
  1439. }
  1440. *tok = token;
  1441. return type;
  1442. }
  1443. static int event_read_print_args(struct event *event, struct print_arg **list)
  1444. {
  1445. enum event_type type = EVENT_ERROR;
  1446. struct print_arg *arg;
  1447. char *token;
  1448. int args = 0;
  1449. do {
  1450. if (type == EVENT_NEWLINE) {
  1451. free_token(token);
  1452. type = read_token_item(&token);
  1453. continue;
  1454. }
  1455. arg = malloc_or_die(sizeof(*arg));
  1456. memset(arg, 0, sizeof(*arg));
  1457. type = process_arg(event, arg, &token);
  1458. if (type == EVENT_ERROR) {
  1459. free_arg(arg);
  1460. return -1;
  1461. }
  1462. *list = arg;
  1463. args++;
  1464. if (type == EVENT_OP) {
  1465. type = process_op(event, arg, &token);
  1466. list = &arg->next;
  1467. continue;
  1468. }
  1469. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  1470. free_token(token);
  1471. *list = arg;
  1472. list = &arg->next;
  1473. continue;
  1474. }
  1475. break;
  1476. } while (type != EVENT_NONE);
  1477. if (type != EVENT_NONE)
  1478. free_token(token);
  1479. return args;
  1480. }
  1481. static int event_read_print(struct event *event)
  1482. {
  1483. enum event_type type;
  1484. char *token;
  1485. int ret;
  1486. if (read_expected_item(EVENT_ITEM, "print") < 0)
  1487. return -1;
  1488. if (read_expected(EVENT_ITEM, "fmt") < 0)
  1489. return -1;
  1490. if (read_expected(EVENT_OP, ":") < 0)
  1491. return -1;
  1492. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  1493. goto fail;
  1494. concat:
  1495. event->print_fmt.format = token;
  1496. event->print_fmt.args = NULL;
  1497. /* ok to have no arg */
  1498. type = read_token_item(&token);
  1499. if (type == EVENT_NONE)
  1500. return 0;
  1501. /* Handle concatination of print lines */
  1502. if (type == EVENT_DQUOTE) {
  1503. char *cat;
  1504. cat = malloc_or_die(strlen(event->print_fmt.format) +
  1505. strlen(token) + 1);
  1506. strcpy(cat, event->print_fmt.format);
  1507. strcat(cat, token);
  1508. free_token(token);
  1509. free_token(event->print_fmt.format);
  1510. event->print_fmt.format = NULL;
  1511. token = cat;
  1512. goto concat;
  1513. }
  1514. if (test_type_token(type, token, EVENT_DELIM, ","))
  1515. goto fail;
  1516. free_token(token);
  1517. ret = event_read_print_args(event, &event->print_fmt.args);
  1518. if (ret < 0)
  1519. return -1;
  1520. return ret;
  1521. fail:
  1522. free_token(token);
  1523. return -1;
  1524. }
  1525. static struct format_field *
  1526. find_common_field(struct event *event, const char *name)
  1527. {
  1528. struct format_field *format;
  1529. for (format = event->format.common_fields;
  1530. format; format = format->next) {
  1531. if (strcmp(format->name, name) == 0)
  1532. break;
  1533. }
  1534. return format;
  1535. }
  1536. static struct format_field *
  1537. find_field(struct event *event, const char *name)
  1538. {
  1539. struct format_field *format;
  1540. for (format = event->format.fields;
  1541. format; format = format->next) {
  1542. if (strcmp(format->name, name) == 0)
  1543. break;
  1544. }
  1545. return format;
  1546. }
  1547. static struct format_field *
  1548. find_any_field(struct event *event, const char *name)
  1549. {
  1550. struct format_field *format;
  1551. format = find_common_field(event, name);
  1552. if (format)
  1553. return format;
  1554. return find_field(event, name);
  1555. }
  1556. unsigned long long read_size(void *ptr, int size)
  1557. {
  1558. switch (size) {
  1559. case 1:
  1560. return *(unsigned char *)ptr;
  1561. case 2:
  1562. return data2host2(ptr);
  1563. case 4:
  1564. return data2host4(ptr);
  1565. case 8:
  1566. return data2host8(ptr);
  1567. default:
  1568. /* BUG! */
  1569. return 0;
  1570. }
  1571. }
  1572. unsigned long long
  1573. raw_field_value(struct event *event, const char *name, void *data)
  1574. {
  1575. struct format_field *field;
  1576. field = find_any_field(event, name);
  1577. if (!field)
  1578. return 0ULL;
  1579. return read_size(data + field->offset, field->size);
  1580. }
  1581. void *raw_field_ptr(struct event *event, const char *name, void *data)
  1582. {
  1583. struct format_field *field;
  1584. field = find_any_field(event, name);
  1585. if (!field)
  1586. return NULL;
  1587. if (field->flags & FIELD_IS_DYNAMIC) {
  1588. int offset;
  1589. offset = *(int *)(data + field->offset);
  1590. offset &= 0xffff;
  1591. return data + offset;
  1592. }
  1593. return data + field->offset;
  1594. }
  1595. static int get_common_info(const char *type, int *offset, int *size)
  1596. {
  1597. struct event *event;
  1598. struct format_field *field;
  1599. /*
  1600. * All events should have the same common elements.
  1601. * Pick any event to find where the type is;
  1602. */
  1603. if (!event_list)
  1604. die("no event_list!");
  1605. event = event_list;
  1606. field = find_common_field(event, type);
  1607. if (!field)
  1608. die("field '%s' not found", type);
  1609. *offset = field->offset;
  1610. *size = field->size;
  1611. return 0;
  1612. }
  1613. static int __parse_common(void *data, int *size, int *offset,
  1614. const char *name)
  1615. {
  1616. int ret;
  1617. if (!*size) {
  1618. ret = get_common_info(name, offset, size);
  1619. if (ret < 0)
  1620. return ret;
  1621. }
  1622. return read_size(data + *offset, *size);
  1623. }
  1624. int trace_parse_common_type(void *data)
  1625. {
  1626. static int type_offset;
  1627. static int type_size;
  1628. return __parse_common(data, &type_size, &type_offset,
  1629. "common_type");
  1630. }
  1631. int trace_parse_common_pid(void *data)
  1632. {
  1633. static int pid_offset;
  1634. static int pid_size;
  1635. return __parse_common(data, &pid_size, &pid_offset,
  1636. "common_pid");
  1637. }
  1638. int parse_common_pc(void *data)
  1639. {
  1640. static int pc_offset;
  1641. static int pc_size;
  1642. return __parse_common(data, &pc_size, &pc_offset,
  1643. "common_preempt_count");
  1644. }
  1645. int parse_common_flags(void *data)
  1646. {
  1647. static int flags_offset;
  1648. static int flags_size;
  1649. return __parse_common(data, &flags_size, &flags_offset,
  1650. "common_flags");
  1651. }
  1652. int parse_common_lock_depth(void *data)
  1653. {
  1654. static int ld_offset;
  1655. static int ld_size;
  1656. int ret;
  1657. ret = __parse_common(data, &ld_size, &ld_offset,
  1658. "common_lock_depth");
  1659. if (ret < 0)
  1660. return -1;
  1661. return ret;
  1662. }
  1663. struct event *trace_find_event(int id)
  1664. {
  1665. struct event *event;
  1666. for (event = event_list; event; event = event->next) {
  1667. if (event->id == id)
  1668. break;
  1669. }
  1670. return event;
  1671. }
  1672. struct event *trace_find_next_event(struct event *event)
  1673. {
  1674. if (!event)
  1675. return event_list;
  1676. return event->next;
  1677. }
  1678. static unsigned long long eval_num_arg(void *data, int size,
  1679. struct event *event, struct print_arg *arg)
  1680. {
  1681. unsigned long long val = 0;
  1682. unsigned long long left, right;
  1683. struct print_arg *larg;
  1684. switch (arg->type) {
  1685. case PRINT_NULL:
  1686. /* ?? */
  1687. return 0;
  1688. case PRINT_ATOM:
  1689. return strtoull(arg->atom.atom, NULL, 0);
  1690. case PRINT_FIELD:
  1691. if (!arg->field.field) {
  1692. arg->field.field = find_any_field(event, arg->field.name);
  1693. if (!arg->field.field)
  1694. die("field %s not found", arg->field.name);
  1695. }
  1696. /* must be a number */
  1697. val = read_size(data + arg->field.field->offset,
  1698. arg->field.field->size);
  1699. break;
  1700. case PRINT_FLAGS:
  1701. case PRINT_SYMBOL:
  1702. break;
  1703. case PRINT_TYPE:
  1704. return eval_num_arg(data, size, event, arg->typecast.item);
  1705. case PRINT_STRING:
  1706. return 0;
  1707. break;
  1708. case PRINT_OP:
  1709. if (strcmp(arg->op.op, "[") == 0) {
  1710. /*
  1711. * Arrays are special, since we don't want
  1712. * to read the arg as is.
  1713. */
  1714. if (arg->op.left->type != PRINT_FIELD)
  1715. goto default_op; /* oops, all bets off */
  1716. larg = arg->op.left;
  1717. if (!larg->field.field) {
  1718. larg->field.field =
  1719. find_any_field(event, larg->field.name);
  1720. if (!larg->field.field)
  1721. die("field %s not found", larg->field.name);
  1722. }
  1723. right = eval_num_arg(data, size, event, arg->op.right);
  1724. val = read_size(data + larg->field.field->offset +
  1725. right * long_size, long_size);
  1726. break;
  1727. }
  1728. default_op:
  1729. left = eval_num_arg(data, size, event, arg->op.left);
  1730. right = eval_num_arg(data, size, event, arg->op.right);
  1731. switch (arg->op.op[0]) {
  1732. case '|':
  1733. if (arg->op.op[1])
  1734. val = left || right;
  1735. else
  1736. val = left | right;
  1737. break;
  1738. case '&':
  1739. if (arg->op.op[1])
  1740. val = left && right;
  1741. else
  1742. val = left & right;
  1743. break;
  1744. case '<':
  1745. switch (arg->op.op[1]) {
  1746. case 0:
  1747. val = left < right;
  1748. break;
  1749. case '<':
  1750. val = left << right;
  1751. break;
  1752. case '=':
  1753. val = left <= right;
  1754. break;
  1755. default:
  1756. die("unknown op '%s'", arg->op.op);
  1757. }
  1758. break;
  1759. case '>':
  1760. switch (arg->op.op[1]) {
  1761. case 0:
  1762. val = left > right;
  1763. break;
  1764. case '>':
  1765. val = left >> right;
  1766. break;
  1767. case '=':
  1768. val = left >= right;
  1769. break;
  1770. default:
  1771. die("unknown op '%s'", arg->op.op);
  1772. }
  1773. break;
  1774. case '=':
  1775. if (arg->op.op[1] != '=')
  1776. die("unknown op '%s'", arg->op.op);
  1777. val = left == right;
  1778. break;
  1779. case '-':
  1780. val = left - right;
  1781. break;
  1782. case '+':
  1783. val = left + right;
  1784. break;
  1785. default:
  1786. die("unknown op '%s'", arg->op.op);
  1787. }
  1788. break;
  1789. default: /* not sure what to do there */
  1790. return 0;
  1791. }
  1792. return val;
  1793. }
  1794. struct flag {
  1795. const char *name;
  1796. unsigned long long value;
  1797. };
  1798. static const struct flag flags[] = {
  1799. { "HI_SOFTIRQ", 0 },
  1800. { "TIMER_SOFTIRQ", 1 },
  1801. { "NET_TX_SOFTIRQ", 2 },
  1802. { "NET_RX_SOFTIRQ", 3 },
  1803. { "BLOCK_SOFTIRQ", 4 },
  1804. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  1805. { "TASKLET_SOFTIRQ", 6 },
  1806. { "SCHED_SOFTIRQ", 7 },
  1807. { "HRTIMER_SOFTIRQ", 8 },
  1808. { "RCU_SOFTIRQ", 9 },
  1809. { "HRTIMER_NORESTART", 0 },
  1810. { "HRTIMER_RESTART", 1 },
  1811. };
  1812. unsigned long long eval_flag(const char *flag)
  1813. {
  1814. int i;
  1815. /*
  1816. * Some flags in the format files do not get converted.
  1817. * If the flag is not numeric, see if it is something that
  1818. * we already know about.
  1819. */
  1820. if (isdigit(flag[0]))
  1821. return strtoull(flag, NULL, 0);
  1822. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1823. if (strcmp(flags[i].name, flag) == 0)
  1824. return flags[i].value;
  1825. return 0;
  1826. }
  1827. static void print_str_arg(void *data, int size,
  1828. struct event *event, struct print_arg *arg)
  1829. {
  1830. struct print_flag_sym *flag;
  1831. unsigned long long val, fval;
  1832. char *str;
  1833. int print;
  1834. switch (arg->type) {
  1835. case PRINT_NULL:
  1836. /* ?? */
  1837. return;
  1838. case PRINT_ATOM:
  1839. printf("%s", arg->atom.atom);
  1840. return;
  1841. case PRINT_FIELD:
  1842. if (!arg->field.field) {
  1843. arg->field.field = find_any_field(event, arg->field.name);
  1844. if (!arg->field.field)
  1845. die("field %s not found", arg->field.name);
  1846. }
  1847. str = malloc_or_die(arg->field.field->size + 1);
  1848. memcpy(str, data + arg->field.field->offset,
  1849. arg->field.field->size);
  1850. str[arg->field.field->size] = 0;
  1851. printf("%s", str);
  1852. free(str);
  1853. break;
  1854. case PRINT_FLAGS:
  1855. val = eval_num_arg(data, size, event, arg->flags.field);
  1856. print = 0;
  1857. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1858. fval = eval_flag(flag->value);
  1859. if (!val && !fval) {
  1860. printf("%s", flag->str);
  1861. break;
  1862. }
  1863. if (fval && (val & fval) == fval) {
  1864. if (print && arg->flags.delim)
  1865. printf("%s", arg->flags.delim);
  1866. printf("%s", flag->str);
  1867. print = 1;
  1868. val &= ~fval;
  1869. }
  1870. }
  1871. break;
  1872. case PRINT_SYMBOL:
  1873. val = eval_num_arg(data, size, event, arg->symbol.field);
  1874. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1875. fval = eval_flag(flag->value);
  1876. if (val == fval) {
  1877. printf("%s", flag->str);
  1878. break;
  1879. }
  1880. }
  1881. break;
  1882. case PRINT_TYPE:
  1883. break;
  1884. case PRINT_STRING: {
  1885. int str_offset;
  1886. if (arg->string.offset == -1) {
  1887. struct format_field *f;
  1888. f = find_any_field(event, arg->string.string);
  1889. arg->string.offset = f->offset;
  1890. }
  1891. str_offset = *(int *)(data + arg->string.offset);
  1892. str_offset &= 0xffff;
  1893. printf("%s", ((char *)data) + str_offset);
  1894. break;
  1895. }
  1896. case PRINT_OP:
  1897. /*
  1898. * The only op for string should be ? :
  1899. */
  1900. if (arg->op.op[0] != '?')
  1901. return;
  1902. val = eval_num_arg(data, size, event, arg->op.left);
  1903. if (val)
  1904. print_str_arg(data, size, event, arg->op.right->op.left);
  1905. else
  1906. print_str_arg(data, size, event, arg->op.right->op.right);
  1907. break;
  1908. default:
  1909. /* well... */
  1910. break;
  1911. }
  1912. }
  1913. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1914. {
  1915. static struct format_field *field, *ip_field;
  1916. struct print_arg *args, *arg, **next;
  1917. unsigned long long ip, val;
  1918. char *ptr;
  1919. void *bptr;
  1920. if (!field) {
  1921. field = find_field(event, "buf");
  1922. if (!field)
  1923. die("can't find buffer field for binary printk");
  1924. ip_field = find_field(event, "ip");
  1925. if (!ip_field)
  1926. die("can't find ip field for binary printk");
  1927. }
  1928. ip = read_size(data + ip_field->offset, ip_field->size);
  1929. /*
  1930. * The first arg is the IP pointer.
  1931. */
  1932. args = malloc_or_die(sizeof(*args));
  1933. arg = args;
  1934. arg->next = NULL;
  1935. next = &arg->next;
  1936. arg->type = PRINT_ATOM;
  1937. arg->atom.atom = malloc_or_die(32);
  1938. sprintf(arg->atom.atom, "%lld", ip);
  1939. /* skip the first "%pf : " */
  1940. for (ptr = fmt + 6, bptr = data + field->offset;
  1941. bptr < data + size && *ptr; ptr++) {
  1942. int ls = 0;
  1943. if (*ptr == '%') {
  1944. process_again:
  1945. ptr++;
  1946. switch (*ptr) {
  1947. case '%':
  1948. break;
  1949. case 'l':
  1950. ls++;
  1951. goto process_again;
  1952. case 'L':
  1953. ls = 2;
  1954. goto process_again;
  1955. case '0' ... '9':
  1956. goto process_again;
  1957. case 'p':
  1958. ls = 1;
  1959. /* fall through */
  1960. case 'd':
  1961. case 'u':
  1962. case 'x':
  1963. case 'i':
  1964. /* the pointers are always 4 bytes aligned */
  1965. bptr = (void *)(((unsigned long)bptr + 3) &
  1966. ~3);
  1967. switch (ls) {
  1968. case 0:
  1969. case 1:
  1970. ls = long_size;
  1971. break;
  1972. case 2:
  1973. ls = 8;
  1974. default:
  1975. break;
  1976. }
  1977. val = read_size(bptr, ls);
  1978. bptr += ls;
  1979. arg = malloc_or_die(sizeof(*arg));
  1980. arg->next = NULL;
  1981. arg->type = PRINT_ATOM;
  1982. arg->atom.atom = malloc_or_die(32);
  1983. sprintf(arg->atom.atom, "%lld", val);
  1984. *next = arg;
  1985. next = &arg->next;
  1986. break;
  1987. case 's':
  1988. arg = malloc_or_die(sizeof(*arg));
  1989. arg->next = NULL;
  1990. arg->type = PRINT_STRING;
  1991. arg->string.string = strdup(bptr);
  1992. bptr += strlen(bptr) + 1;
  1993. *next = arg;
  1994. next = &arg->next;
  1995. default:
  1996. break;
  1997. }
  1998. }
  1999. }
  2000. return args;
  2001. }
  2002. static void free_args(struct print_arg *args)
  2003. {
  2004. struct print_arg *next;
  2005. while (args) {
  2006. next = args->next;
  2007. if (args->type == PRINT_ATOM)
  2008. free(args->atom.atom);
  2009. else
  2010. free(args->string.string);
  2011. free(args);
  2012. args = next;
  2013. }
  2014. }
  2015. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  2016. {
  2017. unsigned long long addr;
  2018. static struct format_field *field;
  2019. struct printk_map *printk;
  2020. char *format;
  2021. char *p;
  2022. if (!field) {
  2023. field = find_field(event, "fmt");
  2024. if (!field)
  2025. die("can't find format field for binary printk");
  2026. printf("field->offset = %d size=%d\n", field->offset, field->size);
  2027. }
  2028. addr = read_size(data + field->offset, field->size);
  2029. printk = find_printk(addr);
  2030. if (!printk) {
  2031. format = malloc_or_die(45);
  2032. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  2033. addr);
  2034. return format;
  2035. }
  2036. p = printk->printk;
  2037. /* Remove any quotes. */
  2038. if (*p == '"')
  2039. p++;
  2040. format = malloc_or_die(strlen(p) + 10);
  2041. sprintf(format, "%s : %s", "%pf", p);
  2042. /* remove ending quotes and new line since we will add one too */
  2043. p = format + strlen(format) - 1;
  2044. if (*p == '"')
  2045. *p = 0;
  2046. p -= 2;
  2047. if (strcmp(p, "\\n") == 0)
  2048. *p = 0;
  2049. return format;
  2050. }
  2051. static void pretty_print(void *data, int size, struct event *event)
  2052. {
  2053. struct print_fmt *print_fmt = &event->print_fmt;
  2054. struct print_arg *arg = print_fmt->args;
  2055. struct print_arg *args = NULL;
  2056. const char *ptr = print_fmt->format;
  2057. unsigned long long val;
  2058. struct func_map *func;
  2059. const char *saveptr;
  2060. char *bprint_fmt = NULL;
  2061. char format[32];
  2062. int show_func;
  2063. int len;
  2064. int ls;
  2065. if (event->flags & EVENT_FL_ISFUNC)
  2066. ptr = " %pF <-- %pF";
  2067. if (event->flags & EVENT_FL_ISBPRINT) {
  2068. bprint_fmt = get_bprint_format(data, size, event);
  2069. args = make_bprint_args(bprint_fmt, data, size, event);
  2070. arg = args;
  2071. ptr = bprint_fmt;
  2072. }
  2073. for (; *ptr; ptr++) {
  2074. ls = 0;
  2075. if (*ptr == '\\') {
  2076. ptr++;
  2077. switch (*ptr) {
  2078. case 'n':
  2079. printf("\n");
  2080. break;
  2081. case 't':
  2082. printf("\t");
  2083. break;
  2084. case 'r':
  2085. printf("\r");
  2086. break;
  2087. case '\\':
  2088. printf("\\");
  2089. break;
  2090. default:
  2091. printf("%c", *ptr);
  2092. break;
  2093. }
  2094. } else if (*ptr == '%') {
  2095. saveptr = ptr;
  2096. show_func = 0;
  2097. cont_process:
  2098. ptr++;
  2099. switch (*ptr) {
  2100. case '%':
  2101. printf("%%");
  2102. break;
  2103. case 'l':
  2104. ls++;
  2105. goto cont_process;
  2106. case 'L':
  2107. ls = 2;
  2108. goto cont_process;
  2109. case 'z':
  2110. case 'Z':
  2111. case '0' ... '9':
  2112. goto cont_process;
  2113. case 'p':
  2114. if (long_size == 4)
  2115. ls = 1;
  2116. else
  2117. ls = 2;
  2118. if (*(ptr+1) == 'F' ||
  2119. *(ptr+1) == 'f') {
  2120. ptr++;
  2121. show_func = *ptr;
  2122. }
  2123. /* fall through */
  2124. case 'd':
  2125. case 'i':
  2126. case 'x':
  2127. case 'X':
  2128. case 'u':
  2129. if (!arg)
  2130. die("no argument match");
  2131. len = ((unsigned long)ptr + 1) -
  2132. (unsigned long)saveptr;
  2133. /* should never happen */
  2134. if (len > 32)
  2135. die("bad format!");
  2136. memcpy(format, saveptr, len);
  2137. format[len] = 0;
  2138. val = eval_num_arg(data, size, event, arg);
  2139. arg = arg->next;
  2140. if (show_func) {
  2141. func = find_func(val);
  2142. if (func) {
  2143. printf("%s", func->func);
  2144. if (show_func == 'F')
  2145. printf("+0x%llx",
  2146. val - func->addr);
  2147. break;
  2148. }
  2149. }
  2150. switch (ls) {
  2151. case 0:
  2152. printf(format, (int)val);
  2153. break;
  2154. case 1:
  2155. printf(format, (long)val);
  2156. break;
  2157. case 2:
  2158. printf(format, (long long)val);
  2159. break;
  2160. default:
  2161. die("bad count (%d)", ls);
  2162. }
  2163. break;
  2164. case 's':
  2165. if (!arg)
  2166. die("no matching argument");
  2167. print_str_arg(data, size, event, arg);
  2168. arg = arg->next;
  2169. break;
  2170. default:
  2171. printf(">%c<", *ptr);
  2172. }
  2173. } else
  2174. printf("%c", *ptr);
  2175. }
  2176. if (args) {
  2177. free_args(args);
  2178. free(bprint_fmt);
  2179. }
  2180. }
  2181. static inline int log10_cpu(int nb)
  2182. {
  2183. if (nb / 100)
  2184. return 3;
  2185. if (nb / 10)
  2186. return 2;
  2187. return 1;
  2188. }
  2189. static void print_lat_fmt(void *data, int size __unused)
  2190. {
  2191. unsigned int lat_flags;
  2192. unsigned int pc;
  2193. int lock_depth;
  2194. int hardirq;
  2195. int softirq;
  2196. lat_flags = parse_common_flags(data);
  2197. pc = parse_common_pc(data);
  2198. lock_depth = parse_common_lock_depth(data);
  2199. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  2200. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  2201. printf("%c%c%c",
  2202. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  2203. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  2204. 'X' : '.',
  2205. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  2206. 'N' : '.',
  2207. (hardirq && softirq) ? 'H' :
  2208. hardirq ? 'h' : softirq ? 's' : '.');
  2209. if (pc)
  2210. printf("%x", pc);
  2211. else
  2212. printf(".");
  2213. if (lock_depth < 0)
  2214. printf(". ");
  2215. else
  2216. printf("%d ", lock_depth);
  2217. }
  2218. #define TRACE_GRAPH_INDENT 2
  2219. static struct record *
  2220. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2221. struct record *next)
  2222. {
  2223. struct format_field *field;
  2224. struct event *event;
  2225. unsigned long val;
  2226. int type;
  2227. int pid;
  2228. type = trace_parse_common_type(next->data);
  2229. event = trace_find_event(type);
  2230. if (!event)
  2231. return NULL;
  2232. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2233. return NULL;
  2234. pid = trace_parse_common_pid(next->data);
  2235. field = find_field(event, "func");
  2236. if (!field)
  2237. die("function return does not have field func");
  2238. val = read_size(next->data + field->offset, field->size);
  2239. if (cur_pid != pid || cur_func != val)
  2240. return NULL;
  2241. /* this is a leaf, now advance the iterator */
  2242. return trace_read_data(cpu);
  2243. }
  2244. /* Signal a overhead of time execution to the output */
  2245. static void print_graph_overhead(unsigned long long duration)
  2246. {
  2247. /* Non nested entry or return */
  2248. if (duration == ~0ULL)
  2249. return (void)printf(" ");
  2250. /* Duration exceeded 100 msecs */
  2251. if (duration > 100000ULL)
  2252. return (void)printf("! ");
  2253. /* Duration exceeded 10 msecs */
  2254. if (duration > 10000ULL)
  2255. return (void)printf("+ ");
  2256. printf(" ");
  2257. }
  2258. static void print_graph_duration(unsigned long long duration)
  2259. {
  2260. unsigned long usecs = duration / 1000;
  2261. unsigned long nsecs_rem = duration % 1000;
  2262. /* log10(ULONG_MAX) + '\0' */
  2263. char msecs_str[21];
  2264. char nsecs_str[5];
  2265. int len;
  2266. int i;
  2267. sprintf(msecs_str, "%lu", usecs);
  2268. /* Print msecs */
  2269. len = printf("%lu", usecs);
  2270. /* Print nsecs (we don't want to exceed 7 numbers) */
  2271. if (len < 7) {
  2272. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2273. len += printf(".%s", nsecs_str);
  2274. }
  2275. printf(" us ");
  2276. /* Print remaining spaces to fit the row's width */
  2277. for (i = len; i < 7; i++)
  2278. printf(" ");
  2279. printf("| ");
  2280. }
  2281. static void
  2282. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2283. {
  2284. unsigned long long rettime, calltime;
  2285. unsigned long long duration, depth;
  2286. unsigned long long val;
  2287. struct format_field *field;
  2288. struct func_map *func;
  2289. struct event *ret_event;
  2290. int type;
  2291. int i;
  2292. type = trace_parse_common_type(ret_rec->data);
  2293. ret_event = trace_find_event(type);
  2294. field = find_field(ret_event, "rettime");
  2295. if (!field)
  2296. die("can't find rettime in return graph");
  2297. rettime = read_size(ret_rec->data + field->offset, field->size);
  2298. field = find_field(ret_event, "calltime");
  2299. if (!field)
  2300. die("can't find rettime in return graph");
  2301. calltime = read_size(ret_rec->data + field->offset, field->size);
  2302. duration = rettime - calltime;
  2303. /* Overhead */
  2304. print_graph_overhead(duration);
  2305. /* Duration */
  2306. print_graph_duration(duration);
  2307. field = find_field(event, "depth");
  2308. if (!field)
  2309. die("can't find depth in entry graph");
  2310. depth = read_size(data + field->offset, field->size);
  2311. /* Function */
  2312. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2313. printf(" ");
  2314. field = find_field(event, "func");
  2315. if (!field)
  2316. die("can't find func in entry graph");
  2317. val = read_size(data + field->offset, field->size);
  2318. func = find_func(val);
  2319. if (func)
  2320. printf("%s();", func->func);
  2321. else
  2322. printf("%llx();", val);
  2323. }
  2324. static void print_graph_nested(struct event *event, void *data)
  2325. {
  2326. struct format_field *field;
  2327. unsigned long long depth;
  2328. unsigned long long val;
  2329. struct func_map *func;
  2330. int i;
  2331. /* No overhead */
  2332. print_graph_overhead(-1);
  2333. /* No time */
  2334. printf(" | ");
  2335. field = find_field(event, "depth");
  2336. if (!field)
  2337. die("can't find depth in entry graph");
  2338. depth = read_size(data + field->offset, field->size);
  2339. /* Function */
  2340. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2341. printf(" ");
  2342. field = find_field(event, "func");
  2343. if (!field)
  2344. die("can't find func in entry graph");
  2345. val = read_size(data + field->offset, field->size);
  2346. func = find_func(val);
  2347. if (func)
  2348. printf("%s() {", func->func);
  2349. else
  2350. printf("%llx() {", val);
  2351. }
  2352. static void
  2353. pretty_print_func_ent(void *data, int size, struct event *event,
  2354. int cpu, int pid)
  2355. {
  2356. struct format_field *field;
  2357. struct record *rec;
  2358. void *copy_data;
  2359. unsigned long val;
  2360. if (latency_format) {
  2361. print_lat_fmt(data, size);
  2362. printf(" | ");
  2363. }
  2364. field = find_field(event, "func");
  2365. if (!field)
  2366. die("function entry does not have func field");
  2367. val = read_size(data + field->offset, field->size);
  2368. /*
  2369. * peek_data may unmap the data pointer. Copy it first.
  2370. */
  2371. copy_data = malloc_or_die(size);
  2372. memcpy(copy_data, data, size);
  2373. data = copy_data;
  2374. rec = trace_peek_data(cpu);
  2375. if (rec) {
  2376. rec = get_return_for_leaf(cpu, pid, val, rec);
  2377. if (rec) {
  2378. print_graph_entry_leaf(event, data, rec);
  2379. goto out_free;
  2380. }
  2381. }
  2382. print_graph_nested(event, data);
  2383. out_free:
  2384. free(data);
  2385. }
  2386. static void
  2387. pretty_print_func_ret(void *data, int size __unused, struct event *event)
  2388. {
  2389. unsigned long long rettime, calltime;
  2390. unsigned long long duration, depth;
  2391. struct format_field *field;
  2392. int i;
  2393. if (latency_format) {
  2394. print_lat_fmt(data, size);
  2395. printf(" | ");
  2396. }
  2397. field = find_field(event, "rettime");
  2398. if (!field)
  2399. die("can't find rettime in return graph");
  2400. rettime = read_size(data + field->offset, field->size);
  2401. field = find_field(event, "calltime");
  2402. if (!field)
  2403. die("can't find calltime in return graph");
  2404. calltime = read_size(data + field->offset, field->size);
  2405. duration = rettime - calltime;
  2406. /* Overhead */
  2407. print_graph_overhead(duration);
  2408. /* Duration */
  2409. print_graph_duration(duration);
  2410. field = find_field(event, "depth");
  2411. if (!field)
  2412. die("can't find depth in entry graph");
  2413. depth = read_size(data + field->offset, field->size);
  2414. /* Function */
  2415. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2416. printf(" ");
  2417. printf("}");
  2418. }
  2419. static void
  2420. pretty_print_func_graph(void *data, int size, struct event *event,
  2421. int cpu, int pid)
  2422. {
  2423. if (event->flags & EVENT_FL_ISFUNCENT)
  2424. pretty_print_func_ent(data, size, event, cpu, pid);
  2425. else if (event->flags & EVENT_FL_ISFUNCRET)
  2426. pretty_print_func_ret(data, size, event);
  2427. printf("\n");
  2428. }
  2429. void print_trace_event(int cpu, void *data, int size)
  2430. {
  2431. struct event *event;
  2432. int type;
  2433. int pid;
  2434. type = trace_parse_common_type(data);
  2435. event = trace_find_event(type);
  2436. if (!event) {
  2437. warning("ug! no event found for type %d", type);
  2438. return;
  2439. }
  2440. pid = trace_parse_common_pid(data);
  2441. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2442. return pretty_print_func_graph(data, size, event, cpu, pid);
  2443. if (latency_format)
  2444. print_lat_fmt(data, size);
  2445. if (event->flags & EVENT_FL_FAILED) {
  2446. printf("EVENT '%s' FAILED TO PARSE\n",
  2447. event->name);
  2448. return;
  2449. }
  2450. pretty_print(data, size, event);
  2451. }
  2452. static void print_fields(struct print_flag_sym *field)
  2453. {
  2454. printf("{ %s, %s }", field->value, field->str);
  2455. if (field->next) {
  2456. printf(", ");
  2457. print_fields(field->next);
  2458. }
  2459. }
  2460. static void print_args(struct print_arg *args)
  2461. {
  2462. int print_paren = 1;
  2463. switch (args->type) {
  2464. case PRINT_NULL:
  2465. printf("null");
  2466. break;
  2467. case PRINT_ATOM:
  2468. printf("%s", args->atom.atom);
  2469. break;
  2470. case PRINT_FIELD:
  2471. printf("REC->%s", args->field.name);
  2472. break;
  2473. case PRINT_FLAGS:
  2474. printf("__print_flags(");
  2475. print_args(args->flags.field);
  2476. printf(", %s, ", args->flags.delim);
  2477. print_fields(args->flags.flags);
  2478. printf(")");
  2479. break;
  2480. case PRINT_SYMBOL:
  2481. printf("__print_symbolic(");
  2482. print_args(args->symbol.field);
  2483. printf(", ");
  2484. print_fields(args->symbol.symbols);
  2485. printf(")");
  2486. break;
  2487. case PRINT_STRING:
  2488. printf("__get_str(%s)", args->string.string);
  2489. break;
  2490. case PRINT_TYPE:
  2491. printf("(%s)", args->typecast.type);
  2492. print_args(args->typecast.item);
  2493. break;
  2494. case PRINT_OP:
  2495. if (strcmp(args->op.op, ":") == 0)
  2496. print_paren = 0;
  2497. if (print_paren)
  2498. printf("(");
  2499. print_args(args->op.left);
  2500. printf(" %s ", args->op.op);
  2501. print_args(args->op.right);
  2502. if (print_paren)
  2503. printf(")");
  2504. break;
  2505. default:
  2506. /* we should warn... */
  2507. return;
  2508. }
  2509. if (args->next) {
  2510. printf("\n");
  2511. print_args(args->next);
  2512. }
  2513. }
  2514. int parse_ftrace_file(char *buf, unsigned long size)
  2515. {
  2516. struct format_field *field;
  2517. struct print_arg *arg, **list;
  2518. struct event *event;
  2519. int ret;
  2520. init_input_buf(buf, size);
  2521. event = alloc_event();
  2522. if (!event)
  2523. return -ENOMEM;
  2524. event->flags |= EVENT_FL_ISFTRACE;
  2525. event->name = event_read_name();
  2526. if (!event->name)
  2527. die("failed to read ftrace event name");
  2528. if (strcmp(event->name, "function") == 0)
  2529. event->flags |= EVENT_FL_ISFUNC;
  2530. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2531. event->flags |= EVENT_FL_ISFUNCENT;
  2532. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2533. event->flags |= EVENT_FL_ISFUNCRET;
  2534. else if (strcmp(event->name, "bprint") == 0)
  2535. event->flags |= EVENT_FL_ISBPRINT;
  2536. event->id = event_read_id();
  2537. if (event->id < 0)
  2538. die("failed to read ftrace event id");
  2539. add_event(event);
  2540. ret = event_read_format(event);
  2541. if (ret < 0)
  2542. die("failed to read ftrace event format");
  2543. ret = event_read_print(event);
  2544. if (ret < 0)
  2545. die("failed to read ftrace event print fmt");
  2546. /* New ftrace handles args */
  2547. if (ret > 0)
  2548. return 0;
  2549. /*
  2550. * The arguments for ftrace files are parsed by the fields.
  2551. * Set up the fields as their arguments.
  2552. */
  2553. list = &event->print_fmt.args;
  2554. for (field = event->format.fields; field; field = field->next) {
  2555. arg = malloc_or_die(sizeof(*arg));
  2556. memset(arg, 0, sizeof(*arg));
  2557. *list = arg;
  2558. list = &arg->next;
  2559. arg->type = PRINT_FIELD;
  2560. arg->field.name = field->name;
  2561. arg->field.field = field;
  2562. }
  2563. return 0;
  2564. }
  2565. int parse_event_file(char *buf, unsigned long size, char *sys)
  2566. {
  2567. struct event *event;
  2568. int ret;
  2569. init_input_buf(buf, size);
  2570. event = alloc_event();
  2571. if (!event)
  2572. return -ENOMEM;
  2573. event->name = event_read_name();
  2574. if (!event->name)
  2575. die("failed to read event name");
  2576. event->id = event_read_id();
  2577. if (event->id < 0)
  2578. die("failed to read event id");
  2579. ret = event_read_format(event);
  2580. if (ret < 0) {
  2581. warning("failed to read event format for %s", event->name);
  2582. goto event_failed;
  2583. }
  2584. ret = event_read_print(event);
  2585. if (ret < 0) {
  2586. warning("failed to read event print fmt for %s", event->name);
  2587. goto event_failed;
  2588. }
  2589. event->system = strdup(sys);
  2590. #define PRINT_ARGS 0
  2591. if (PRINT_ARGS && event->print_fmt.args)
  2592. print_args(event->print_fmt.args);
  2593. add_event(event);
  2594. return 0;
  2595. event_failed:
  2596. event->flags |= EVENT_FL_FAILED;
  2597. /* still add it even if it failed */
  2598. add_event(event);
  2599. return -1;
  2600. }
  2601. void parse_set_info(int nr_cpus, int long_sz)
  2602. {
  2603. cpus = nr_cpus;
  2604. long_size = long_sz;
  2605. }
  2606. int common_pc(struct scripting_context *context)
  2607. {
  2608. return parse_common_pc(context->event_data);
  2609. }
  2610. int common_flags(struct scripting_context *context)
  2611. {
  2612. return parse_common_flags(context->event_data);
  2613. }
  2614. int common_lock_depth(struct scripting_context *context)
  2615. {
  2616. return parse_common_lock_depth(context->event_data);
  2617. }