<commit_before>/*
 * trace_events_filter - generic event filtering
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 */

#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/ctype.h>

#include "trace.h"

static int filter_pred_64(struct filter_pred *pred, void *event)
{
	u64 *addr = (u64 *)(event + pred->offset);
	u64 val = (u64)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_32(struct filter_pred *pred, void *event)
{
	u32 *addr = (u32 *)(event + pred->offset);
	u32 val = (u32)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_16(struct filter_pred *pred, void *event)
{
	u16 *addr = (u16 *)(event + pred->offset);
	u16 val = (u16)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_8(struct filter_pred *pred, void *event)
{
	u8 *addr = (u8 *)(event + pred->offset);
	u8 val = (u8)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_string(struct filter_pred *pred, void *event)
{
	char *addr = (char *)(event + pred->offset);
	int cmp, match;

	cmp = strncmp(addr, pred->str_val, pred->str_len);

	match = (!cmp) ^ pred->not;

	return match;
}

/* return 1 if event matches, 0 otherwise (discard) */
int filter_match_preds(struct ftrace_event_call *call, void *rec)
{
	int i, matched, and_failed = 0;
	struct filter_pred *pred;

	for (i = 0; i < MAX_FILTER_PRED; i++) {
		if (call->preds[i]) {
			pred = call->preds[i];
			if (and_failed && !pred->or)
				continue;
			matched = pred->fn(pred, rec);
			if (!matched && !pred->or) {
				and_failed = 1;
				continue;
			} else if (matched && pred->or)
				return 1;
		} else
			break;
	}

	if (and_failed)
		return 0;

	return 1;
}

int filter_print_preds(struct filter_pred **preds, char *buf)
{
	ssize_t this_len = 0;
	char *field_name;
	struct filter_pred *pred;
	int i;

	if (!preds) {
		this_len += sprintf(buf + this_len, "none\n");
		return this_len;
	}

	for (i = 0; i < MAX_FILTER_PRED; i++) {
		if (preds[i]) {
			pred = preds[i];
			field_name = pred->field_name;
			if (i)
				this_len += sprintf(buf + this_len,
					    pred->or ? "|| " : "&& ");
			this_len += sprintf(buf + this_len,
					    "%s ", field_name);
			this_len += sprintf(buf + this_len,
					    pred->not ? "!= " : "== ");
			if (pred->str_val)
				this_len += sprintf(buf + this_len,
						    "%s\n", pred->str_val);
			else
				this_len += sprintf(buf + this_len,
						    "%llu\n", pred->val);
		} else
			break;
	}

	return this_len;
}

static struct ftrace_event_field *
find_event_field(struct ftrace_event_call *call, char *name)
{
	struct ftrace_event_field *field, *next;

	list_for_each_entry_safe(field, next, &call->fields, link) {
		if (!strcmp(field->name, name))
			return field;
	}<commit_msg>tracing/filters: use list_for_each_entry<commit_after>/*
 * trace_events_filter - generic event filtering
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) 2009 <NAME> <tzan<EMAIL>>
 */

#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/ctype.h>

#include "trace.h"

static int filter_pred_64(struct filter_pred *pred, void *event)
{
	u64 *addr = (u64 *)(event + pred->offset);
	u64 val = (u64)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_32(struct filter_pred *pred, void *event)
{
	u32 *addr = (u32 *)(event + pred->offset);
	u32 val = (u32)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_16(struct filter_pred *pred, void *event)
{
	u16 *addr = (u16 *)(event + pred->offset);
	u16 val = (u16)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_8(struct filter_pred *pred, void *event)
{
	u8 *addr = (u8 *)(event + pred->offset);
	u8 val = (u8)pred->val;
	int match;

	match = (val == *addr) ^ pred->not;

	return match;
}

static int filter_pred_string(struct filter_pred *pred, void *event)
{
	char *addr = (char *)(event + pred->offset);
	int cmp, match;

	cmp = strncmp(addr, pred->str_val, pred->str_len);

	match = (!cmp) ^ pred->not;

	return match;
}

/* return 1 if event matches, 0 otherwise (discard) */
int filter_match_preds(struct ftrace_event_call *call, void *rec)
{
	int i, matched, and_failed = 0;
	struct filter_pred *pred;

	for (i = 0; i < MAX_FILTER_PRED; i++) {
		if (call->preds[i]) {
			pred = call->preds[i];
			if (and_failed && !pred->or)
				continue;
			matched = pred->fn(pred, rec);
			if (!matched && !pred->or) {
				and_failed = 1;
				continue;
			} else if (matched && pred->or)
				return 1;
		} else
			break;
	}

	if (and_failed)
		return 0;

	return 1;
}

int filter_print_preds(struct filter_pred **preds, char *buf)
{
	ssize_t this_len = 0;
	char *field_name;
	struct filter_pred *pred;
	int i;

	if (!preds) {
		this_len += sprintf(buf + this_len, "none\n");
		return this_len;
	}

	for (i = 0; i < MAX_FILTER_PRED; i++) {
		if (preds[i]) {
			pred = preds[i];
			field_name = pred->field_name;
			if (i)
				this_len += sprintf(buf + this_len,
					    pred->or ? "|| " : "&& ");
			this_len += sprintf(buf + this_len,
					    "%s ", field_name);
			this_len += sprintf(buf + this_len,
					    pred->not ? "!= " : "== ");
			if (pred->str_val)
				this_len += sprintf(buf + this_len,
						    "%s\n", pred->str_val);
			else
				this_len += sprintf(buf + this_len,
						    "%llu\n", pred->val);
		} else
			break;
	}

	return this_len;
}

static struct ftrace_event_field *
find_event_field(struct ftrace_event_call *call, char *name)
{
	struct ftrace_event_field *field;

	list_for_each_entry(field, &call->fields, link) {
		if (!strcmp(field->name, name))
			return field;
	}
{'fraction_non_alphanumeric': 0.14242298660035915, 'fraction_numerical': 0.017682000276281255, 'mean_word_length': 3.536340852130326, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2957316', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2603, 'n_words': 794}