Skip to contentSkip to conversation

trace-cmd: libparsevent cleanups - part 2

PATCH Applied Namhyung Kim 2 participants 38 messages Apr 23, 2012 – Jul 6, 2012

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: libparsevent cleanups - part 2

Hi,

This is the second part of my libparsevent cleanups. The patches are based on my previous patch set [1].

Any comments are welcome.
Thanks.

[1] https://lkml.org/lkml/2012/4/8/177

Namhyung Kim (11):
parse-events: Free flag/sym field on failure path
parse-events: Fix freeing arg on process_dynamic_array()
parse-events: Get rid of handling concatenation on event_read_print
parse-events: Remove unused arg->dynarray.index
parse-events: Use proper function parameter type
parse-events: Pass string type argument to args
parse-events: Do not call add_event() again if allocation failed
parse-events: Fix some comments
parse-filter: Check result of malloc() during reading token
parse-filter: Fix signature of create_arg_item()
parse-filter: Check return value of arg_to_str()

 parse-events.c |   62 ++++++++++++++++++++++----------------------------------
 parse-events.h |    1 -
 parse-filter.c |   22 ++++++++++++++------
 3 files changed, 40 insertions(+), 45 deletions(-)
Signature · 1 line
1.7.10
#1 lore raw

PATCH 1/11 parse-events: Free flag/sym field on failure path

If parsing flag/sym field failed after allocating it, it should be freed.

---
 parse-events.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
parse-events.c +9 -5
index bd078ba..81e6581 100644
@@ -2166,10 +2166,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 
 		value = arg_eval(arg);
 		if (value == NULL)
-			goto out_free;
+			goto out_free_field;
 		field->value = strdup(value);
 		if (field->value == NULL)
-			goto out_free;
+			goto out_free_field;
 
 		free_arg(arg);
 		arg = alloc_arg();
@@ -2177,14 +2177,14 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 		free_token(token);
 		type = process_arg(event, arg, &token);
 		if (test_type_token(type, token, EVENT_OP, "}"))
-			goto out_free;
+			goto out_free_field_value;
 
 		value = arg_eval(arg);
 		if (value == NULL)
-			goto out_free;
+			goto out_free_field_value;
 		field->str = strdup(value);
 		if (field->str == NULL)
-			goto out_free;
+			goto out_free_field_value;
 		free_arg(arg);
 		arg = NULL;
 
@@ -2198,6 +2198,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 	*tok = token;
 	return type;
 
+out_free_field_value:
+	free(field->value);
+out_free_field:
+	free(field);
 out_free:
 	free_arg(arg);
 	free_token(token);
Signature · 1 line
1.7.10
#2 lore raw

Re: [PATCH 01/11] parse-events: Free flag/sym field on failure path

Quoted 13 lines · Namhyung Kim

If parsing flag/sym field failed after allocating it, it should be freed.

---
 parse-events.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
parse-events.c +0 -0
index bd078ba..81e6581 100644

parse-events.c process_fields(struct event_format *event, struct print_flag_sym **list, char ** · line 2169

Quoted 5 lines
parse-events.c +1 -1
@@ -2166,10 +2166,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 
 		value = arg_eval(arg);
 		if (value == NULL)
-			goto out_free;
+			goto out_free_field;

Instead of adding more goto's what, before the first goto "out_free" add:

	field = NULL;

and since the field is allocated and also memset to zero, we could do in the out_free:

	if (field) {
		free(field->value);
		free(field);
	}

I think that would be cleaner.

-- Steve

Quoted 36 lines
parse-events.c +8 -4
 		field->value = strdup(value);
 		if (field->value == NULL)
-			goto out_free;
+			goto out_free_field;
 
 		free_arg(arg);
 		arg = alloc_arg();
@@ -2177,14 +2177,14 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 		free_token(token);
 		type = process_arg(event, arg, &token);
 		if (test_type_token(type, token, EVENT_OP, "}"))
-			goto out_free;
+			goto out_free_field_value;
 
 		value = arg_eval(arg);
 		if (value == NULL)
-			goto out_free;
+			goto out_free_field_value;
 		field->str = strdup(value);
 		if (field->str == NULL)
-			goto out_free;
+			goto out_free_field_value;
 		free_arg(arg);
 		arg = NULL;
 
@@ -2198,6 +2198,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 	*tok = token;
 	return type;
 
+out_free_field_value:
+	free(field->value);
+out_free_field:
+	free(field);
 out_free:
 	free_arg(arg);
 	free_token(token);
#3 lore raw

Re: [PATCH 01/11] parse-events: Free flag/sym field on failure path

Hi,

Quoted 28 lines · Steven Rostedt
Quoted 13 lines · Namhyung Kim

If parsing flag/sym field failed after allocating it, it should be freed.

---
 parse-events.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
parse-events.c +0 -0
index bd078ba..81e6581 100644

parse-events.c process_fields(struct event_format *event, struct print_flag_sym **list, char ** · line 2169

Quoted 5 lines
parse-events.c +1 -1
@@ -2166,10 +2166,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 
 		value = arg_eval(arg);
 		if (value == NULL)
-			goto out_free;
+			goto out_free_field;

Instead of adding more goto's what, before the first goto "out_free" add:

	field = NULL;

and since the field is allocated and also memset to zero, we could do in the out_free:

Quoted 9 lines
	if (field) {
		free(field->value);
		free(field);
	}

I think that would be cleaner.

-- Steve

OK, will resend v2.

Thanks,
Namhyung

#4 lore raw

PATCH v2 1/1 parse-events: Free flag/sym field on failure path

If parsing flag/sym field failed after allocating it, it should be freed. As Steven advises, make the field NULL and check it at out_free instead of adding more goto labels.

---
 parse-events.c |    5 +++++
 1 file changed, 5 insertions(+)
parse-events.c +5 -0
index bd078ba..3ccedd9 100644
@@ -2147,6 +2147,7 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 		if (test_type_token(type, token, EVENT_OP, "{"))
 			break;
 
+		field = NULL;
 		arg = alloc_arg();
 
 		free_token(token);
@@ -2199,6 +2200,10 @@ process_fields(struct event_format *event, struct print_flag_sym **list, char **
 	return type;
 
 out_free:
+	if (field) {
+		free(field->value);
+		free(field);
+	}
 	free_arg(arg);
 	free_token(token);
 	*tok = NULL;
Signature · 1 line
1.7.10
#5 lore raw

PATCH 2/11 parse-events: Fix freeing arg on process_dynamic_array()

In reply to Namhyung Kim

The @arg paremeter should not be freed inside of process_XXX(), because it'd be freed from the caller of process_arg(). We can free it only after it was reused for local usage.

---
 parse-events.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
parse-events.c +4 -3
index 81e6581..0cd79c5 100644
@@ -2330,17 +2330,18 @@ process_dynamic_array(struct event_format *event, struct print_arg *arg, char **
 	arg = alloc_arg();
 	type = process_arg(event, arg, &token);
 	if (type == EVENT_ERROR)
-		goto out_free;
+		goto out_free_arg;
 
 	if (!test_type_token(type, token, EVENT_OP, "]"))
-		goto out_free;
+		goto out_free_arg;
 
 	free_token(token);
 	type = read_token_item(tok);
 	return type;
 
+ out_free_arg:
+	free_arg(arg);
  out_free:
-	free(arg);
 	free_token(token);
 	*tok = NULL;
 	return EVENT_ERROR;
Signature · 1 line
1.7.10
#6 lore raw

Re: [PATCH 02/11] parse-events: Fix freeing arg on process_dynamic_array()

Quoted 5 lines · Namhyung Kim

The @arg paremeter should not be freed inside of process_XXX(), because it'd be freed from the caller of process_arg(). We can free it only after it was reused for local usage.

Applied, thanks!

-- Steve

#7 lore raw

PATCH 3/11 parse-events: Get rid of handling concatenation on event_read_print

In reply to Namhyung Kim

The concatenation of subsequent DQUOTE'd strings is handled by __read_token() already.

---
 parse-events.c |   16 ----------------
 1 file changed, 16 deletions(-)
parse-events.c +0 -16
index 0cd79c5..b4ae313 100644
@@ -2695,7 +2695,6 @@ static int event_read_print(struct event_format *event)
 	if (read_expect_type(EVENT_DQUOTE, &token) < 0)
 		goto fail;
 
- concat:
 	event->print_fmt.format = token;
 	event->print_fmt.args = NULL;
 
@@ -2705,21 +2704,6 @@ static int event_read_print(struct event_format *event)
 	if (type == EVENT_NONE)
 		return 0;
 
-	/* Handle concatenation of print lines */
-	if (type == EVENT_DQUOTE) {
-		char *cat;
-
-		cat = malloc_or_die(strlen(event->print_fmt.format) +
-				    strlen(token) + 1);
-		strcpy(cat, event->print_fmt.format);
-		strcat(cat, token);
-		free_token(token);
-		free_token(event->print_fmt.format);
-		event->print_fmt.format = NULL;
-		token = cat;
-		goto concat;
-	}
-			     
 	if (test_type_token(type, token, EVENT_DELIM, ","))
 		goto fail;
 
Signature · 1 line
1.7.10
#8 lore raw

Re: [PATCH 03/11] parse-events: Get rid of handling concatenation on event_read_print

Quoted 3 lines · Namhyung Kim

The concatenation of subsequent DQUOTE'd strings is handled by __read_token() already.

You missed the point of this concat. It's not connecting "xxx" it is connecting "xxx" "yyy" into "xxxyyy", as some events do this.

-- Steve

Quoted 39 lines
---
 parse-events.c |   16 ----------------
 1 file changed, 16 deletions(-)
parse-events.c +0 -16
index 0cd79c5..b4ae313 100644
@@ -2695,7 +2695,6 @@ static int event_read_print(struct event_format *event)
 	if (read_expect_type(EVENT_DQUOTE, &token) < 0)
 		goto fail;
 
- concat:
 	event->print_fmt.format = token;
 	event->print_fmt.args = NULL;
 
@@ -2705,21 +2704,6 @@ static int event_read_print(struct event_format *event)
 	if (type == EVENT_NONE)
 		return 0;
 
-	/* Handle concatenation of print lines */
-	if (type == EVENT_DQUOTE) {
-		char *cat;
-
-		cat = malloc_or_die(strlen(event->print_fmt.format) +
-				    strlen(token) + 1);
-		strcpy(cat, event->print_fmt.format);
-		strcat(cat, token);
-		free_token(token);
-		free_token(event->print_fmt.format);
-		event->print_fmt.format = NULL;
-		token = cat;
-		goto concat;
-	}
-			     
 	if (test_type_token(type, token, EVENT_DELIM, ","))
 		goto fail;
 
#9 lore raw

Re: [PATCH 03/11] parse-events: Get rid of handling concatenation on event_read_print

Hi,

Quoted 8 lines · Steven Rostedt
Quoted 3 lines · Namhyung Kim

The concatenation of subsequent DQUOTE'd strings is handled by __read_token() already.

You missed the point of this concat. It's not connecting "xxx" it is connecting "xxx" "yyy" into "xxxyyy", as some events do this.

I think I don't :). Please see commit 0f15820 ("parse-events:
Concatinate two strings that are adjacent").

Thanks,
Namhyung

#10 lore raw

Re: [PATCH 03/11] parse-events: Get rid of handling concatenation on event_read_print

Quoted 8 lines · Namhyung Kim

Hi,

Quoted 5 lines · Steven Rostedt
Quoted 3 lines · Namhyung Kim

The concatenation of subsequent DQUOTE'd strings is handled by __read_token() already.

Quoted 6 lines
Quoted 3 lines

You missed the point of this concat. It's not connecting "xxx" it is connecting "xxx" "yyy" into "xxxyyy", as some events do this.

I think I don't :). Please see commit 0f15820 ("parse-events:
Concatinate two strings that are adjacent").

Heh, OK, I remember doing it for the print statement, but I guess I needed to do it for the token too. As the commits for both are almost identical. As the print was first, it must not have been enough, and I moved it to the token, but kept the first concat there.

I still want to hold off on this patch just to make sure there's not something else that was missed.

Thanks,

-- Steve

#11 lore raw

PATCH 4/11 parse-events: Remove unused arg->dynarray.index

In reply to Namhyung Kim

It seems index field of struct print_arg_dynarray is not used. Let's remove it.

---
 parse-events.c |    4 ----
 parse-events.h |    1 -
 2 files changed, 5 deletions(-)
parse-events.c +0 -4
index b4ae313..3e5ac44 100644
@@ -706,9 +706,6 @@ static void free_arg(struct print_arg *arg)
 	case PRINT_BSTRING:
 		free(arg->string.string);
 		break;
-	case PRINT_DYNAMIC_ARRAY:
-		free(arg->dynarray.index);
-		break;
 	case PRINT_OP:
 		free(arg->op.op);
 		free_arg(arg->op.left);
@@ -2315,7 +2312,6 @@ process_dynamic_array(struct event_format *event, struct print_arg *arg, char **
 		goto out_free;
 
 	arg->dynarray.field = field;
-	arg->dynarray.index = 0;
 
 	if (read_expected(EVENT_DELIM, ")") < 0)
 		goto out_free;
parse-events.h +0 -1
index 67e7886..960e598 100644
@@ -226,7 +226,6 @@ struct print_arg_symbol {
 
 struct print_arg_dynarray {
 	struct format_field	*field;
-	struct print_arg	*index;
 };
 
 struct print_arg;
Signature · 1 line
1.7.10
#12 lore raw

Re: [PATCH 04/11] parse-events: Remove unused arg->dynarray.index

Quoted 2 lines · Namhyung Kim

It seems index field of struct print_arg_dynarray is not used. Let's remove it.

Actually, the "non-use" looks like a bug. Where it is set to 0 (it should be set to NULL), an 'arg' is allocated, but it looks to be what index wants.

The array, may be allocated with a set size, and we may use this info later.

Looks to be unfinished code, that I started but never got to finish.

-- Steve

#13 lore raw

PATCH 5/11 parse-events: Use proper function parameter type

In reply to Namhyung Kim

The param needs to be updated when setting args up so that the loop in process_defined_func() can see the correct param->type for the farg.

---
 parse-events.c |    1 +
 1 file changed, 1 insertion(+)
parse-events.c +1 -0
index 3e5ac44..b7c81d8 100644
@@ -3396,6 +3396,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,
 			break;
 		}
 		farg = farg->next;
+		param = param->next;
 	}
 
 	ret = (*func_handle->func)(s, args);
Signature · 1 line
1.7.10
#14 lore raw

Re: [PATCH 05/11] parse-events: Use proper function parameter type

Quoted 3 lines · Namhyung Kim

The param needs to be updated when setting args up so that the loop in process_defined_func() can see the correct param->type for the farg.

Ouch!

Applied, thanks!

-- Steve

Quoted 18 lines
---
 parse-events.c |    1 +
 1 file changed, 1 insertion(+)
parse-events.c +1 -0
index 3e5ac44..b7c81d8 100644
@@ -3396,6 +3396,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,
 			break;
 		}
 		farg = farg->next;
+		param = param->next;
 	}
 
 	ret = (*func_handle->func)(s, args);
#15 lore raw

PATCH 6/11 parse-events: Pass string type argument to args

In reply to Namhyung Kim

It seems PEVENT_FUNC_ARG_STRING missed passing the allocated string to the args array. Fix it.

---
 parse-events.c |    1 +
 1 file changed, 1 insertion(+)
parse-events.c +1 -0
index b7c81d8..3b39923 100644
@@ -3384,6 +3384,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,
 			if (!string->str)
 				die("malloc str");
 
+			args[i] = (unsigned long long)string->str;
 			strings = string;
 			trace_seq_destroy(&str);
 			break;
Signature · 1 line
1.7.10
#16 lore raw

Re: [PATCH 06/11] parse-events: Pass string type argument to args

Quoted 2 lines · Namhyung Kim

It seems PEVENT_FUNC_ARG_STRING missed passing the allocated string to the args array. Fix it.

Another Ouch!

Applied, thanks!

-- Steve

Quoted 18 lines
---
 parse-events.c |    1 +
 1 file changed, 1 insertion(+)
parse-events.c +1 -0
index b7c81d8..3b39923 100644
@@ -3384,6 +3384,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,
 			if (!string->str)
 				die("malloc str");
 
+			args[i] = (unsigned long long)string->str;
 			strings = string;
 			trace_seq_destroy(&str);
 			break;
#17 lore raw

[tip:perf/core] tools lib traceevent: Pass string type argument to args

In reply to Namhyung Kim
Commit-ID:  3831a42deba59bf26618b0dd6fa1320a0a94ebd9
Gitweb:     http://git.kernel.org/tip/3831a42deba59bf26618b0dd6fa1320a0a94ebd9
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 23 Apr 2012 13:58:33 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Pass string type argument to args

It seems PEVENT_FUNC_ARG_STRING missed passing the allocated string to the args array. Fix it.

Cc Frederic Weisbecker <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/lib/traceevent/event-parse.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
tools/lib/traceevent/event-parse.c +1 -0
index 05eb6b4..337ca02 100644
@@ -3489,6 +3489,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,
 			if (!string->str)
 				die("malloc str");
 
+			args[i] = (unsigned long long)string->str;
 			strings = string;
 			trace_seq_destroy(&str);
 			break;
#18 lore raw

PATCH 7/11 parse-events: Do not call add_event() again if allocation failed

In reply to Namhyung Kim

When memory allocation for the field name is failed, do not goto event_failed since we added the event already.

---
 parse-events.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
parse-events.c +2 -1
index 3b39923..52cf331 100644
@@ -4637,7 +4637,8 @@ int pevent_parse_event(struct pevent *pevent,
 			arg->field.name = strdup(field->name);
 			if (!arg->field.name) {
 				do_warning("failed to allocate field name");
-				goto event_failed;
+				event->flags |= EVENT_FL_FAILED;
+				return -1;
 			}
 			arg->field.field = field;
 		}
Signature · 1 line
1.7.10
#19 lore raw

Re: [PATCH 07/11] parse-events: Do not call add_event() again if allocation failed

Quoted 4 lines · Namhyung Kim

When memory allocation for the field name is failed, do not goto event_failed since we added the event already.

Applied, Thanks!

-- Steve

#20 lore raw

[tip:perf/core] tools lib traceevent: Do not call add_event() again if allocation failed

In reply to Namhyung Kim
Commit-ID:  4b5632bc3122ec9b7e875294c8abe92f1573196a
Gitweb:     http://git.kernel.org/tip/4b5632bc3122ec9b7e875294c8abe92f1573196a
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 23 Apr 2012 13:58:34 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Do not call add_event() again if allocation failed

When memory allocation for the field name is failed, do not goto event_failed since we added the event already.

Cc Frederic Weisbecker <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/lib/traceevent/event-parse.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
tools/lib/traceevent/event-parse.c +2 -1
index 337ca02..99b0cd4 100644
@@ -4770,7 +4770,8 @@ int pevent_parse_event(struct pevent *pevent,
 			arg->field.name = strdup(field->name);
 			if (!arg->field.name) {
 				do_warning("failed to allocate field name");
-				goto event_failed;
+				event->flags |= EVENT_FL_FAILED;
+				return -1;
 			}
 			arg->field.field = field;
 		}
#21 lore raw

PATCH 8/11 parse-events: Fix some comments

In reply to Namhyung Kim

Update and add missing argument descriptions and fix some typo on function comments.

---
 parse-events.c |   16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
parse-events.c +7 -9
index 52cf331..0d785c2 100644
@@ -3920,8 +3920,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event
  * pevent_data_lat_fmt - parse the data for the latency format
  * @pevent: a handle to the pevent
  * @s: the trace_seq to write to
- * @data: the raw data to read from
- * @size: currently unused.
+ * @record: the record to read from
  *
  * This parses out the Latency format (interrupts disabled,
  * need rescheduling, in hard/soft interrupt, preempt count
@@ -4056,10 +4055,7 @@ const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  * pevent_data_comm_from_pid - parse the data into the print format
  * @s: the trace_seq to write to
  * @event: the handle to the event
- * @cpu: the cpu the event was recorded on
- * @data: the raw data
- * @size: the size of the raw data
- * @nsecs: the timestamp of the event
+ * @record: the record to read from
  *
  * This parses the raw @data using the given @event information and
  * writes the print format into the trace_seq.
@@ -4811,7 +4807,7 @@ int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  * @record: The record with the field name.
  * @err: print default error if failed.
  *
- * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
+ * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  */
 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
 			   struct event_format *event, const char *name,
@@ -4853,11 +4849,12 @@ static void free_func_handle(struct pevent_function_handler *func)
  * pevent_register_print_function - register a helper function
  * @pevent: the handle to the pevent
  * @func: the function to process the helper function
+ * @ret_type: the return type of the helper function
  * @name: the name of the helper function
  * @parameters: A list of enum pevent_func_arg_type
  *
  * Some events may have helper functions in the print format arguments.
- * This allows a plugin to dynmically create a way to process one
+ * This allows a plugin to dynamically create a way to process one
  * of these functions.
  *
  * The @parameters is a variable list of pevent_func_arg_type enums that
@@ -4928,12 +4925,13 @@ int pevent_register_print_function(struct pevent *pevent,
 }
 
 /**
- * pevent_register_event_handle - register a way to parse an event
+ * pevent_register_event_handler - register a way to parse an event
  * @pevent: the handle to the pevent
  * @id: the id of the event to register
  * @sys_name: the system name the event belongs to
  * @event_name: the name of the event
  * @func: the function to call to parse the event information
+ * @context: the data to be passed to @func
  *
  * This function allows a developer to override the parsing of
  * a given event. If for some reason the default print format
Signature · 1 line
1.7.10
#22 lore raw

Re: [PATCH 08/11] parse-events: Fix some comments

Quoted 3 lines · Namhyung Kim

Update and add missing argument descriptions and fix some typo on function comments.

Applied, thanks!

Quoted 6 lines

---

*
- * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
+ * Returns: 0 on success, -1 field not found, or 1 if buffer is full.

Must have been some dvorak/qwerty brain fart (I use dvorak). As, the qwerty 'l' is the dvorak 'n'.

-- Steve

Quoted 3 lines

*/
int pevent_print_num_field(struct trace_seq *s, const char *fmt,
struct event_format *event, const char *name,

#23 lore raw

[tip:perf/core] tools lib traceevent: Fix some comments

In reply to Namhyung Kim
Commit-ID:  16e6b8fdfd181ac79f04ff826c42c7d8a2806a17
Gitweb:     http://git.kernel.org/tip/16e6b8fdfd181ac79f04ff826c42c7d8a2806a17
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 23 Apr 2012 13:58:35 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Fix some comments

Update and add missing argument descriptions and fix some typo on function comments.

Cc Frederic Weisbecker <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/lib/traceevent/event-parse.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)
tools/lib/traceevent/event-parse.c +7 -9
index 99b0cd4..8630160 100644
@@ -4037,8 +4037,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event
  * pevent_data_lat_fmt - parse the data for the latency format
  * @pevent: a handle to the pevent
  * @s: the trace_seq to write to
- * @data: the raw data to read from
- * @size: currently unused.
+ * @record: the record to read from
  *
  * This parses out the Latency format (interrupts disabled,
  * need rescheduling, in hard/soft interrupt, preempt count
@@ -4173,10 +4172,7 @@ const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  * pevent_data_comm_from_pid - parse the data into the print format
  * @s: the trace_seq to write to
  * @event: the handle to the event
- * @cpu: the cpu the event was recorded on
- * @data: the raw data
- * @size: the size of the raw data
- * @nsecs: the timestamp of the event
+ * @record: the record to read from
  *
  * This parses the raw @data using the given @event information and
  * writes the print format into the trace_seq.
@@ -4944,7 +4940,7 @@ int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  * @record: The record with the field name.
  * @err: print default error if failed.
  *
- * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
+ * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  */
 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
 			   struct event_format *event, const char *name,
@@ -4986,11 +4982,12 @@ static void free_func_handle(struct pevent_function_handler *func)
  * pevent_register_print_function - register a helper function
  * @pevent: the handle to the pevent
  * @func: the function to process the helper function
+ * @ret_type: the return type of the helper function
  * @name: the name of the helper function
  * @parameters: A list of enum pevent_func_arg_type
  *
  * Some events may have helper functions in the print format arguments.
- * This allows a plugin to dynmically create a way to process one
+ * This allows a plugin to dynamically create a way to process one
  * of these functions.
  *
  * The @parameters is a variable list of pevent_func_arg_type enums that
@@ -5061,12 +5058,13 @@ int pevent_register_print_function(struct pevent *pevent,
 }
 
 /**
- * pevent_register_event_handle - register a way to parse an event
+ * pevent_register_event_handler - register a way to parse an event
  * @pevent: the handle to the pevent
  * @id: the id of the event to register
  * @sys_name: the system name the event belongs to
  * @event_name: the name of the event
  * @func: the function to call to parse the event information
+ * @context: the data to be passed to @func
  *
  * This function allows a developer to override the parsing of
  * a given event. If for some reason the default print format
#24 lore raw

PATCH 9/11 parse-filter: Check result of malloc() during reading token

In reply to Namhyung Kim

The malloc can fail so the return value should be checked. For now, just use malloc_or_die().

---
 parse-filter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
parse-filter.c +1 -1
index bc62f1f..0e6c0b1 100644
@@ -96,7 +96,7 @@ static enum event_type read_token(char **tok)
 	    (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
 	    pevent_peek_char() == '~') {
 		/* append it */
-		*tok = malloc(3);
+		*tok = malloc_or_die(3);
 		sprintf(*tok, "%c%c", *token, '~');
 		free_token(token);
 		/* Now remove the '~' from the buffer */
Signature · 1 line
1.7.10
#25 lore raw

Re: [PATCH 09/11] parse-filter: Check result of malloc() during reading token

Quoted 2 lines · Namhyung Kim

The malloc can fail so the return value should be checked. For now, just use malloc_or_die().

Applied, thanks!

Although we will be getting rid of the 'die' feature. But at least this marks a location that we need to track.

-- Steve

Quoted 19 lines
---
 parse-filter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
parse-filter.c +1 -1
index bc62f1f..0e6c0b1 100644
@@ -96,7 +96,7 @@ static enum event_type read_token(char **tok)
 	    (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
 	    pevent_peek_char() == '~') {
 		/* append it */
-		*tok = malloc(3);
+		*tok = malloc_or_die(3);
 		sprintf(*tok, "%c%c", *token, '~');
 		free_token(token);
 		/* Now remove the '~' from the buffer */
#26 lore raw

Re: [PATCH 09/11] parse-filter: Check result of malloc() during reading token

Hi,

Quoted 9 lines · Steven Rostedt
Quoted 2 lines · Namhyung Kim

The malloc can fail so the return value should be checked. For now, just use malloc_or_die().

Applied, thanks!

Although we will be getting rid of the 'die' feature. But at least this marks a location that we need to track.

Right. The point of this patch was that too. My next step will be the removal of 'die' at all.

Thanks,
Namhyung

#27 lore raw

[tip:perf/core] tools lib traceevent: Check result of malloc() during reading token

In reply to Namhyung Kim
Commit-ID:  e54b34aed1c4082ed03f4d1f7a19276059b1e30a
Gitweb:     http://git.kernel.org/tip/e54b34aed1c4082ed03f4d1f7a19276059b1e30a
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 23 Apr 2012 13:58:36 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Check result of malloc() during reading token

The malloc can fail so the return value should be checked. For now, just use malloc_or_die().

Cc Frederic Weisbecker <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/lib/traceevent/parse-filter.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/lib/traceevent/parse-filter.c +1 -1
index 80d872a..d54c2b4 100644
@@ -96,7 +96,7 @@ static enum event_type read_token(char **tok)
 	    (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
 	    pevent_peek_char() == '~') {
 		/* append it */
-		*tok = malloc(3);
+		*tok = malloc_or_die(3);
 		sprintf(*tok, "%c%c", *token, '~');
 		free_token(token);
 		/* Now remove the '~' from the buffer */
#28 lore raw

PATCH 10/11 parse-filter: Fix signature of create_arg_item()

In reply to Namhyung Kim

The @type should be a type of enum event_type not enum filter_arg_type. This fixes following warning:

 $ make
  COMPILE FPIC           parse-events.o
  COMPILE FPIC           parse-filter.o
/home/namhyung/project/trace-cmd/parse-filter.c: In function ‘create_arg_item’:
/home/namhyung/project/trace-cmd/parse-filter.c:343:9: warning: comparison between ‘enum filter_arg_type’ and ‘enum event_type’ [-Wenum-compare]
/home/namhyung/project/trace-cmd/parse-filter.c:339:2: warning: case value ‘8’ not in enumerated type ‘enum filter_arg_type’ [-Wswitch]
  BUILD STATIC LIB       libparsevent.a
  BUILD STATIC LIB       libtracecmd.a
  BUILD                  trace-cmd
/usr/bin/make -C /home/namhyung/project/trace-cmd/Documentation all
make[1]: Nothing to be done for `all'.
Note: to build the gui, type "make gui"
---
 parse-filter.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
parse-filter.c +2 -3
index 0e6c0b1..fa6e6a2 100644
@@ -325,9 +325,8 @@ static void free_events(struct event_list *events)
 }
 
 static struct filter_arg *
-create_arg_item(struct event_format *event,
-		const char *token, enum filter_arg_type type,
-		char **error_str)
+create_arg_item(struct event_format *event, const char *token,
+		enum event_type type, char **error_str)
 {
 	struct format_field *field;
 	struct filter_arg *arg;
Signature · 1 line
1.7.10
#29 lore raw

Re: [PATCH 10/11] parse-filter: Fix signature of create_arg_item()

Quoted 15 lines · Namhyung Kim

The @type should be a type of enum event_type not enum filter_arg_type. This fixes following warning:

 $ make
  COMPILE FPIC           parse-events.o
  COMPILE FPIC           parse-filter.o
/home/namhyung/project/trace-cmd/parse-filter.c: In function ‘create_arg_item’:
/home/namhyung/project/trace-cmd/parse-filter.c:343:9: warning: comparison between ‘enum filter_arg_type’ and ‘enum event_type’ [-Wenum-compare]
/home/namhyung/project/trace-cmd/parse-filter.c:339:2: warning: case value ‘8’ not in enumerated type ‘enum filter_arg_type’ [-Wswitch]
  BUILD STATIC LIB       libparsevent.a
  BUILD STATIC LIB       libtracecmd.a
  BUILD                  trace-cmd
/usr/bin/make -C /home/namhyung/project/trace-cmd/Documentation all
make[1]: Nothing to be done for `all'.
Note: to build the gui, type "make gui"

Applied thanks!

-- Steve

Quoted 22 lines
---
 parse-filter.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
parse-filter.c +2 -3
index 0e6c0b1..fa6e6a2 100644
@@ -325,9 +325,8 @@ static void free_events(struct event_list *events)
 }
 
 static struct filter_arg *
-create_arg_item(struct event_format *event,
-		const char *token, enum filter_arg_type type,
-		char **error_str)
+create_arg_item(struct event_format *event, const char *token,
+		enum event_type type, char **error_str)
 {
 	struct format_field *field;
 	struct filter_arg *arg;
#30 lore raw

PATCH 11/11 parse-filter: Check return value of arg_to_str()

In reply to Namhyung Kim

The arg_to_str() can fail so we should handle that case properly.

---
 parse-filter.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
parse-filter.c +13 -2
index fa6e6a2..d09fbd2 100644
@@ -2026,11 +2026,13 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 	char *lstr;
 	char *rstr;
 	char *op;
-	char *str;
+	char *str = NULL;
 	int len;
 
 	lstr = arg_to_str(filter, arg->exp.left);
 	rstr = arg_to_str(filter, arg->exp.right);
+	if (!lstr || !rstr)
+		goto out;
 
 	switch (arg->exp.type) {
 	case FILTER_EXP_ADD:
@@ -2070,6 +2072,7 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 	len = strlen(op) + strlen(lstr) + strlen(rstr) + 4;
 	str = malloc_or_die(len);
 	snprintf(str, len, "%s %s %s", lstr, op, rstr);
+out:
 	free(lstr);
 	free(rstr);
 
@@ -2086,6 +2089,8 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 
 	lstr = arg_to_str(filter, arg->num.left);
 	rstr = arg_to_str(filter, arg->num.right);
+	if (!lstr || !rstr)
+		goto out;
 
 	switch (arg->num.type) {
 	case FILTER_CMP_EQ:
@@ -2122,6 +2127,7 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 		break;
 	}
 
+out:
 	free(lstr);
 	free(rstr);
 	return str;
@@ -2272,7 +2278,12 @@ int pevent_filter_compare(struct event_filter *filter1, struct event_filter *fil
 		/* The best way to compare complex filters is with strings */
 		str1 = arg_to_str(filter1, filter_type1->filter);
 		str2 = arg_to_str(filter2, filter_type2->filter);
-		result = strcmp(str1, str2) != 0;
+		if (str1 && str2)
+			result = strcmp(str1, str2) != 0;
+		else
+			/* bail out if allocation fails */
+			result = 1;
+
 		free(str1);
 		free(str2);
 		if (result)
Signature · 1 line
1.7.10
#31 lore raw

Re: [PATCH 11/11] parse-filter: Check return value of arg_to_str()

Quoted 1 line · Namhyung Kim

The arg_to_str() can fail so we should handle that case properly.

Applied thanks!

I also applied your first series. I'll be pushing this to kernel.org shortly.

-- Steve

Quoted 6 lines
---
 parse-filter.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
#32 lore raw

[tip:perf/core] tools lib traceevent: Check return value of arg_to_str()

In reply to Namhyung Kim
Commit-ID:  0fed48341529716c38493be66591bda458921b75
Gitweb:     http://git.kernel.org/tip/0fed48341529716c38493be66591bda458921b75
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 23 Apr 2012 13:58:38 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Check return value of arg_to_str()

The arg_to_str() can fail so we should handle that case properly.

Cc Frederic Weisbecker <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/lib/traceevent/parse-filter.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
tools/lib/traceevent/parse-filter.c +13 -2
index d54c2b4..f020ac6 100644
@@ -2026,11 +2026,13 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 	char *lstr;
 	char *rstr;
 	char *op;
-	char *str;
+	char *str = NULL;
 	int len;
 
 	lstr = arg_to_str(filter, arg->exp.left);
 	rstr = arg_to_str(filter, arg->exp.right);
+	if (!lstr || !rstr)
+		goto out;
 
 	switch (arg->exp.type) {
 	case FILTER_EXP_ADD:
@@ -2070,6 +2072,7 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 	len = strlen(op) + strlen(lstr) + strlen(rstr) + 4;
 	str = malloc_or_die(len);
 	snprintf(str, len, "%s %s %s", lstr, op, rstr);
+out:
 	free(lstr);
 	free(rstr);
 
@@ -2086,6 +2089,8 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 
 	lstr = arg_to_str(filter, arg->num.left);
 	rstr = arg_to_str(filter, arg->num.right);
+	if (!lstr || !rstr)
+		goto out;
 
 	switch (arg->num.type) {
 	case FILTER_CMP_EQ:
@@ -2122,6 +2127,7 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 		break;
 	}
 
+out:
 	free(lstr);
 	free(rstr);
 	return str;
@@ -2272,7 +2278,12 @@ int pevent_filter_compare(struct event_filter *filter1, struct event_filter *fil
 		/* The best way to compare complex filters is with strings */
 		str1 = arg_to_str(filter1, filter_type1->filter);
 		str2 = arg_to_str(filter2, filter_type2->filter);
-		result = strcmp(str1, str2) != 0;
+		if (str1 && str2)
+			result = strcmp(str1, str2) != 0;
+		else
+			/* bail out if allocation fails */
+			result = 1;
+
 		free(str1);
 		free(str2);
 		if (result)
#33 lore raw

[PATCH 12/11] parse-events: Cleanup realloc use

In reply to Namhyung Kim

The if branch is completely unnecessary since 'realloc' can handle NULL pointers for the first parameter.

This patch is just an adoption of Ulrich Drepper's recent patch on perf tools.

Cc Ulrich Drepper <[email protected]>
---
 parse-events.c |    8 ++------
 parse-filter.c |   24 ++++++++----------------
 2 files changed, 10 insertions(+), 22 deletions(-)
parse-events.c +2 -6
index 3ccedd9..9779298 100644
@@ -627,12 +627,8 @@ static void add_event(struct pevent *pevent, struct event_format *event)
 {
 	int i;
 
-	if (!pevent->events)
-		pevent->events = malloc_or_die(sizeof(event));
-	else
-		pevent->events =
-			realloc(pevent->events, sizeof(event) *
-				(pevent->nr_events + 1));
+	pevent->events = realloc(pevent->events, sizeof(event) *
+				 (pevent->nr_events + 1));
 	if (!pevent->events)
 		die("Can not allocate events");
 
parse-filter.c +8 -16
index bc62f1f..79be550 100644
@@ -148,17 +148,11 @@ add_filter_type(struct event_filter *filter, int id)
 	if (filter_type)
 		return filter_type;
 
-	if (!filter->filters)
-		filter->event_filters =
-			malloc_or_die(sizeof(*filter->event_filters));
-	else {
-		filter->event_filters =
-			realloc(filter->event_filters,
-				sizeof(*filter->event_filters) *
-				(filter->filters + 1));
-		if (!filter->event_filters)
-			die("Could not allocate filter");
-	}
+	filter->event_filters =	realloc(filter->event_filters,
+					sizeof(*filter->event_filters) *
+					(filter->filters + 1));
+	if (!filter->event_filters)
+		die("Could not allocate filter");
 
 	for (i = 0; i < filter->filters; i++) {
 		if (filter->event_filters[i].event_id > id)
@@ -1481,7 +1475,7 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 {
 	struct filter_type *filter_type;
 	int count = 0;
-	int *ids;
+	int *ids = NULL;
 	int i;
 
 	if (!filter->filters)
@@ -1505,10 +1499,8 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 		default:
 			break;
 		}
-		if (count)
-			ids = realloc(ids, sizeof(*ids) * (count + 1));
-		else
-			ids = malloc(sizeof(*ids));
+
+		ids = realloc(ids, sizeof(*ids) * (count + 1));
 		if (!ids)
 			die("Can't allocate ids");
 		ids[count++] = filter_type->event_id;
Signature · 1 line
1.7.10
#34 lore raw

Re: [PATCH 12/11] parse-events: Cleanup realloc use

Ping?

Quoted 81 lines · Namhyung Kim

The if branch is completely unnecessary since 'realloc' can handle NULL pointers for the first parameter.

This patch is just an adoption of Ulrich Drepper's recent patch on perf tools.

Cc Ulrich Drepper <[email protected]>
---
 parse-events.c |    8 ++------
 parse-filter.c |   24 ++++++++----------------
 2 files changed, 10 insertions(+), 22 deletions(-)
parse-events.c +2 -6
index 3ccedd9..9779298 100644
@@ -627,12 +627,8 @@ static void add_event(struct pevent *pevent, struct event_format *event)
 {
 	int i;
 
-	if (!pevent->events)
-		pevent->events = malloc_or_die(sizeof(event));
-	else
-		pevent->events =
-			realloc(pevent->events, sizeof(event) *
-				(pevent->nr_events + 1));
+	pevent->events = realloc(pevent->events, sizeof(event) *
+				 (pevent->nr_events + 1));
 	if (!pevent->events)
 		die("Can not allocate events");
 
parse-filter.c +8 -16
index bc62f1f..79be550 100644
@@ -148,17 +148,11 @@ add_filter_type(struct event_filter *filter, int id)
 	if (filter_type)
 		return filter_type;
 
-	if (!filter->filters)
-		filter->event_filters =
-			malloc_or_die(sizeof(*filter->event_filters));
-	else {
-		filter->event_filters =
-			realloc(filter->event_filters,
-				sizeof(*filter->event_filters) *
-				(filter->filters + 1));
-		if (!filter->event_filters)
-			die("Could not allocate filter");
-	}
+	filter->event_filters =	realloc(filter->event_filters,
+					sizeof(*filter->event_filters) *
+					(filter->filters + 1));
+	if (!filter->event_filters)
+		die("Could not allocate filter");
 
 	for (i = 0; i < filter->filters; i++) {
 		if (filter->event_filters[i].event_id > id)
@@ -1481,7 +1475,7 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 {
 	struct filter_type *filter_type;
 	int count = 0;
-	int *ids;
+	int *ids = NULL;
 	int i;
 
 	if (!filter->filters)
@@ -1505,10 +1499,8 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 		default:
 			break;
 		}
-		if (count)
-			ids = realloc(ids, sizeof(*ids) * (count + 1));
-		else
-			ids = malloc(sizeof(*ids));
+
+		ids = realloc(ids, sizeof(*ids) * (count + 1));
 		if (!ids)
 			die("Can't allocate ids");
 		ids[count++] = filter_type->event_id;
#35 lore raw

Re: [PATCH 12/11] parse-events: Cleanup realloc use

Quoted 1 line · Namhyung Kim

Ping?

The 12/11 fooled me ;-) I added 1-11 but as my email is not sorted well, I stopped at 11/11 (thinking that was the last patch).

I'll take a look at this one.

Thanks,

-- Steve

#36 lore raw

Re: [PATCH 12/11] parse-events: Cleanup realloc use

Hi,

Quoted 6 lines · Steven Rostedt
Quoted 1 line · Namhyung Kim

Ping?

The 12/11 fooled me ;-) I added 1-11 but as my email is not sorted well, I stopped at 11/11 (thinking that was the last patch).

Sorry, it should be sent separately. I'll not do it again.

Quoted 2 lines

I'll take a look at this one.

Thanks,
Namhyung

#37 lore raw

[tip:perf/core] tools lib traceevent: Cleanup realloc use

In reply to Namhyung Kim
Commit-ID:  f6ced60fb6c0ee115982157457c47e48802d6e1d
Gitweb:     http://git.kernel.org/tip/f6ced60fb6c0ee115982157457c47e48802d6e1d
Author:     Namhyung Kim <[email protected]>
AuthorDate: Tue, 24 Apr 2012 10:29:44 +0900
Committer:  Namhyung Kim <[email protected]>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Cleanup realloc use

The if branch is completely unnecessary since 'realloc' can handle NULL pointers for the first parameter.

This patch is just an adoption of Ulrich Drepper's recent patch on perf tools.

---
 tools/lib/traceevent/event-parse.c  |    8 ++------
 tools/lib/traceevent/parse-filter.c |   24 ++++++++----------------
 2 files changed, 10 insertions(+), 22 deletions(-)
tools/lib/traceevent/event-parse.c +2 -6
index f880db4..5f34aa3 100644
@@ -633,12 +633,8 @@ static void add_event(struct pevent *pevent, struct event_format *event)
 {
 	int i;
 
-	if (!pevent->events)
-		pevent->events = malloc_or_die(sizeof(event));
-	else
-		pevent->events =
-			realloc(pevent->events, sizeof(event) *
-				(pevent->nr_events + 1));
+	pevent->events = realloc(pevent->events, sizeof(event) *
+				 (pevent->nr_events + 1));
 	if (!pevent->events)
 		die("Can not allocate events");
 
tools/lib/traceevent/parse-filter.c +8 -16
index f020ac6..ad17855 100644
@@ -148,17 +148,11 @@ add_filter_type(struct event_filter *filter, int id)
 	if (filter_type)
 		return filter_type;
 
-	if (!filter->filters)
-		filter->event_filters =
-			malloc_or_die(sizeof(*filter->event_filters));
-	else {
-		filter->event_filters =
-			realloc(filter->event_filters,
-				sizeof(*filter->event_filters) *
-				(filter->filters + 1));
-		if (!filter->event_filters)
-			die("Could not allocate filter");
-	}
+	filter->event_filters =	realloc(filter->event_filters,
+					sizeof(*filter->event_filters) *
+					(filter->filters + 1));
+	if (!filter->event_filters)
+		die("Could not allocate filter");
 
 	for (i = 0; i < filter->filters; i++) {
 		if (filter->event_filters[i].event_id > id)
@@ -1480,7 +1474,7 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 {
 	struct filter_type *filter_type;
 	int count = 0;
-	int *ids;
+	int *ids = NULL;
 	int i;
 
 	if (!filter->filters)
@@ -1504,10 +1498,8 @@ void pevent_filter_clear_trivial(struct event_filter *filter,
 		default:
 			break;
 		}
-		if (count)
-			ids = realloc(ids, sizeof(*ids) * (count + 1));
-		else
-			ids = malloc(sizeof(*ids));
+
+		ids = realloc(ids, sizeof(*ids) * (count + 1));
 		if (!ids)
 			die("Can't allocate ids");
 		ids[count++] = filter_type->event_id;