parse-filter.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include "event-parse.h"
  13. #include "event-utils.h"
  14. #define COMM "COMM"
  15. #define CPU "CPU"
  16. static struct format_field comm = {
  17. .name = "COMM",
  18. };
  19. static struct format_field cpu = {
  20. .name = "CPU",
  21. };
  22. struct event_list {
  23. struct event_list *next;
  24. struct event_format *event;
  25. };
  26. static void show_error(char *error_buf, const char *fmt, ...)
  27. {
  28. unsigned long long index;
  29. const char *input;
  30. va_list ap;
  31. int len;
  32. int i;
  33. input = tep_get_input_buf();
  34. index = tep_get_input_buf_ptr();
  35. len = input ? strlen(input) : 0;
  36. if (len) {
  37. strcpy(error_buf, input);
  38. error_buf[len] = '\n';
  39. for (i = 1; i < len && i < index; i++)
  40. error_buf[len+i] = ' ';
  41. error_buf[len + i] = '^';
  42. error_buf[len + i + 1] = '\n';
  43. len += i+2;
  44. }
  45. va_start(ap, fmt);
  46. vsnprintf(error_buf + len, TEP_FILTER_ERROR_BUFSZ - len, fmt, ap);
  47. va_end(ap);
  48. }
  49. static void free_token(char *token)
  50. {
  51. tep_free_token(token);
  52. }
  53. static enum event_type read_token(char **tok)
  54. {
  55. enum event_type type;
  56. char *token = NULL;
  57. do {
  58. free_token(token);
  59. type = tep_read_token(&token);
  60. } while (type == EVENT_NEWLINE || type == EVENT_SPACE);
  61. /* If token is = or ! check to see if the next char is ~ */
  62. if (token &&
  63. (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
  64. tep_peek_char() == '~') {
  65. /* append it */
  66. *tok = malloc(3);
  67. if (*tok == NULL) {
  68. free_token(token);
  69. return EVENT_ERROR;
  70. }
  71. sprintf(*tok, "%c%c", *token, '~');
  72. free_token(token);
  73. /* Now remove the '~' from the buffer */
  74. tep_read_token(&token);
  75. free_token(token);
  76. } else
  77. *tok = token;
  78. return type;
  79. }
  80. static int filter_cmp(const void *a, const void *b)
  81. {
  82. const struct filter_type *ea = a;
  83. const struct filter_type *eb = b;
  84. if (ea->event_id < eb->event_id)
  85. return -1;
  86. if (ea->event_id > eb->event_id)
  87. return 1;
  88. return 0;
  89. }
  90. static struct filter_type *
  91. find_filter_type(struct event_filter *filter, int id)
  92. {
  93. struct filter_type *filter_type;
  94. struct filter_type key;
  95. key.event_id = id;
  96. filter_type = bsearch(&key, filter->event_filters,
  97. filter->filters,
  98. sizeof(*filter->event_filters),
  99. filter_cmp);
  100. return filter_type;
  101. }
  102. static struct filter_type *
  103. add_filter_type(struct event_filter *filter, int id)
  104. {
  105. struct filter_type *filter_type;
  106. int i;
  107. filter_type = find_filter_type(filter, id);
  108. if (filter_type)
  109. return filter_type;
  110. filter_type = realloc(filter->event_filters,
  111. sizeof(*filter->event_filters) *
  112. (filter->filters + 1));
  113. if (!filter_type)
  114. return NULL;
  115. filter->event_filters = filter_type;
  116. for (i = 0; i < filter->filters; i++) {
  117. if (filter->event_filters[i].event_id > id)
  118. break;
  119. }
  120. if (i < filter->filters)
  121. memmove(&filter->event_filters[i+1],
  122. &filter->event_filters[i],
  123. sizeof(*filter->event_filters) *
  124. (filter->filters - i));
  125. filter_type = &filter->event_filters[i];
  126. filter_type->event_id = id;
  127. filter_type->event = tep_find_event(filter->pevent, id);
  128. filter_type->filter = NULL;
  129. filter->filters++;
  130. return filter_type;
  131. }
  132. /**
  133. * tep_filter_alloc - create a new event filter
  134. * @pevent: The pevent that this filter is associated with
  135. */
  136. struct event_filter *tep_filter_alloc(struct tep_handle *pevent)
  137. {
  138. struct event_filter *filter;
  139. filter = malloc(sizeof(*filter));
  140. if (filter == NULL)
  141. return NULL;
  142. memset(filter, 0, sizeof(*filter));
  143. filter->pevent = pevent;
  144. tep_ref(pevent);
  145. return filter;
  146. }
  147. static struct filter_arg *allocate_arg(void)
  148. {
  149. return calloc(1, sizeof(struct filter_arg));
  150. }
  151. static void free_arg(struct filter_arg *arg)
  152. {
  153. if (!arg)
  154. return;
  155. switch (arg->type) {
  156. case FILTER_ARG_NONE:
  157. case FILTER_ARG_BOOLEAN:
  158. break;
  159. case FILTER_ARG_NUM:
  160. free_arg(arg->num.left);
  161. free_arg(arg->num.right);
  162. break;
  163. case FILTER_ARG_EXP:
  164. free_arg(arg->exp.left);
  165. free_arg(arg->exp.right);
  166. break;
  167. case FILTER_ARG_STR:
  168. free(arg->str.val);
  169. regfree(&arg->str.reg);
  170. free(arg->str.buffer);
  171. break;
  172. case FILTER_ARG_VALUE:
  173. if (arg->value.type == FILTER_STRING ||
  174. arg->value.type == FILTER_CHAR)
  175. free(arg->value.str);
  176. break;
  177. case FILTER_ARG_OP:
  178. free_arg(arg->op.left);
  179. free_arg(arg->op.right);
  180. default:
  181. break;
  182. }
  183. free(arg);
  184. }
  185. static int add_event(struct event_list **events,
  186. struct event_format *event)
  187. {
  188. struct event_list *list;
  189. list = malloc(sizeof(*list));
  190. if (list == NULL)
  191. return -1;
  192. list->next = *events;
  193. *events = list;
  194. list->event = event;
  195. return 0;
  196. }
  197. static int event_match(struct event_format *event,
  198. regex_t *sreg, regex_t *ereg)
  199. {
  200. if (sreg) {
  201. return !regexec(sreg, event->system, 0, NULL, 0) &&
  202. !regexec(ereg, event->name, 0, NULL, 0);
  203. }
  204. return !regexec(ereg, event->system, 0, NULL, 0) ||
  205. !regexec(ereg, event->name, 0, NULL, 0);
  206. }
  207. static enum tep_errno
  208. find_event(struct tep_handle *pevent, struct event_list **events,
  209. char *sys_name, char *event_name)
  210. {
  211. struct event_format *event;
  212. regex_t ereg;
  213. regex_t sreg;
  214. int match = 0;
  215. int fail = 0;
  216. char *reg;
  217. int ret;
  218. int i;
  219. if (!event_name) {
  220. /* if no name is given, then swap sys and name */
  221. event_name = sys_name;
  222. sys_name = NULL;
  223. }
  224. ret = asprintf(&reg, "^%s$", event_name);
  225. if (ret < 0)
  226. return TEP_ERRNO__MEM_ALLOC_FAILED;
  227. ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);
  228. free(reg);
  229. if (ret)
  230. return TEP_ERRNO__INVALID_EVENT_NAME;
  231. if (sys_name) {
  232. ret = asprintf(&reg, "^%s$", sys_name);
  233. if (ret < 0) {
  234. regfree(&ereg);
  235. return TEP_ERRNO__MEM_ALLOC_FAILED;
  236. }
  237. ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);
  238. free(reg);
  239. if (ret) {
  240. regfree(&ereg);
  241. return TEP_ERRNO__INVALID_EVENT_NAME;
  242. }
  243. }
  244. for (i = 0; i < pevent->nr_events; i++) {
  245. event = pevent->events[i];
  246. if (event_match(event, sys_name ? &sreg : NULL, &ereg)) {
  247. match = 1;
  248. if (add_event(events, event) < 0) {
  249. fail = 1;
  250. break;
  251. }
  252. }
  253. }
  254. regfree(&ereg);
  255. if (sys_name)
  256. regfree(&sreg);
  257. if (!match)
  258. return TEP_ERRNO__EVENT_NOT_FOUND;
  259. if (fail)
  260. return TEP_ERRNO__MEM_ALLOC_FAILED;
  261. return 0;
  262. }
  263. static void free_events(struct event_list *events)
  264. {
  265. struct event_list *event;
  266. while (events) {
  267. event = events;
  268. events = events->next;
  269. free(event);
  270. }
  271. }
  272. static enum tep_errno
  273. create_arg_item(struct event_format *event, const char *token,
  274. enum event_type type, struct filter_arg **parg, char *error_str)
  275. {
  276. struct format_field *field;
  277. struct filter_arg *arg;
  278. arg = allocate_arg();
  279. if (arg == NULL) {
  280. show_error(error_str, "failed to allocate filter arg");
  281. return TEP_ERRNO__MEM_ALLOC_FAILED;
  282. }
  283. switch (type) {
  284. case EVENT_SQUOTE:
  285. case EVENT_DQUOTE:
  286. arg->type = FILTER_ARG_VALUE;
  287. arg->value.type =
  288. type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR;
  289. arg->value.str = strdup(token);
  290. if (!arg->value.str) {
  291. free_arg(arg);
  292. show_error(error_str, "failed to allocate string filter arg");
  293. return TEP_ERRNO__MEM_ALLOC_FAILED;
  294. }
  295. break;
  296. case EVENT_ITEM:
  297. /* if it is a number, then convert it */
  298. if (isdigit(token[0])) {
  299. arg->type = FILTER_ARG_VALUE;
  300. arg->value.type = FILTER_NUMBER;
  301. arg->value.val = strtoull(token, NULL, 0);
  302. break;
  303. }
  304. /* Consider this a field */
  305. field = tep_find_any_field(event, token);
  306. if (!field) {
  307. /* If token is 'COMM' or 'CPU' then it is special */
  308. if (strcmp(token, COMM) == 0) {
  309. field = &comm;
  310. } else if (strcmp(token, CPU) == 0) {
  311. field = &cpu;
  312. } else {
  313. /* not a field, Make it false */
  314. arg->type = FILTER_ARG_BOOLEAN;
  315. arg->boolean.value = FILTER_FALSE;
  316. break;
  317. }
  318. }
  319. arg->type = FILTER_ARG_FIELD;
  320. arg->field.field = field;
  321. break;
  322. default:
  323. free_arg(arg);
  324. show_error(error_str, "expected a value but found %s", token);
  325. return TEP_ERRNO__UNEXPECTED_TYPE;
  326. }
  327. *parg = arg;
  328. return 0;
  329. }
  330. static struct filter_arg *
  331. create_arg_op(enum filter_op_type btype)
  332. {
  333. struct filter_arg *arg;
  334. arg = allocate_arg();
  335. if (!arg)
  336. return NULL;
  337. arg->type = FILTER_ARG_OP;
  338. arg->op.type = btype;
  339. return arg;
  340. }
  341. static struct filter_arg *
  342. create_arg_exp(enum filter_exp_type etype)
  343. {
  344. struct filter_arg *arg;
  345. arg = allocate_arg();
  346. if (!arg)
  347. return NULL;
  348. arg->type = FILTER_ARG_EXP;
  349. arg->exp.type = etype;
  350. return arg;
  351. }
  352. static struct filter_arg *
  353. create_arg_cmp(enum filter_cmp_type ctype)
  354. {
  355. struct filter_arg *arg;
  356. arg = allocate_arg();
  357. if (!arg)
  358. return NULL;
  359. /* Use NUM and change if necessary */
  360. arg->type = FILTER_ARG_NUM;
  361. arg->num.type = ctype;
  362. return arg;
  363. }
  364. static enum tep_errno
  365. add_right(struct filter_arg *op, struct filter_arg *arg, char *error_str)
  366. {
  367. struct filter_arg *left;
  368. char *str;
  369. int op_type;
  370. int ret;
  371. switch (op->type) {
  372. case FILTER_ARG_EXP:
  373. if (op->exp.right)
  374. goto out_fail;
  375. op->exp.right = arg;
  376. break;
  377. case FILTER_ARG_OP:
  378. if (op->op.right)
  379. goto out_fail;
  380. op->op.right = arg;
  381. break;
  382. case FILTER_ARG_NUM:
  383. if (op->op.right)
  384. goto out_fail;
  385. /*
  386. * The arg must be num, str, or field
  387. */
  388. switch (arg->type) {
  389. case FILTER_ARG_VALUE:
  390. case FILTER_ARG_FIELD:
  391. break;
  392. default:
  393. show_error(error_str, "Illegal rvalue");
  394. return TEP_ERRNO__ILLEGAL_RVALUE;
  395. }
  396. /*
  397. * Depending on the type, we may need to
  398. * convert this to a string or regex.
  399. */
  400. switch (arg->value.type) {
  401. case FILTER_CHAR:
  402. /*
  403. * A char should be converted to number if
  404. * the string is 1 byte, and the compare
  405. * is not a REGEX.
  406. */
  407. if (strlen(arg->value.str) == 1 &&
  408. op->num.type != FILTER_CMP_REGEX &&
  409. op->num.type != FILTER_CMP_NOT_REGEX) {
  410. arg->value.type = FILTER_NUMBER;
  411. goto do_int;
  412. }
  413. /* fall through */
  414. case FILTER_STRING:
  415. /* convert op to a string arg */
  416. op_type = op->num.type;
  417. left = op->num.left;
  418. str = arg->value.str;
  419. /* reset the op for the new field */
  420. memset(op, 0, sizeof(*op));
  421. /*
  422. * If left arg was a field not found then
  423. * NULL the entire op.
  424. */
  425. if (left->type == FILTER_ARG_BOOLEAN) {
  426. free_arg(left);
  427. free_arg(arg);
  428. op->type = FILTER_ARG_BOOLEAN;
  429. op->boolean.value = FILTER_FALSE;
  430. break;
  431. }
  432. /* Left arg must be a field */
  433. if (left->type != FILTER_ARG_FIELD) {
  434. show_error(error_str,
  435. "Illegal lvalue for string comparison");
  436. return TEP_ERRNO__ILLEGAL_LVALUE;
  437. }
  438. /* Make sure this is a valid string compare */
  439. switch (op_type) {
  440. case FILTER_CMP_EQ:
  441. op_type = FILTER_CMP_MATCH;
  442. break;
  443. case FILTER_CMP_NE:
  444. op_type = FILTER_CMP_NOT_MATCH;
  445. break;
  446. case FILTER_CMP_REGEX:
  447. case FILTER_CMP_NOT_REGEX:
  448. ret = regcomp(&op->str.reg, str, REG_ICASE|REG_NOSUB);
  449. if (ret) {
  450. show_error(error_str,
  451. "RegEx '%s' did not compute",
  452. str);
  453. return TEP_ERRNO__INVALID_REGEX;
  454. }
  455. break;
  456. default:
  457. show_error(error_str,
  458. "Illegal comparison for string");
  459. return TEP_ERRNO__ILLEGAL_STRING_CMP;
  460. }
  461. op->type = FILTER_ARG_STR;
  462. op->str.type = op_type;
  463. op->str.field = left->field.field;
  464. op->str.val = strdup(str);
  465. if (!op->str.val) {
  466. show_error(error_str, "Failed to allocate string filter");
  467. return TEP_ERRNO__MEM_ALLOC_FAILED;
  468. }
  469. /*
  470. * Need a buffer to copy data for tests
  471. */
  472. op->str.buffer = malloc(op->str.field->size + 1);
  473. if (!op->str.buffer) {
  474. show_error(error_str, "Failed to allocate string filter");
  475. return TEP_ERRNO__MEM_ALLOC_FAILED;
  476. }
  477. /* Null terminate this buffer */
  478. op->str.buffer[op->str.field->size] = 0;
  479. /* We no longer have left or right args */
  480. free_arg(arg);
  481. free_arg(left);
  482. break;
  483. case FILTER_NUMBER:
  484. do_int:
  485. switch (op->num.type) {
  486. case FILTER_CMP_REGEX:
  487. case FILTER_CMP_NOT_REGEX:
  488. show_error(error_str,
  489. "Op not allowed with integers");
  490. return TEP_ERRNO__ILLEGAL_INTEGER_CMP;
  491. default:
  492. break;
  493. }
  494. /* numeric compare */
  495. op->num.right = arg;
  496. break;
  497. default:
  498. goto out_fail;
  499. }
  500. break;
  501. default:
  502. goto out_fail;
  503. }
  504. return 0;
  505. out_fail:
  506. show_error(error_str, "Syntax error");
  507. return TEP_ERRNO__SYNTAX_ERROR;
  508. }
  509. static struct filter_arg *
  510. rotate_op_right(struct filter_arg *a, struct filter_arg *b)
  511. {
  512. struct filter_arg *arg;
  513. arg = a->op.right;
  514. a->op.right = b;
  515. return arg;
  516. }
  517. static enum tep_errno add_left(struct filter_arg *op, struct filter_arg *arg)
  518. {
  519. switch (op->type) {
  520. case FILTER_ARG_EXP:
  521. if (arg->type == FILTER_ARG_OP)
  522. arg = rotate_op_right(arg, op);
  523. op->exp.left = arg;
  524. break;
  525. case FILTER_ARG_OP:
  526. op->op.left = arg;
  527. break;
  528. case FILTER_ARG_NUM:
  529. if (arg->type == FILTER_ARG_OP)
  530. arg = rotate_op_right(arg, op);
  531. /* left arg of compares must be a field */
  532. if (arg->type != FILTER_ARG_FIELD &&
  533. arg->type != FILTER_ARG_BOOLEAN)
  534. return TEP_ERRNO__INVALID_ARG_TYPE;
  535. op->num.left = arg;
  536. break;
  537. default:
  538. return TEP_ERRNO__INVALID_ARG_TYPE;
  539. }
  540. return 0;
  541. }
  542. enum op_type {
  543. OP_NONE,
  544. OP_BOOL,
  545. OP_NOT,
  546. OP_EXP,
  547. OP_CMP,
  548. };
  549. static enum op_type process_op(const char *token,
  550. enum filter_op_type *btype,
  551. enum filter_cmp_type *ctype,
  552. enum filter_exp_type *etype)
  553. {
  554. *btype = FILTER_OP_NOT;
  555. *etype = FILTER_EXP_NONE;
  556. *ctype = FILTER_CMP_NONE;
  557. if (strcmp(token, "&&") == 0)
  558. *btype = FILTER_OP_AND;
  559. else if (strcmp(token, "||") == 0)
  560. *btype = FILTER_OP_OR;
  561. else if (strcmp(token, "!") == 0)
  562. return OP_NOT;
  563. if (*btype != FILTER_OP_NOT)
  564. return OP_BOOL;
  565. /* Check for value expressions */
  566. if (strcmp(token, "+") == 0) {
  567. *etype = FILTER_EXP_ADD;
  568. } else if (strcmp(token, "-") == 0) {
  569. *etype = FILTER_EXP_SUB;
  570. } else if (strcmp(token, "*") == 0) {
  571. *etype = FILTER_EXP_MUL;
  572. } else if (strcmp(token, "/") == 0) {
  573. *etype = FILTER_EXP_DIV;
  574. } else if (strcmp(token, "%") == 0) {
  575. *etype = FILTER_EXP_MOD;
  576. } else if (strcmp(token, ">>") == 0) {
  577. *etype = FILTER_EXP_RSHIFT;
  578. } else if (strcmp(token, "<<") == 0) {
  579. *etype = FILTER_EXP_LSHIFT;
  580. } else if (strcmp(token, "&") == 0) {
  581. *etype = FILTER_EXP_AND;
  582. } else if (strcmp(token, "|") == 0) {
  583. *etype = FILTER_EXP_OR;
  584. } else if (strcmp(token, "^") == 0) {
  585. *etype = FILTER_EXP_XOR;
  586. } else if (strcmp(token, "~") == 0)
  587. *etype = FILTER_EXP_NOT;
  588. if (*etype != FILTER_EXP_NONE)
  589. return OP_EXP;
  590. /* Check for compares */
  591. if (strcmp(token, "==") == 0)
  592. *ctype = FILTER_CMP_EQ;
  593. else if (strcmp(token, "!=") == 0)
  594. *ctype = FILTER_CMP_NE;
  595. else if (strcmp(token, "<") == 0)
  596. *ctype = FILTER_CMP_LT;
  597. else if (strcmp(token, ">") == 0)
  598. *ctype = FILTER_CMP_GT;
  599. else if (strcmp(token, "<=") == 0)
  600. *ctype = FILTER_CMP_LE;
  601. else if (strcmp(token, ">=") == 0)
  602. *ctype = FILTER_CMP_GE;
  603. else if (strcmp(token, "=~") == 0)
  604. *ctype = FILTER_CMP_REGEX;
  605. else if (strcmp(token, "!~") == 0)
  606. *ctype = FILTER_CMP_NOT_REGEX;
  607. else
  608. return OP_NONE;
  609. return OP_CMP;
  610. }
  611. static int check_op_done(struct filter_arg *arg)
  612. {
  613. switch (arg->type) {
  614. case FILTER_ARG_EXP:
  615. return arg->exp.right != NULL;
  616. case FILTER_ARG_OP:
  617. return arg->op.right != NULL;
  618. case FILTER_ARG_NUM:
  619. return arg->num.right != NULL;
  620. case FILTER_ARG_STR:
  621. /* A string conversion is always done */
  622. return 1;
  623. case FILTER_ARG_BOOLEAN:
  624. /* field not found, is ok */
  625. return 1;
  626. default:
  627. return 0;
  628. }
  629. }
  630. enum filter_vals {
  631. FILTER_VAL_NORM,
  632. FILTER_VAL_FALSE,
  633. FILTER_VAL_TRUE,
  634. };
  635. static enum tep_errno
  636. reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,
  637. struct filter_arg *arg, char *error_str)
  638. {
  639. struct filter_arg *other_child;
  640. struct filter_arg **ptr;
  641. if (parent->type != FILTER_ARG_OP &&
  642. arg->type != FILTER_ARG_OP) {
  643. show_error(error_str, "can not reparent other than OP");
  644. return TEP_ERRNO__REPARENT_NOT_OP;
  645. }
  646. /* Get the sibling */
  647. if (old_child->op.right == arg) {
  648. ptr = &old_child->op.right;
  649. other_child = old_child->op.left;
  650. } else if (old_child->op.left == arg) {
  651. ptr = &old_child->op.left;
  652. other_child = old_child->op.right;
  653. } else {
  654. show_error(error_str, "Error in reparent op, find other child");
  655. return TEP_ERRNO__REPARENT_FAILED;
  656. }
  657. /* Detach arg from old_child */
  658. *ptr = NULL;
  659. /* Check for root */
  660. if (parent == old_child) {
  661. free_arg(other_child);
  662. *parent = *arg;
  663. /* Free arg without recussion */
  664. free(arg);
  665. return 0;
  666. }
  667. if (parent->op.right == old_child)
  668. ptr = &parent->op.right;
  669. else if (parent->op.left == old_child)
  670. ptr = &parent->op.left;
  671. else {
  672. show_error(error_str, "Error in reparent op");
  673. return TEP_ERRNO__REPARENT_FAILED;
  674. }
  675. *ptr = arg;
  676. free_arg(old_child);
  677. return 0;
  678. }
  679. /* Returns either filter_vals (success) or tep_errno (failfure) */
  680. static int test_arg(struct filter_arg *parent, struct filter_arg *arg,
  681. char *error_str)
  682. {
  683. int lval, rval;
  684. switch (arg->type) {
  685. /* bad case */
  686. case FILTER_ARG_BOOLEAN:
  687. return FILTER_VAL_FALSE + arg->boolean.value;
  688. /* good cases: */
  689. case FILTER_ARG_STR:
  690. case FILTER_ARG_VALUE:
  691. case FILTER_ARG_FIELD:
  692. return FILTER_VAL_NORM;
  693. case FILTER_ARG_EXP:
  694. lval = test_arg(arg, arg->exp.left, error_str);
  695. if (lval != FILTER_VAL_NORM)
  696. return lval;
  697. rval = test_arg(arg, arg->exp.right, error_str);
  698. if (rval != FILTER_VAL_NORM)
  699. return rval;
  700. return FILTER_VAL_NORM;
  701. case FILTER_ARG_NUM:
  702. lval = test_arg(arg, arg->num.left, error_str);
  703. if (lval != FILTER_VAL_NORM)
  704. return lval;
  705. rval = test_arg(arg, arg->num.right, error_str);
  706. if (rval != FILTER_VAL_NORM)
  707. return rval;
  708. return FILTER_VAL_NORM;
  709. case FILTER_ARG_OP:
  710. if (arg->op.type != FILTER_OP_NOT) {
  711. lval = test_arg(arg, arg->op.left, error_str);
  712. switch (lval) {
  713. case FILTER_VAL_NORM:
  714. break;
  715. case FILTER_VAL_TRUE:
  716. if (arg->op.type == FILTER_OP_OR)
  717. return FILTER_VAL_TRUE;
  718. rval = test_arg(arg, arg->op.right, error_str);
  719. if (rval != FILTER_VAL_NORM)
  720. return rval;
  721. return reparent_op_arg(parent, arg, arg->op.right,
  722. error_str);
  723. case FILTER_VAL_FALSE:
  724. if (arg->op.type == FILTER_OP_AND)
  725. return FILTER_VAL_FALSE;
  726. rval = test_arg(arg, arg->op.right, error_str);
  727. if (rval != FILTER_VAL_NORM)
  728. return rval;
  729. return reparent_op_arg(parent, arg, arg->op.right,
  730. error_str);
  731. default:
  732. return lval;
  733. }
  734. }
  735. rval = test_arg(arg, arg->op.right, error_str);
  736. switch (rval) {
  737. case FILTER_VAL_NORM:
  738. default:
  739. break;
  740. case FILTER_VAL_TRUE:
  741. if (arg->op.type == FILTER_OP_OR)
  742. return FILTER_VAL_TRUE;
  743. if (arg->op.type == FILTER_OP_NOT)
  744. return FILTER_VAL_FALSE;
  745. return reparent_op_arg(parent, arg, arg->op.left,
  746. error_str);
  747. case FILTER_VAL_FALSE:
  748. if (arg->op.type == FILTER_OP_AND)
  749. return FILTER_VAL_FALSE;
  750. if (arg->op.type == FILTER_OP_NOT)
  751. return FILTER_VAL_TRUE;
  752. return reparent_op_arg(parent, arg, arg->op.left,
  753. error_str);
  754. }
  755. return rval;
  756. default:
  757. show_error(error_str, "bad arg in filter tree");
  758. return TEP_ERRNO__BAD_FILTER_ARG;
  759. }
  760. return FILTER_VAL_NORM;
  761. }
  762. /* Remove any unknown event fields */
  763. static int collapse_tree(struct filter_arg *arg,
  764. struct filter_arg **arg_collapsed, char *error_str)
  765. {
  766. int ret;
  767. ret = test_arg(arg, arg, error_str);
  768. switch (ret) {
  769. case FILTER_VAL_NORM:
  770. break;
  771. case FILTER_VAL_TRUE:
  772. case FILTER_VAL_FALSE:
  773. free_arg(arg);
  774. arg = allocate_arg();
  775. if (arg) {
  776. arg->type = FILTER_ARG_BOOLEAN;
  777. arg->boolean.value = ret == FILTER_VAL_TRUE;
  778. } else {
  779. show_error(error_str, "Failed to allocate filter arg");
  780. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  781. }
  782. break;
  783. default:
  784. /* test_arg() already set the error_str */
  785. free_arg(arg);
  786. arg = NULL;
  787. break;
  788. }
  789. *arg_collapsed = arg;
  790. return ret;
  791. }
  792. static enum tep_errno
  793. process_filter(struct event_format *event, struct filter_arg **parg,
  794. char *error_str, int not)
  795. {
  796. enum event_type type;
  797. char *token = NULL;
  798. struct filter_arg *current_op = NULL;
  799. struct filter_arg *current_exp = NULL;
  800. struct filter_arg *left_item = NULL;
  801. struct filter_arg *arg = NULL;
  802. enum op_type op_type;
  803. enum filter_op_type btype;
  804. enum filter_exp_type etype;
  805. enum filter_cmp_type ctype;
  806. enum tep_errno ret;
  807. *parg = NULL;
  808. do {
  809. free(token);
  810. type = read_token(&token);
  811. switch (type) {
  812. case EVENT_SQUOTE:
  813. case EVENT_DQUOTE:
  814. case EVENT_ITEM:
  815. ret = create_arg_item(event, token, type, &arg, error_str);
  816. if (ret < 0)
  817. goto fail;
  818. if (!left_item)
  819. left_item = arg;
  820. else if (current_exp) {
  821. ret = add_right(current_exp, arg, error_str);
  822. if (ret < 0)
  823. goto fail;
  824. left_item = NULL;
  825. /* Not's only one one expression */
  826. if (not) {
  827. arg = NULL;
  828. if (current_op)
  829. goto fail_syntax;
  830. free(token);
  831. *parg = current_exp;
  832. return 0;
  833. }
  834. } else
  835. goto fail_syntax;
  836. arg = NULL;
  837. break;
  838. case EVENT_DELIM:
  839. if (*token == ',') {
  840. show_error(error_str, "Illegal token ','");
  841. ret = TEP_ERRNO__ILLEGAL_TOKEN;
  842. goto fail;
  843. }
  844. if (*token == '(') {
  845. if (left_item) {
  846. show_error(error_str,
  847. "Open paren can not come after item");
  848. ret = TEP_ERRNO__INVALID_PAREN;
  849. goto fail;
  850. }
  851. if (current_exp) {
  852. show_error(error_str,
  853. "Open paren can not come after expression");
  854. ret = TEP_ERRNO__INVALID_PAREN;
  855. goto fail;
  856. }
  857. ret = process_filter(event, &arg, error_str, 0);
  858. if (ret != TEP_ERRNO__UNBALANCED_PAREN) {
  859. if (ret == 0) {
  860. show_error(error_str,
  861. "Unbalanced number of '('");
  862. ret = TEP_ERRNO__UNBALANCED_PAREN;
  863. }
  864. goto fail;
  865. }
  866. ret = 0;
  867. /* A not wants just one expression */
  868. if (not) {
  869. if (current_op)
  870. goto fail_syntax;
  871. *parg = arg;
  872. return 0;
  873. }
  874. if (current_op)
  875. ret = add_right(current_op, arg, error_str);
  876. else
  877. current_exp = arg;
  878. if (ret < 0)
  879. goto fail;
  880. } else { /* ')' */
  881. if (!current_op && !current_exp)
  882. goto fail_syntax;
  883. /* Make sure everything is finished at this level */
  884. if (current_exp && !check_op_done(current_exp))
  885. goto fail_syntax;
  886. if (current_op && !check_op_done(current_op))
  887. goto fail_syntax;
  888. if (current_op)
  889. *parg = current_op;
  890. else
  891. *parg = current_exp;
  892. free(token);
  893. return TEP_ERRNO__UNBALANCED_PAREN;
  894. }
  895. break;
  896. case EVENT_OP:
  897. op_type = process_op(token, &btype, &ctype, &etype);
  898. /* All expect a left arg except for NOT */
  899. switch (op_type) {
  900. case OP_BOOL:
  901. /* Logic ops need a left expression */
  902. if (!current_exp && !current_op)
  903. goto fail_syntax;
  904. /* fall through */
  905. case OP_NOT:
  906. /* logic only processes ops and exp */
  907. if (left_item)
  908. goto fail_syntax;
  909. break;
  910. case OP_EXP:
  911. case OP_CMP:
  912. if (!left_item)
  913. goto fail_syntax;
  914. break;
  915. case OP_NONE:
  916. show_error(error_str,
  917. "Unknown op token %s", token);
  918. ret = TEP_ERRNO__UNKNOWN_TOKEN;
  919. goto fail;
  920. }
  921. ret = 0;
  922. switch (op_type) {
  923. case OP_BOOL:
  924. arg = create_arg_op(btype);
  925. if (arg == NULL)
  926. goto fail_alloc;
  927. if (current_op)
  928. ret = add_left(arg, current_op);
  929. else
  930. ret = add_left(arg, current_exp);
  931. current_op = arg;
  932. current_exp = NULL;
  933. break;
  934. case OP_NOT:
  935. arg = create_arg_op(btype);
  936. if (arg == NULL)
  937. goto fail_alloc;
  938. if (current_op)
  939. ret = add_right(current_op, arg, error_str);
  940. if (ret < 0)
  941. goto fail;
  942. current_exp = arg;
  943. ret = process_filter(event, &arg, error_str, 1);
  944. if (ret < 0)
  945. goto fail;
  946. ret = add_right(current_exp, arg, error_str);
  947. if (ret < 0)
  948. goto fail;
  949. break;
  950. case OP_EXP:
  951. case OP_CMP:
  952. if (op_type == OP_EXP)
  953. arg = create_arg_exp(etype);
  954. else
  955. arg = create_arg_cmp(ctype);
  956. if (arg == NULL)
  957. goto fail_alloc;
  958. if (current_op)
  959. ret = add_right(current_op, arg, error_str);
  960. if (ret < 0)
  961. goto fail;
  962. ret = add_left(arg, left_item);
  963. if (ret < 0) {
  964. arg = NULL;
  965. goto fail_syntax;
  966. }
  967. current_exp = arg;
  968. break;
  969. default:
  970. break;
  971. }
  972. arg = NULL;
  973. if (ret < 0)
  974. goto fail_syntax;
  975. break;
  976. case EVENT_NONE:
  977. break;
  978. case EVENT_ERROR:
  979. goto fail_alloc;
  980. default:
  981. goto fail_syntax;
  982. }
  983. } while (type != EVENT_NONE);
  984. if (!current_op && !current_exp)
  985. goto fail_syntax;
  986. if (!current_op)
  987. current_op = current_exp;
  988. ret = collapse_tree(current_op, parg, error_str);
  989. /* collapse_tree() may free current_op, and updates parg accordingly */
  990. current_op = NULL;
  991. if (ret < 0)
  992. goto fail;
  993. free(token);
  994. return 0;
  995. fail_alloc:
  996. show_error(error_str, "failed to allocate filter arg");
  997. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  998. goto fail;
  999. fail_syntax:
  1000. show_error(error_str, "Syntax error");
  1001. ret = TEP_ERRNO__SYNTAX_ERROR;
  1002. fail:
  1003. free_arg(current_op);
  1004. free_arg(current_exp);
  1005. free_arg(arg);
  1006. free(token);
  1007. return ret;
  1008. }
  1009. static enum tep_errno
  1010. process_event(struct event_format *event, const char *filter_str,
  1011. struct filter_arg **parg, char *error_str)
  1012. {
  1013. int ret;
  1014. tep_buffer_init(filter_str, strlen(filter_str));
  1015. ret = process_filter(event, parg, error_str, 0);
  1016. if (ret < 0)
  1017. return ret;
  1018. /* If parg is NULL, then make it into FALSE */
  1019. if (!*parg) {
  1020. *parg = allocate_arg();
  1021. if (*parg == NULL)
  1022. return TEP_ERRNO__MEM_ALLOC_FAILED;
  1023. (*parg)->type = FILTER_ARG_BOOLEAN;
  1024. (*parg)->boolean.value = FILTER_FALSE;
  1025. }
  1026. return 0;
  1027. }
  1028. static enum tep_errno
  1029. filter_event(struct event_filter *filter, struct event_format *event,
  1030. const char *filter_str, char *error_str)
  1031. {
  1032. struct filter_type *filter_type;
  1033. struct filter_arg *arg;
  1034. enum tep_errno ret;
  1035. if (filter_str) {
  1036. ret = process_event(event, filter_str, &arg, error_str);
  1037. if (ret < 0)
  1038. return ret;
  1039. } else {
  1040. /* just add a TRUE arg */
  1041. arg = allocate_arg();
  1042. if (arg == NULL)
  1043. return TEP_ERRNO__MEM_ALLOC_FAILED;
  1044. arg->type = FILTER_ARG_BOOLEAN;
  1045. arg->boolean.value = FILTER_TRUE;
  1046. }
  1047. filter_type = add_filter_type(filter, event->id);
  1048. if (filter_type == NULL) {
  1049. free_arg(arg);
  1050. return TEP_ERRNO__MEM_ALLOC_FAILED;
  1051. }
  1052. if (filter_type->filter)
  1053. free_arg(filter_type->filter);
  1054. filter_type->filter = arg;
  1055. return 0;
  1056. }
  1057. static void filter_init_error_buf(struct event_filter *filter)
  1058. {
  1059. /* clear buffer to reset show error */
  1060. tep_buffer_init("", 0);
  1061. filter->error_buffer[0] = '\0';
  1062. }
  1063. /**
  1064. * tep_filter_add_filter_str - add a new filter
  1065. * @filter: the event filter to add to
  1066. * @filter_str: the filter string that contains the filter
  1067. *
  1068. * Returns 0 if the filter was successfully added or a
  1069. * negative error code. Use tep_filter_strerror() to see
  1070. * actual error message in case of error.
  1071. */
  1072. enum tep_errno tep_filter_add_filter_str(struct event_filter *filter,
  1073. const char *filter_str)
  1074. {
  1075. struct tep_handle *pevent = filter->pevent;
  1076. struct event_list *event;
  1077. struct event_list *events = NULL;
  1078. const char *filter_start;
  1079. const char *next_event;
  1080. char *this_event;
  1081. char *event_name = NULL;
  1082. char *sys_name = NULL;
  1083. char *sp;
  1084. enum tep_errno rtn = 0; /* TEP_ERRNO__SUCCESS */
  1085. int len;
  1086. int ret;
  1087. filter_init_error_buf(filter);
  1088. filter_start = strchr(filter_str, ':');
  1089. if (filter_start)
  1090. len = filter_start - filter_str;
  1091. else
  1092. len = strlen(filter_str);
  1093. do {
  1094. next_event = strchr(filter_str, ',');
  1095. if (next_event &&
  1096. (!filter_start || next_event < filter_start))
  1097. len = next_event - filter_str;
  1098. else if (filter_start)
  1099. len = filter_start - filter_str;
  1100. else
  1101. len = strlen(filter_str);
  1102. this_event = malloc(len + 1);
  1103. if (this_event == NULL) {
  1104. /* This can only happen when events is NULL, but still */
  1105. free_events(events);
  1106. return TEP_ERRNO__MEM_ALLOC_FAILED;
  1107. }
  1108. memcpy(this_event, filter_str, len);
  1109. this_event[len] = 0;
  1110. if (next_event)
  1111. next_event++;
  1112. filter_str = next_event;
  1113. sys_name = strtok_r(this_event, "/", &sp);
  1114. event_name = strtok_r(NULL, "/", &sp);
  1115. if (!sys_name) {
  1116. /* This can only happen when events is NULL, but still */
  1117. free_events(events);
  1118. free(this_event);
  1119. return TEP_ERRNO__FILTER_NOT_FOUND;
  1120. }
  1121. /* Find this event */
  1122. ret = find_event(pevent, &events, strim(sys_name), strim(event_name));
  1123. if (ret < 0) {
  1124. free_events(events);
  1125. free(this_event);
  1126. return ret;
  1127. }
  1128. free(this_event);
  1129. } while (filter_str);
  1130. /* Skip the ':' */
  1131. if (filter_start)
  1132. filter_start++;
  1133. /* filter starts here */
  1134. for (event = events; event; event = event->next) {
  1135. ret = filter_event(filter, event->event, filter_start,
  1136. filter->error_buffer);
  1137. /* Failures are returned if a parse error happened */
  1138. if (ret < 0)
  1139. rtn = ret;
  1140. if (ret >= 0 && pevent->test_filters) {
  1141. char *test;
  1142. test = tep_filter_make_string(filter, event->event->id);
  1143. if (test) {
  1144. printf(" '%s: %s'\n", event->event->name, test);
  1145. free(test);
  1146. }
  1147. }
  1148. }
  1149. free_events(events);
  1150. if (rtn >= 0 && pevent->test_filters)
  1151. exit(0);
  1152. return rtn;
  1153. }
  1154. static void free_filter_type(struct filter_type *filter_type)
  1155. {
  1156. free_arg(filter_type->filter);
  1157. }
  1158. /**
  1159. * tep_filter_strerror - fill error message in a buffer
  1160. * @filter: the event filter contains error
  1161. * @err: the error code
  1162. * @buf: the buffer to be filled in
  1163. * @buflen: the size of the buffer
  1164. *
  1165. * Returns 0 if message was filled successfully, -1 if error
  1166. */
  1167. int tep_filter_strerror(struct event_filter *filter, enum tep_errno err,
  1168. char *buf, size_t buflen)
  1169. {
  1170. if (err <= __TEP_ERRNO__START || err >= __TEP_ERRNO__END)
  1171. return -1;
  1172. if (strlen(filter->error_buffer) > 0) {
  1173. size_t len = snprintf(buf, buflen, "%s", filter->error_buffer);
  1174. if (len > buflen)
  1175. return -1;
  1176. return 0;
  1177. }
  1178. return tep_strerror(filter->pevent, err, buf, buflen);
  1179. }
  1180. /**
  1181. * tep_filter_remove_event - remove a filter for an event
  1182. * @filter: the event filter to remove from
  1183. * @event_id: the event to remove a filter for
  1184. *
  1185. * Removes the filter saved for an event defined by @event_id
  1186. * from the @filter.
  1187. *
  1188. * Returns 1: if an event was removed
  1189. * 0: if the event was not found
  1190. */
  1191. int tep_filter_remove_event(struct event_filter *filter,
  1192. int event_id)
  1193. {
  1194. struct filter_type *filter_type;
  1195. unsigned long len;
  1196. if (!filter->filters)
  1197. return 0;
  1198. filter_type = find_filter_type(filter, event_id);
  1199. if (!filter_type)
  1200. return 0;
  1201. free_filter_type(filter_type);
  1202. /* The filter_type points into the event_filters array */
  1203. len = (unsigned long)(filter->event_filters + filter->filters) -
  1204. (unsigned long)(filter_type + 1);
  1205. memmove(filter_type, filter_type + 1, len);
  1206. filter->filters--;
  1207. memset(&filter->event_filters[filter->filters], 0,
  1208. sizeof(*filter_type));
  1209. return 1;
  1210. }
  1211. /**
  1212. * tep_filter_reset - clear all filters in a filter
  1213. * @filter: the event filter to reset
  1214. *
  1215. * Removes all filters from a filter and resets it.
  1216. */
  1217. void tep_filter_reset(struct event_filter *filter)
  1218. {
  1219. int i;
  1220. for (i = 0; i < filter->filters; i++)
  1221. free_filter_type(&filter->event_filters[i]);
  1222. free(filter->event_filters);
  1223. filter->filters = 0;
  1224. filter->event_filters = NULL;
  1225. }
  1226. void tep_filter_free(struct event_filter *filter)
  1227. {
  1228. tep_unref(filter->pevent);
  1229. tep_filter_reset(filter);
  1230. free(filter);
  1231. }
  1232. static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg);
  1233. static int copy_filter_type(struct event_filter *filter,
  1234. struct event_filter *source,
  1235. struct filter_type *filter_type)
  1236. {
  1237. struct filter_arg *arg;
  1238. struct event_format *event;
  1239. const char *sys;
  1240. const char *name;
  1241. char *str;
  1242. /* Can't assume that the pevent's are the same */
  1243. sys = filter_type->event->system;
  1244. name = filter_type->event->name;
  1245. event = tep_find_event_by_name(filter->pevent, sys, name);
  1246. if (!event)
  1247. return -1;
  1248. str = arg_to_str(source, filter_type->filter);
  1249. if (!str)
  1250. return -1;
  1251. if (strcmp(str, "TRUE") == 0 || strcmp(str, "FALSE") == 0) {
  1252. /* Add trivial event */
  1253. arg = allocate_arg();
  1254. if (arg == NULL) {
  1255. free(str);
  1256. return -1;
  1257. }
  1258. arg->type = FILTER_ARG_BOOLEAN;
  1259. if (strcmp(str, "TRUE") == 0)
  1260. arg->boolean.value = 1;
  1261. else
  1262. arg->boolean.value = 0;
  1263. filter_type = add_filter_type(filter, event->id);
  1264. if (filter_type == NULL) {
  1265. free(str);
  1266. free_arg(arg);
  1267. return -1;
  1268. }
  1269. filter_type->filter = arg;
  1270. free(str);
  1271. return 0;
  1272. }
  1273. filter_event(filter, event, str, NULL);
  1274. free(str);
  1275. return 0;
  1276. }
  1277. /**
  1278. * tep_filter_copy - copy a filter using another filter
  1279. * @dest - the filter to copy to
  1280. * @source - the filter to copy from
  1281. *
  1282. * Returns 0 on success and -1 if not all filters were copied
  1283. */
  1284. int tep_filter_copy(struct event_filter *dest, struct event_filter *source)
  1285. {
  1286. int ret = 0;
  1287. int i;
  1288. tep_filter_reset(dest);
  1289. for (i = 0; i < source->filters; i++) {
  1290. if (copy_filter_type(dest, source, &source->event_filters[i]))
  1291. ret = -1;
  1292. }
  1293. return ret;
  1294. }
  1295. /**
  1296. * tep_update_trivial - update the trivial filters with the given filter
  1297. * @dest - the filter to update
  1298. * @source - the filter as the source of the update
  1299. * @type - the type of trivial filter to update.
  1300. *
  1301. * Scan dest for trivial events matching @type to replace with the source.
  1302. *
  1303. * Returns 0 on success and -1 if there was a problem updating, but
  1304. * events may have still been updated on error.
  1305. */
  1306. int tep_update_trivial(struct event_filter *dest, struct event_filter *source,
  1307. enum filter_trivial_type type)
  1308. {
  1309. struct tep_handle *src_pevent;
  1310. struct tep_handle *dest_pevent;
  1311. struct event_format *event;
  1312. struct filter_type *filter_type;
  1313. struct filter_arg *arg;
  1314. char *str;
  1315. int i;
  1316. src_pevent = source->pevent;
  1317. dest_pevent = dest->pevent;
  1318. /* Do nothing if either of the filters has nothing to filter */
  1319. if (!dest->filters || !source->filters)
  1320. return 0;
  1321. for (i = 0; i < dest->filters; i++) {
  1322. filter_type = &dest->event_filters[i];
  1323. arg = filter_type->filter;
  1324. if (arg->type != FILTER_ARG_BOOLEAN)
  1325. continue;
  1326. if ((arg->boolean.value && type == FILTER_TRIVIAL_FALSE) ||
  1327. (!arg->boolean.value && type == FILTER_TRIVIAL_TRUE))
  1328. continue;
  1329. event = filter_type->event;
  1330. if (src_pevent != dest_pevent) {
  1331. /* do a look up */
  1332. event = tep_find_event_by_name(src_pevent,
  1333. event->system,
  1334. event->name);
  1335. if (!event)
  1336. return -1;
  1337. }
  1338. str = tep_filter_make_string(source, event->id);
  1339. if (!str)
  1340. continue;
  1341. /* Don't bother if the filter is trivial too */
  1342. if (strcmp(str, "TRUE") != 0 && strcmp(str, "FALSE") != 0)
  1343. filter_event(dest, event, str, NULL);
  1344. free(str);
  1345. }
  1346. return 0;
  1347. }
  1348. /**
  1349. * tep_filter_clear_trivial - clear TRUE and FALSE filters
  1350. * @filter: the filter to remove trivial filters from
  1351. * @type: remove only true, false, or both
  1352. *
  1353. * Removes filters that only contain a TRUE or FALES boolean arg.
  1354. *
  1355. * Returns 0 on success and -1 if there was a problem.
  1356. */
  1357. int tep_filter_clear_trivial(struct event_filter *filter,
  1358. enum filter_trivial_type type)
  1359. {
  1360. struct filter_type *filter_type;
  1361. int count = 0;
  1362. int *ids = NULL;
  1363. int i;
  1364. if (!filter->filters)
  1365. return 0;
  1366. /*
  1367. * Two steps, first get all ids with trivial filters.
  1368. * then remove those ids.
  1369. */
  1370. for (i = 0; i < filter->filters; i++) {
  1371. int *new_ids;
  1372. filter_type = &filter->event_filters[i];
  1373. if (filter_type->filter->type != FILTER_ARG_BOOLEAN)
  1374. continue;
  1375. switch (type) {
  1376. case FILTER_TRIVIAL_FALSE:
  1377. if (filter_type->filter->boolean.value)
  1378. continue;
  1379. break;
  1380. case FILTER_TRIVIAL_TRUE:
  1381. if (!filter_type->filter->boolean.value)
  1382. continue;
  1383. default:
  1384. break;
  1385. }
  1386. new_ids = realloc(ids, sizeof(*ids) * (count + 1));
  1387. if (!new_ids) {
  1388. free(ids);
  1389. return -1;
  1390. }
  1391. ids = new_ids;
  1392. ids[count++] = filter_type->event_id;
  1393. }
  1394. if (!count)
  1395. return 0;
  1396. for (i = 0; i < count; i++)
  1397. tep_filter_remove_event(filter, ids[i]);
  1398. free(ids);
  1399. return 0;
  1400. }
  1401. /**
  1402. * tep_filter_event_has_trivial - return true event contains trivial filter
  1403. * @filter: the filter with the information
  1404. * @event_id: the id of the event to test
  1405. * @type: trivial type to test for (TRUE, FALSE, EITHER)
  1406. *
  1407. * Returns 1 if the event contains a matching trivial type
  1408. * otherwise 0.
  1409. */
  1410. int tep_filter_event_has_trivial(struct event_filter *filter,
  1411. int event_id,
  1412. enum filter_trivial_type type)
  1413. {
  1414. struct filter_type *filter_type;
  1415. if (!filter->filters)
  1416. return 0;
  1417. filter_type = find_filter_type(filter, event_id);
  1418. if (!filter_type)
  1419. return 0;
  1420. if (filter_type->filter->type != FILTER_ARG_BOOLEAN)
  1421. return 0;
  1422. switch (type) {
  1423. case FILTER_TRIVIAL_FALSE:
  1424. return !filter_type->filter->boolean.value;
  1425. case FILTER_TRIVIAL_TRUE:
  1426. return filter_type->filter->boolean.value;
  1427. default:
  1428. return 1;
  1429. }
  1430. }
  1431. static int test_filter(struct event_format *event, struct filter_arg *arg,
  1432. struct tep_record *record, enum tep_errno *err);
  1433. static const char *
  1434. get_comm(struct event_format *event, struct tep_record *record)
  1435. {
  1436. const char *comm;
  1437. int pid;
  1438. pid = tep_data_pid(event->pevent, record);
  1439. comm = tep_data_comm_from_pid(event->pevent, pid);
  1440. return comm;
  1441. }
  1442. static unsigned long long
  1443. get_value(struct event_format *event,
  1444. struct format_field *field, struct tep_record *record)
  1445. {
  1446. unsigned long long val;
  1447. /* Handle our dummy "comm" field */
  1448. if (field == &comm) {
  1449. const char *name;
  1450. name = get_comm(event, record);
  1451. return (unsigned long)name;
  1452. }
  1453. /* Handle our dummy "cpu" field */
  1454. if (field == &cpu)
  1455. return record->cpu;
  1456. tep_read_number_field(field, record->data, &val);
  1457. if (!(field->flags & FIELD_IS_SIGNED))
  1458. return val;
  1459. switch (field->size) {
  1460. case 1:
  1461. return (char)val;
  1462. case 2:
  1463. return (short)val;
  1464. case 4:
  1465. return (int)val;
  1466. case 8:
  1467. return (long long)val;
  1468. }
  1469. return val;
  1470. }
  1471. static unsigned long long
  1472. get_arg_value(struct event_format *event, struct filter_arg *arg,
  1473. struct tep_record *record, enum tep_errno *err);
  1474. static unsigned long long
  1475. get_exp_value(struct event_format *event, struct filter_arg *arg,
  1476. struct tep_record *record, enum tep_errno *err)
  1477. {
  1478. unsigned long long lval, rval;
  1479. lval = get_arg_value(event, arg->exp.left, record, err);
  1480. rval = get_arg_value(event, arg->exp.right, record, err);
  1481. if (*err) {
  1482. /*
  1483. * There was an error, no need to process anymore.
  1484. */
  1485. return 0;
  1486. }
  1487. switch (arg->exp.type) {
  1488. case FILTER_EXP_ADD:
  1489. return lval + rval;
  1490. case FILTER_EXP_SUB:
  1491. return lval - rval;
  1492. case FILTER_EXP_MUL:
  1493. return lval * rval;
  1494. case FILTER_EXP_DIV:
  1495. return lval / rval;
  1496. case FILTER_EXP_MOD:
  1497. return lval % rval;
  1498. case FILTER_EXP_RSHIFT:
  1499. return lval >> rval;
  1500. case FILTER_EXP_LSHIFT:
  1501. return lval << rval;
  1502. case FILTER_EXP_AND:
  1503. return lval & rval;
  1504. case FILTER_EXP_OR:
  1505. return lval | rval;
  1506. case FILTER_EXP_XOR:
  1507. return lval ^ rval;
  1508. case FILTER_EXP_NOT:
  1509. default:
  1510. if (!*err)
  1511. *err = TEP_ERRNO__INVALID_EXP_TYPE;
  1512. }
  1513. return 0;
  1514. }
  1515. static unsigned long long
  1516. get_arg_value(struct event_format *event, struct filter_arg *arg,
  1517. struct tep_record *record, enum tep_errno *err)
  1518. {
  1519. switch (arg->type) {
  1520. case FILTER_ARG_FIELD:
  1521. return get_value(event, arg->field.field, record);
  1522. case FILTER_ARG_VALUE:
  1523. if (arg->value.type != FILTER_NUMBER) {
  1524. if (!*err)
  1525. *err = TEP_ERRNO__NOT_A_NUMBER;
  1526. }
  1527. return arg->value.val;
  1528. case FILTER_ARG_EXP:
  1529. return get_exp_value(event, arg, record, err);
  1530. default:
  1531. if (!*err)
  1532. *err = TEP_ERRNO__INVALID_ARG_TYPE;
  1533. }
  1534. return 0;
  1535. }
  1536. static int test_num(struct event_format *event, struct filter_arg *arg,
  1537. struct tep_record *record, enum tep_errno *err)
  1538. {
  1539. unsigned long long lval, rval;
  1540. lval = get_arg_value(event, arg->num.left, record, err);
  1541. rval = get_arg_value(event, arg->num.right, record, err);
  1542. if (*err) {
  1543. /*
  1544. * There was an error, no need to process anymore.
  1545. */
  1546. return 0;
  1547. }
  1548. switch (arg->num.type) {
  1549. case FILTER_CMP_EQ:
  1550. return lval == rval;
  1551. case FILTER_CMP_NE:
  1552. return lval != rval;
  1553. case FILTER_CMP_GT:
  1554. return lval > rval;
  1555. case FILTER_CMP_LT:
  1556. return lval < rval;
  1557. case FILTER_CMP_GE:
  1558. return lval >= rval;
  1559. case FILTER_CMP_LE:
  1560. return lval <= rval;
  1561. default:
  1562. if (!*err)
  1563. *err = TEP_ERRNO__ILLEGAL_INTEGER_CMP;
  1564. return 0;
  1565. }
  1566. }
  1567. static const char *get_field_str(struct filter_arg *arg, struct tep_record *record)
  1568. {
  1569. struct event_format *event;
  1570. struct tep_handle *pevent;
  1571. unsigned long long addr;
  1572. const char *val = NULL;
  1573. unsigned int size;
  1574. char hex[64];
  1575. /* If the field is not a string convert it */
  1576. if (arg->str.field->flags & FIELD_IS_STRING) {
  1577. val = record->data + arg->str.field->offset;
  1578. size = arg->str.field->size;
  1579. if (arg->str.field->flags & FIELD_IS_DYNAMIC) {
  1580. addr = *(unsigned int *)val;
  1581. val = record->data + (addr & 0xffff);
  1582. size = addr >> 16;
  1583. }
  1584. /*
  1585. * We need to copy the data since we can't be sure the field
  1586. * is null terminated.
  1587. */
  1588. if (*(val + size - 1)) {
  1589. /* copy it */
  1590. memcpy(arg->str.buffer, val, arg->str.field->size);
  1591. /* the buffer is already NULL terminated */
  1592. val = arg->str.buffer;
  1593. }
  1594. } else {
  1595. event = arg->str.field->event;
  1596. pevent = event->pevent;
  1597. addr = get_value(event, arg->str.field, record);
  1598. if (arg->str.field->flags & (FIELD_IS_POINTER | FIELD_IS_LONG))
  1599. /* convert to a kernel symbol */
  1600. val = tep_find_function(pevent, addr);
  1601. if (val == NULL) {
  1602. /* just use the hex of the string name */
  1603. snprintf(hex, 64, "0x%llx", addr);
  1604. val = hex;
  1605. }
  1606. }
  1607. return val;
  1608. }
  1609. static int test_str(struct event_format *event, struct filter_arg *arg,
  1610. struct tep_record *record, enum tep_errno *err)
  1611. {
  1612. const char *val;
  1613. if (arg->str.field == &comm)
  1614. val = get_comm(event, record);
  1615. else
  1616. val = get_field_str(arg, record);
  1617. switch (arg->str.type) {
  1618. case FILTER_CMP_MATCH:
  1619. return strcmp(val, arg->str.val) == 0;
  1620. case FILTER_CMP_NOT_MATCH:
  1621. return strcmp(val, arg->str.val) != 0;
  1622. case FILTER_CMP_REGEX:
  1623. /* Returns zero on match */
  1624. return !regexec(&arg->str.reg, val, 0, NULL, 0);
  1625. case FILTER_CMP_NOT_REGEX:
  1626. return regexec(&arg->str.reg, val, 0, NULL, 0);
  1627. default:
  1628. if (!*err)
  1629. *err = TEP_ERRNO__ILLEGAL_STRING_CMP;
  1630. return 0;
  1631. }
  1632. }
  1633. static int test_op(struct event_format *event, struct filter_arg *arg,
  1634. struct tep_record *record, enum tep_errno *err)
  1635. {
  1636. switch (arg->op.type) {
  1637. case FILTER_OP_AND:
  1638. return test_filter(event, arg->op.left, record, err) &&
  1639. test_filter(event, arg->op.right, record, err);
  1640. case FILTER_OP_OR:
  1641. return test_filter(event, arg->op.left, record, err) ||
  1642. test_filter(event, arg->op.right, record, err);
  1643. case FILTER_OP_NOT:
  1644. return !test_filter(event, arg->op.right, record, err);
  1645. default:
  1646. if (!*err)
  1647. *err = TEP_ERRNO__INVALID_OP_TYPE;
  1648. return 0;
  1649. }
  1650. }
  1651. static int test_filter(struct event_format *event, struct filter_arg *arg,
  1652. struct tep_record *record, enum tep_errno *err)
  1653. {
  1654. if (*err) {
  1655. /*
  1656. * There was an error, no need to process anymore.
  1657. */
  1658. return 0;
  1659. }
  1660. switch (arg->type) {
  1661. case FILTER_ARG_BOOLEAN:
  1662. /* easy case */
  1663. return arg->boolean.value;
  1664. case FILTER_ARG_OP:
  1665. return test_op(event, arg, record, err);
  1666. case FILTER_ARG_NUM:
  1667. return test_num(event, arg, record, err);
  1668. case FILTER_ARG_STR:
  1669. return test_str(event, arg, record, err);
  1670. case FILTER_ARG_EXP:
  1671. case FILTER_ARG_VALUE:
  1672. case FILTER_ARG_FIELD:
  1673. /*
  1674. * Expressions, fields and values evaluate
  1675. * to true if they return non zero
  1676. */
  1677. return !!get_arg_value(event, arg, record, err);
  1678. default:
  1679. if (!*err)
  1680. *err = TEP_ERRNO__INVALID_ARG_TYPE;
  1681. return 0;
  1682. }
  1683. }
  1684. /**
  1685. * tep_event_filtered - return true if event has filter
  1686. * @filter: filter struct with filter information
  1687. * @event_id: event id to test if filter exists
  1688. *
  1689. * Returns 1 if filter found for @event_id
  1690. * otherwise 0;
  1691. */
  1692. int tep_event_filtered(struct event_filter *filter, int event_id)
  1693. {
  1694. struct filter_type *filter_type;
  1695. if (!filter->filters)
  1696. return 0;
  1697. filter_type = find_filter_type(filter, event_id);
  1698. return filter_type ? 1 : 0;
  1699. }
  1700. /**
  1701. * tep_filter_match - test if a record matches a filter
  1702. * @filter: filter struct with filter information
  1703. * @record: the record to test against the filter
  1704. *
  1705. * Returns: match result or error code (prefixed with TEP_ERRNO__)
  1706. * FILTER_MATCH - filter found for event and @record matches
  1707. * FILTER_MISS - filter found for event and @record does not match
  1708. * FILTER_NOT_FOUND - no filter found for @record's event
  1709. * NO_FILTER - if no filters exist
  1710. * otherwise - error occurred during test
  1711. */
  1712. enum tep_errno tep_filter_match(struct event_filter *filter,
  1713. struct tep_record *record)
  1714. {
  1715. struct tep_handle *pevent = filter->pevent;
  1716. struct filter_type *filter_type;
  1717. int event_id;
  1718. int ret;
  1719. enum tep_errno err = 0;
  1720. filter_init_error_buf(filter);
  1721. if (!filter->filters)
  1722. return TEP_ERRNO__NO_FILTER;
  1723. event_id = tep_data_type(pevent, record);
  1724. filter_type = find_filter_type(filter, event_id);
  1725. if (!filter_type)
  1726. return TEP_ERRNO__FILTER_NOT_FOUND;
  1727. ret = test_filter(filter_type->event, filter_type->filter, record, &err);
  1728. if (err)
  1729. return err;
  1730. return ret ? TEP_ERRNO__FILTER_MATCH : TEP_ERRNO__FILTER_MISS;
  1731. }
  1732. static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
  1733. {
  1734. char *str = NULL;
  1735. char *left = NULL;
  1736. char *right = NULL;
  1737. char *op = NULL;
  1738. int left_val = -1;
  1739. int right_val = -1;
  1740. int val;
  1741. switch (arg->op.type) {
  1742. case FILTER_OP_AND:
  1743. op = "&&";
  1744. /* fall through */
  1745. case FILTER_OP_OR:
  1746. if (!op)
  1747. op = "||";
  1748. left = arg_to_str(filter, arg->op.left);
  1749. right = arg_to_str(filter, arg->op.right);
  1750. if (!left || !right)
  1751. break;
  1752. /* Try to consolidate boolean values */
  1753. if (strcmp(left, "TRUE") == 0)
  1754. left_val = 1;
  1755. else if (strcmp(left, "FALSE") == 0)
  1756. left_val = 0;
  1757. if (strcmp(right, "TRUE") == 0)
  1758. right_val = 1;
  1759. else if (strcmp(right, "FALSE") == 0)
  1760. right_val = 0;
  1761. if (left_val >= 0) {
  1762. if ((arg->op.type == FILTER_OP_AND && !left_val) ||
  1763. (arg->op.type == FILTER_OP_OR && left_val)) {
  1764. /* Just return left value */
  1765. str = left;
  1766. left = NULL;
  1767. break;
  1768. }
  1769. if (right_val >= 0) {
  1770. /* just evaluate this. */
  1771. val = 0;
  1772. switch (arg->op.type) {
  1773. case FILTER_OP_AND:
  1774. val = left_val && right_val;
  1775. break;
  1776. case FILTER_OP_OR:
  1777. val = left_val || right_val;
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. asprintf(&str, val ? "TRUE" : "FALSE");
  1783. break;
  1784. }
  1785. }
  1786. if (right_val >= 0) {
  1787. if ((arg->op.type == FILTER_OP_AND && !right_val) ||
  1788. (arg->op.type == FILTER_OP_OR && right_val)) {
  1789. /* Just return right value */
  1790. str = right;
  1791. right = NULL;
  1792. break;
  1793. }
  1794. /* The right value is meaningless */
  1795. str = left;
  1796. left = NULL;
  1797. break;
  1798. }
  1799. asprintf(&str, "(%s) %s (%s)", left, op, right);
  1800. break;
  1801. case FILTER_OP_NOT:
  1802. op = "!";
  1803. right = arg_to_str(filter, arg->op.right);
  1804. if (!right)
  1805. break;
  1806. /* See if we can consolidate */
  1807. if (strcmp(right, "TRUE") == 0)
  1808. right_val = 1;
  1809. else if (strcmp(right, "FALSE") == 0)
  1810. right_val = 0;
  1811. if (right_val >= 0) {
  1812. /* just return the opposite */
  1813. asprintf(&str, right_val ? "FALSE" : "TRUE");
  1814. break;
  1815. }
  1816. asprintf(&str, "%s(%s)", op, right);
  1817. break;
  1818. default:
  1819. /* ?? */
  1820. break;
  1821. }
  1822. free(left);
  1823. free(right);
  1824. return str;
  1825. }
  1826. static char *val_to_str(struct event_filter *filter, struct filter_arg *arg)
  1827. {
  1828. char *str = NULL;
  1829. asprintf(&str, "%lld", arg->value.val);
  1830. return str;
  1831. }
  1832. static char *field_to_str(struct event_filter *filter, struct filter_arg *arg)
  1833. {
  1834. return strdup(arg->field.field->name);
  1835. }
  1836. static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
  1837. {
  1838. char *lstr;
  1839. char *rstr;
  1840. char *op;
  1841. char *str = NULL;
  1842. lstr = arg_to_str(filter, arg->exp.left);
  1843. rstr = arg_to_str(filter, arg->exp.right);
  1844. if (!lstr || !rstr)
  1845. goto out;
  1846. switch (arg->exp.type) {
  1847. case FILTER_EXP_ADD:
  1848. op = "+";
  1849. break;
  1850. case FILTER_EXP_SUB:
  1851. op = "-";
  1852. break;
  1853. case FILTER_EXP_MUL:
  1854. op = "*";
  1855. break;
  1856. case FILTER_EXP_DIV:
  1857. op = "/";
  1858. break;
  1859. case FILTER_EXP_MOD:
  1860. op = "%";
  1861. break;
  1862. case FILTER_EXP_RSHIFT:
  1863. op = ">>";
  1864. break;
  1865. case FILTER_EXP_LSHIFT:
  1866. op = "<<";
  1867. break;
  1868. case FILTER_EXP_AND:
  1869. op = "&";
  1870. break;
  1871. case FILTER_EXP_OR:
  1872. op = "|";
  1873. break;
  1874. case FILTER_EXP_XOR:
  1875. op = "^";
  1876. break;
  1877. default:
  1878. op = "[ERROR IN EXPRESSION TYPE]";
  1879. break;
  1880. }
  1881. asprintf(&str, "%s %s %s", lstr, op, rstr);
  1882. out:
  1883. free(lstr);
  1884. free(rstr);
  1885. return str;
  1886. }
  1887. static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
  1888. {
  1889. char *lstr;
  1890. char *rstr;
  1891. char *str = NULL;
  1892. char *op = NULL;
  1893. lstr = arg_to_str(filter, arg->num.left);
  1894. rstr = arg_to_str(filter, arg->num.right);
  1895. if (!lstr || !rstr)
  1896. goto out;
  1897. switch (arg->num.type) {
  1898. case FILTER_CMP_EQ:
  1899. op = "==";
  1900. /* fall through */
  1901. case FILTER_CMP_NE:
  1902. if (!op)
  1903. op = "!=";
  1904. /* fall through */
  1905. case FILTER_CMP_GT:
  1906. if (!op)
  1907. op = ">";
  1908. /* fall through */
  1909. case FILTER_CMP_LT:
  1910. if (!op)
  1911. op = "<";
  1912. /* fall through */
  1913. case FILTER_CMP_GE:
  1914. if (!op)
  1915. op = ">=";
  1916. /* fall through */
  1917. case FILTER_CMP_LE:
  1918. if (!op)
  1919. op = "<=";
  1920. asprintf(&str, "%s %s %s", lstr, op, rstr);
  1921. break;
  1922. default:
  1923. /* ?? */
  1924. break;
  1925. }
  1926. out:
  1927. free(lstr);
  1928. free(rstr);
  1929. return str;
  1930. }
  1931. static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)
  1932. {
  1933. char *str = NULL;
  1934. char *op = NULL;
  1935. switch (arg->str.type) {
  1936. case FILTER_CMP_MATCH:
  1937. op = "==";
  1938. /* fall through */
  1939. case FILTER_CMP_NOT_MATCH:
  1940. if (!op)
  1941. op = "!=";
  1942. /* fall through */
  1943. case FILTER_CMP_REGEX:
  1944. if (!op)
  1945. op = "=~";
  1946. /* fall through */
  1947. case FILTER_CMP_NOT_REGEX:
  1948. if (!op)
  1949. op = "!~";
  1950. asprintf(&str, "%s %s \"%s\"",
  1951. arg->str.field->name, op, arg->str.val);
  1952. break;
  1953. default:
  1954. /* ?? */
  1955. break;
  1956. }
  1957. return str;
  1958. }
  1959. static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)
  1960. {
  1961. char *str = NULL;
  1962. switch (arg->type) {
  1963. case FILTER_ARG_BOOLEAN:
  1964. asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");
  1965. return str;
  1966. case FILTER_ARG_OP:
  1967. return op_to_str(filter, arg);
  1968. case FILTER_ARG_NUM:
  1969. return num_to_str(filter, arg);
  1970. case FILTER_ARG_STR:
  1971. return str_to_str(filter, arg);
  1972. case FILTER_ARG_VALUE:
  1973. return val_to_str(filter, arg);
  1974. case FILTER_ARG_FIELD:
  1975. return field_to_str(filter, arg);
  1976. case FILTER_ARG_EXP:
  1977. return exp_to_str(filter, arg);
  1978. default:
  1979. /* ?? */
  1980. return NULL;
  1981. }
  1982. }
  1983. /**
  1984. * tep_filter_make_string - return a string showing the filter
  1985. * @filter: filter struct with filter information
  1986. * @event_id: the event id to return the filter string with
  1987. *
  1988. * Returns a string that displays the filter contents.
  1989. * This string must be freed with free(str).
  1990. * NULL is returned if no filter is found or allocation failed.
  1991. */
  1992. char *
  1993. tep_filter_make_string(struct event_filter *filter, int event_id)
  1994. {
  1995. struct filter_type *filter_type;
  1996. if (!filter->filters)
  1997. return NULL;
  1998. filter_type = find_filter_type(filter, event_id);
  1999. if (!filter_type)
  2000. return NULL;
  2001. return arg_to_str(filter, filter_type->filter);
  2002. }
  2003. /**
  2004. * tep_filter_compare - compare two filters and return if they are the same
  2005. * @filter1: Filter to compare with @filter2
  2006. * @filter2: Filter to compare with @filter1
  2007. *
  2008. * Returns:
  2009. * 1 if the two filters hold the same content.
  2010. * 0 if they do not.
  2011. */
  2012. int tep_filter_compare(struct event_filter *filter1, struct event_filter *filter2)
  2013. {
  2014. struct filter_type *filter_type1;
  2015. struct filter_type *filter_type2;
  2016. char *str1, *str2;
  2017. int result;
  2018. int i;
  2019. /* Do the easy checks first */
  2020. if (filter1->filters != filter2->filters)
  2021. return 0;
  2022. if (!filter1->filters && !filter2->filters)
  2023. return 1;
  2024. /*
  2025. * Now take a look at each of the events to see if they have the same
  2026. * filters to them.
  2027. */
  2028. for (i = 0; i < filter1->filters; i++) {
  2029. filter_type1 = &filter1->event_filters[i];
  2030. filter_type2 = find_filter_type(filter2, filter_type1->event_id);
  2031. if (!filter_type2)
  2032. break;
  2033. if (filter_type1->filter->type != filter_type2->filter->type)
  2034. break;
  2035. switch (filter_type1->filter->type) {
  2036. case FILTER_TRIVIAL_FALSE:
  2037. case FILTER_TRIVIAL_TRUE:
  2038. /* trivial types just need the type compared */
  2039. continue;
  2040. default:
  2041. break;
  2042. }
  2043. /* The best way to compare complex filters is with strings */
  2044. str1 = arg_to_str(filter1, filter_type1->filter);
  2045. str2 = arg_to_str(filter2, filter_type2->filter);
  2046. if (str1 && str2)
  2047. result = strcmp(str1, str2) != 0;
  2048. else
  2049. /* bail out if allocation fails */
  2050. result = 1;
  2051. free(str1);
  2052. free(str2);
  2053. if (result)
  2054. break;
  2055. }
  2056. if (i < filter1->filters)
  2057. return 0;
  2058. return 1;
  2059. }