parse-filter.c 52 KB

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