trace-cmd: libparsevent cleanups - part 2
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
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
@@ -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
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.
Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)parse-events.c +0
-0
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
-4field->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);
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.
Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)parse-events.c +0
-0parse-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
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
@@ -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
PATCH 2/11 parse-events: Fix freeing arg on process_dynamic_array()
In reply to Namhyung KimThe @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
@@ -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
Steven Rostedt
AppliedRe: [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 ofprocess_arg(). We can free it only after it was reused for local usage.Signed-off-by Namhyung Kim <[email protected]>
Applied, thanks!
-- Steve
PATCH 3/11 parse-events: Get rid of handling concatenation on event_read_print
In reply to Namhyung KimThe 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
@@ -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
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
Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 16 ---------------- 1 file changed, 16 deletions(-)parse-events.c +0
-16@@ -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;
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
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
PATCH 4/11 parse-events: Remove unused arg->dynarray.index
In reply to Namhyung KimIt 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
@@ -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
@@ -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
Re: [PATCH 04/11] parse-events: Remove unused arg->dynarray.index
Quoted 2 lines · Namhyung Kim
It seems index field of struct
print_arg_dynarrayis 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
PATCH 5/11 parse-events: Use proper function parameter type
In reply to Namhyung KimThe 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
@@ -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
Steven Rostedt
AppliedRe: [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 correctparam->typefor the farg.
Ouch!
Applied, thanks!
-- Steve
Quoted 18 lines
Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 1 + 1 file changed, 1 insertion(+)parse-events.c +1
-0@@ -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);
PATCH 6/11 parse-events: Pass string type argument to args
In reply to Namhyung KimIt 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
@@ -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
Steven Rostedt
AppliedRe: [PATCH 06/11] parse-events: Pass string type argument to args
Quoted 2 lines · Namhyung Kim
It seems
PEVENT_FUNC_ARG_STRINGmissed passing the allocated string to the args array. Fix it.
Another Ouch!
Applied, thanks!
-- Steve
Quoted 18 lines
Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 1 + 1 file changed, 1 insertion(+)parse-events.c +1
-0@@ -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 ;
[tip:perf/core] tools lib traceevent: Pass string type argument to args
In reply to Namhyung KimCommit-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.
--- tools/lib/traceevent/event-parse.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
tools/lib/traceevent/event-parse.c +1 -0
@@ -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 ;
PATCH 7/11 parse-events: Do not call add_event() again if allocation failed
In reply to Namhyung KimWhen 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
@@ -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
Steven Rostedt
AppliedRe: [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_failedsince we added the event already.Signed-off-by Namhyung Kim <[email protected]>
Applied, Thanks!
-- Steve
[tip:perf/core] tools lib traceevent: Do not call add_event() again if allocation failed
In reply to Namhyung KimCommit-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.
--- tools/lib/traceevent/event-parse.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
tools/lib/traceevent/event-parse.c +2 -1
@@ -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; }
PATCH 8/11 parse-events: Fix some comments
In reply to Namhyung KimUpdate 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
@@ -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
Steven Rostedt
AppliedRe: [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
Signed-off-by Namhyung Kim <[email protected]>---
*
- * 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(structtrace_seq*s, const char *fmt,
structevent_format*event, const char *name,
[tip:perf/core] tools lib traceevent: Fix some comments
In reply to Namhyung KimCommit-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.
--- tools/lib/traceevent/event-parse.c | 16 +++++++--------- 1 files changed, 7 insertions(+), 9 deletions(-)
tools/lib/traceevent/event-parse.c +7 -9
@@ -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
PATCH 9/11 parse-filter: Check result of malloc() during reading token
In reply to Namhyung KimThe 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
@@ -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
Steven Rostedt
AppliedRe: [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
Signed-off-by Namhyung Kim <[email protected]>--- parse-filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)parse-filter.c +1
-1@@ -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 */
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
[tip:perf/core] tools lib traceevent: Check result of malloc() during reading token
In reply to Namhyung KimCommit-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().
--- tools/lib/traceevent/parse-filter.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
tools/lib/traceevent/parse-filter.c +1 -1
@@ -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 */
PATCH 10/11 parse-filter: Fix signature of create_arg_item()
In reply to Namhyung KimThe @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
@@ -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
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_typenot enumfilter_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
Signed-off-by Namhyung Kim <[email protected]>--- parse-filter.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)parse-filter.c +2
-3@@ -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;
PATCH 11/11 parse-filter: Check return value of arg_to_str()
In reply to Namhyung KimThe 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
@@ -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
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
Signed-off-by Namhyung Kim <[email protected]>--- parse-filter.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
[tip:perf/core] tools lib traceevent: Check return value of arg_to_str()
In reply to Namhyung KimCommit-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.
--- tools/lib/traceevent/parse-filter.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-)
tools/lib/traceevent/parse-filter.c +13 -2
@@ -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)
[PATCH 12/11] parse-events: Cleanup realloc use
In reply to Namhyung KimThe 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.
--- parse-events.c | 8 ++------ parse-filter.c | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 22 deletions(-)
parse-events.c +2 -6
@@ -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
@@ -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
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]>Signed-off-by Namhyung Kim <[email protected]>--- parse-events.c | 8 ++------ parse-filter.c | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 22 deletions(-)parse-events.c +2
-6@@ -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@@ -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;
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
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
[tip:perf/core] tools lib traceevent: Cleanup realloc use
In reply to Namhyung KimCommit-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
@@ -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
@@ -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;