tools lib traceevent: Replace malloc_or_die to plain calloc in alloc_event()
PATCH v2 1/5 tools lib traceevent: Replace malloc_or_die to plain calloc in alloc_event()
From: Namhyung Kim <[email protected]>
Because the only caller of the alloc_event() (pevent_parse_event) checks return value properly and all it does after allocation is initializing, it can be changed to use plain calloc.
Thanks to Steven for suggesting this simpler way.
--- tools/lib/traceevent/event-parse.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
tools/lib/traceevent/event-parse.c +1 -6
@@ -619,12 +619,7 @@ void pevent_print_printk(struct pevent *pevent)static struct event_format *alloc_event (void ) { -struct event_format *event; - - event =malloc_or_die (sizeof (*event)); -memset (event,0 ,sizeof (*event)); - -return event; +return calloc (1 ,sizeof (struct event_format )); }static void add_event (struct pevent *pevent,struct event_format *event)
Signature · 1 line
1.7.10.2
PATCH v2 2/5 tools lib traceevent: Do not link broken field arg for an old ftrace event
From: Namhyung Kim <[email protected]>
Defer linking a newly allocated arg to print_fmt.args until all of its field is setup so that later access to ->field.name cannot be NULL.
--- tools/lib/traceevent/event-parse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/lib/traceevent/event-parse.c +3 -3
@@ -4658,20 +4658,20 @@ int pevent_parse_event(struct pevent *pevent,struct print_arg *arg, **list;/* old ftrace had no args */ - list = &event->print_fmt.args;for (field = event->format.fields; field; field = field->next) { arg =alloc_arg (); - *list = arg; - list = &arg->next; arg->type =PRINT_FIELD ; arg->field.name =strdup (field->name);if (!arg->field.name) {do_warning ("failed to allocate field name" ); event->flags |=EVENT_FL_FAILED ; +free_arg (arg);return -1 ; } arg->field.field = field; + *list = arg; + list = &arg->next; }return 0 ; }
Signature · 1 line
1.7.10.2
PATCH v2 3/5 tools lib traceevent: Introduce pevent_errno
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
Define and use error numbers for pevent_parse_event() and get rid of die() and do_warning() calls. If the function returns non-zero value, the caller can check the return code and do appropriate things.
I chose the error numbers to be negative not to clash with standard errno, and as usual, 0 for success.
--- tools/lib/traceevent/event-parse.c | 50 +++++++++++++++++++++--------------- tools/lib/traceevent/event-parse.h | 26 +++++++++++++++++-- 2 files changed, 54 insertions(+), 22 deletions(-)
tools/lib/traceevent/event-parse.c +30 -20
@@ -4590,9 +4590,8 @@ static int find_event_handle(struct pevent *pevent, struct event_format *event)* * /sys/kernel/debug/tracing/events/.../.../format */ -int pevent_parse_event (struct pevent *pevent, -const char *buf,unsigned long size, -const char *sys) +enum pevent_errno pevent_parse_event (struct pevent *pevent,const char *buf, +unsigned long size,const char *sys) {struct event_format *event;int ret;
@@ -4601,17 +4600,16 @@ int pevent_parse_event(struct pevent *pevent, event =alloc_event ();if (!event) -return -ENOMEM ; +return PEVENT_ERRNO__MEM_ALLOC_FAILED ; event->name =event_read_name ();if (!event->name) {/* Bad event? */ -free (event); -return -1 ; + ret =PEVENT_ERRNO__MEM_ALLOC_FAILED ; + goto event_alloc_failed; }if (strcmp (sys,"ftrace" ) ==0 ) { - event->flags |=EVENT_FL_ISFTRACE ;if (strcmp (event->name,"bprint" ) ==0 )
@@ -4619,20 +4617,28 @@ int pevent_parse_event(struct pevent *pevent, } event->id =event_read_id (); -if (event->id <0 ) -die ("failed to read event id" ); +if (event->id <0 ) { + ret =PEVENT_ERRNO__READ_ID_FAILED ; +/* +* This isn't an allocation error actually. +* But as the ID is critical, just bail out. +*/ + goto event_alloc_failed; + } event->system =strdup (sys); -if (!event->system) -die ("failed to allocate system" ); +if (!event->system) { + ret =PEVENT_ERRNO__MEM_ALLOC_FAILED ; + goto event_alloc_failed; + }/* Add pevent to event so that it can be referenced */ event->pevent = pevent; ret =event_read_format (event);if (ret <0 ) { -do_warning ("failed to read event format for %s" , event->name); - goto event_failed; + ret =PEVENT_ERRNO__READ_FORMAT_FAILED ; + goto event_parse_failed; }/*
@@ -4644,10 +4650,9 @@ int pevent_parse_event(struct pevent *pevent, ret =event_read_print (event);if (ret <0 ) { -do_warning ("failed to read event print fmt for %s" , - event->name); show_warning =1 ; - goto event_failed; + ret =PEVENT_ERRNO__READ_PRINT_FAILED ; + goto event_parse_failed; } show_warning =1 ;
@@ -4664,10 +4669,9 @@ int pevent_parse_event(struct pevent *pevent, arg->type =PRINT_FIELD ; arg->field.name =strdup (field->name);if (!arg->field.name) { -do_warning ("failed to allocate field name" ); event->flags |=EVENT_FL_FAILED ;free_arg (arg); -return -1 ; +return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED ; } arg->field.field = field; *list = arg;
@@ -4682,11 +4686,17 @@ int pevent_parse_event(struct pevent *pevent,return 0 ; - event_failed: + event_parse_failed: event->flags |=EVENT_FL_FAILED ;/* still add it even if it failed */ add_event (pevent, event); -return -1 ; +return ret; + + event_alloc_failed: +free (event->system); +free (event->name); +free (event); +return ret; } int get_field_val(struct trace_seq *s, struct format_field *field,
tools/lib/traceevent/event-parse.h +24 -2
@@ -338,6 +338,28 @@ enum pevent_flag { PEVENT_NSEC_OUTPUT = 1, /* output in NSECS */ }; +enum pevent_errno { +PEVENT_ERRNO__SUCCESS =0 , + +/* +* Choose an arbitrary negative big number not to clash with standard +* errno since SUS requires the errno has distinct positive values. +* See 'Issue 6' in the link below. +* +* http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html +*/ + __PEVENT_ERRNO__START =-100000 , + +PEVENT_ERRNO__MEM_ALLOC_FAILED = __PEVENT_ERRNO__START, +PEVENT_ERRNO__PARSE_EVENT_FAILED , +PEVENT_ERRNO__READ_ID_FAILED , +PEVENT_ERRNO__READ_FORMAT_FAILED , +PEVENT_ERRNO__READ_PRINT_FAILED , +PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED , + + __PEVENT_ERRNO__END, +}; +struct cmdline ;struct cmdline_list ;struct func_map ;
@@ -502,8 +524,8 @@ void pevent_print_event(struct pevent *pevent, struct trace_seq *s,int pevent_parse_header_page (struct pevent *pevent,char *buf,unsigned long size,int long_size); -int pevent_parse_event (struct pevent *pevent,const char *buf, -unsigned long size,const char *sys); +enum pevent_errno pevent_parse_event (struct pevent *pevent,const char *buf, +unsigned long size,const char *sys); void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event, const char *name, struct pevent_record *record,
Signature · 1 line
1.7.10.2
PATCH v2 4/5 tools lib traceevent: Introduce pevent_strerror
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
The pevent_strerror() sets @buf to a string that describes the (libtraceevent-specific) error condition that is passed via @errnum.
This is similar to strerror_r() and does same thing if @errnum has a standard errno value.
To sync error string with its code, define PEVENT_ERRORS with _PE() macro and use it as suggested by Steven.
--- tools/lib/traceevent/event-parse.c | 43 ++++++++++++++++++++++++++++++++++++ tools/lib/traceevent/event-parse.h | 20 ++++++++++++----- 2 files changed, 57 insertions(+), 6 deletions(-)
tools/lib/traceevent/event-parse.c +43 -0
@@ -4699,6 +4699,49 @@ enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,return ret; } +#undef _PE +#define _PE (code, str) str +static const char *const pevent_error_str[] = { +PEVENT_ERRORS +}; +#undef _PE + +int pevent_strerror (struct pevent *pevent,enum pevent_errno errnum, +char *buf,size_t buflen) +{ +int idx; +const char *msg; + +if (errnum >=0 ) { +strerror_r (errnum, buf, buflen); +return 0 ; + } + +if (errnum <= __PEVENT_ERRNO__START || + errnum >= __PEVENT_ERRNO__END) +return -1 ; + + idx = errnum - __PEVENT_ERRNO__START; + msg = pevent_error_str[idx]; + +switch (errnum) { +case PEVENT_ERRNO__MEM_ALLOC_FAILED : +case PEVENT_ERRNO__PARSE_EVENT_FAILED : +case PEVENT_ERRNO__READ_ID_FAILED : +case PEVENT_ERRNO__READ_FORMAT_FAILED : +case PEVENT_ERRNO__READ_PRINT_FAILED : +case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED : +snprintf (buf, buflen,"%s" , msg); +break ; + +default : +/* cannot reach here */ +break ; + } + +return 0 ; +} +int get_field_val (struct trace_seq *s,struct format_field *field,const char *name,struct pevent_record *record,unsigned long long *val,int err)
tools/lib/traceevent/event-parse.h +14 -6
@@ -338,6 +338,16 @@ enum pevent_flag { PEVENT_NSEC_OUTPUT = 1, /* output in NSECS */ }; +#define PEVENT_ERRORS \ + _PE(MEM_ALLOC_FAILED, "failed to allocate memory"), \ + _PE(PARSE_EVENT_FAILED, "failed to parse event"), \ + _PE(READ_ID_FAILED, "failed to read event id"), \ + _PE(READ_FORMAT_FAILED, "failed to read event format"), \ + _PE(READ_PRINT_FAILED, "failed to read event print fmt"), \ + _PE(OLD_FTRACE_ARG_FAILED,"failed to allocate field name for ftrace") + +#undef _PE +#define _PE (__code, __str) PEVENT_ERRNO__ ## __codeenum pevent_errno {PEVENT_ERRNO__SUCCESS =0 ,
@@ -350,15 +360,11 @@ enum pevent_errno {*/ __PEVENT_ERRNO__START =-100000 , -PEVENT_ERRNO__MEM_ALLOC_FAILED = __PEVENT_ERRNO__START, -PEVENT_ERRNO__PARSE_EVENT_FAILED , -PEVENT_ERRNO__READ_ID_FAILED , -PEVENT_ERRNO__READ_FORMAT_FAILED , -PEVENT_ERRNO__READ_PRINT_FAILED , -PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED , +PEVENT_ERRORS , __PEVENT_ERRNO__END, }; +#undef _PEstruct cmdline ;struct cmdline_list ;
@@ -576,6 +582,8 @@ int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec);const char *pevent_data_comm_from_pid (struct pevent *pevent,int pid);void pevent_event_info (struct trace_seq *s,struct event_format *event,struct pevent_record *record); +int pevent_strerror (struct pevent *pevent,enum pevent_errno errnum, +char *buf,size_t buflen);struct event_format **pevent_list_events (struct pevent *pevent,enum event_sort_type );struct format_field **pevent_event_common_fields (struct event_format *event);
Signature · 1 line
1.7.10.2
Re: [PATCH v2 4/5] tools lib traceevent: Introduce pevent_strerror
tools/lib/traceevent/event-parse.c enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf, · line 4722
Quoted 3 lines · Namhyung Kim
tools/lib/traceevent/event-parse.c +3
-0+if (errnum <= __PEVENT_ERRNO__START || + errnum >= __PEVENT_ERRNO__END) +return -1 ;
OK, so you changed it to have both START and END be exclusive in the range. No more 'MEM_ALLOC_FAILED = START'.
Quoted 48 lines
tools/lib/traceevent/event-parse.c +22
-0+ + idx = errnum - __PEVENT_ERRNO__START; + msg = pevent_error_str[idx]; + +switch (errnum) { +case PEVENT_ERRNO__MEM_ALLOC_FAILED : +case PEVENT_ERRNO__PARSE_EVENT_FAILED : +case PEVENT_ERRNO__READ_ID_FAILED : +case PEVENT_ERRNO__READ_FORMAT_FAILED : +case PEVENT_ERRNO__READ_PRINT_FAILED : +case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED : +snprintf (buf, buflen,"%s" , msg); +break ; + +default : +/* cannot reach here */ +break ; + } + +return 0 ; +} +int get_field_val (struct trace_seq *s,struct format_field *field,const char *name,struct pevent_record *record,unsigned long long *val,int err)tools/lib/traceevent/event-parse.h +10
-0@@ -338,6 +338,16 @@ enum pevent_flag { PEVENT_NSEC_OUTPUT = 1, /* output in NSECS */ }; +#define PEVENT_ERRORS \ + _PE(MEM_ALLOC_FAILED, "failed to allocate memory"), \ + _PE(PARSE_EVENT_FAILED, "failed to parse event"), \ + _PE(READ_ID_FAILED, "failed to read event id"), \ + _PE(READ_FORMAT_FAILED, "failed to read event format"), \ + _PE(READ_PRINT_FAILED, "failed to read event print fmt"), \ + _PE(OLD_FTRACE_ARG_FAILED,"failed to allocate field name for ftrace") + +#undef _PE +#define _PE (__code, __str) PEVENT_ERRNO__ ## __codeenum pevent_errno {PEVENT_ERRNO__SUCCESS =0 ,@@ -350,15 +360,11 @@ enum pevent_errno {*/
tools/lib/traceevent/event-parse.h enum pevent_errno { · line 364
Quoted 10 lines
tools/lib/traceevent/event-parse.h +1
-6__PEVENT_ERRNO__START =-100000 , -PEVENT_ERRNO__MEM_ALLOC_FAILED = __PEVENT_ERRNO__START, -PEVENT_ERRNO__PARSE_EVENT_FAILED , -PEVENT_ERRNO__READ_ID_FAILED , -PEVENT_ERRNO__READ_FORMAT_FAILED , -PEVENT_ERRNO__READ_PRINT_FAILED , - PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED, + PEVENT_ERRORS,
Looks good.
-- Steve
Quoted 15 lines
tools/lib/traceevent/event-parse.h +3
-0__PEVENT_ERRNO__END, }; +#undef _PEstruct cmdline ;struct cmdline_list ;@@ -576,6 +582,8 @@ int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec);const char *pevent_data_comm_from_pid (struct pevent *pevent,int pid);void pevent_event_info (struct trace_seq *s,struct event_format *event,struct pevent_record *record); +int pevent_strerror (struct pevent *pevent,enum pevent_errno errnum, +char *buf,size_t buflen);struct event_format **pevent_list_events (struct pevent *pevent,enum event_sort_type );struct format_field **pevent_event_common_fields (struct event_format *event);
PATCH v2 5/5 scripts/tags.sh: Teach [ce]tags about libtraceeevent error codes
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
As we use a macro trick to sync each error codes with its description string, teach [ce]tags to process them properly.
--- scripts/tags.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/tags.sh +4 -2
@@ -153,7 +153,8 @@ exuberant() --regex-c++='/CLEARPAGEFLAG_NOOP\(([^,)]*).*/ClearPage\1/' \ --regex-c++='/__CLEARPAGEFLAG_NOOP\(([^,)]*).*/__ClearPage\1/' \ --regex-c++='/TESTCLEARFLAG_FALSE\(([^,)]*).*/TestClearPage\1/' \ - --regex-c++='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' + --regex-c++='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' \ + --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' all_kconfigs | xargs $1 -a \ --langdef=kconfig --language-force=kconfig \
@@ -195,7 +196,8 @@ emacs() --regex='/CLEARPAGEFLAG_NOOP\(([^,)]*).*/ClearPage\1/' \ --regex='/__CLEARPAGEFLAG_NOOP\(([^,)]*).*/__ClearPage\1/' \ --regex='/TESTCLEARFLAG_FALSE\(([^,)]*).*/TestClearPage\1/' \ - --regex='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' + --regex='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' \ + --regex='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' all_kconfigs | xargs $1 -a \ --regex='/^[ \t]*\(\(menu\)*config\)[ \t]+\([a-zA-Z0-9_]+\)/\3/'
Signature · 1 line
1.7.10.2
Re: [PATCH v2 5/5] scripts/tags.sh: Teach [ce]tags about libtraceeevent error codes
Quoted 29 lines · Namhyung Kim
From: Namhyung Kim <[email protected]>
As we use a macro trick to sync each error codes with its description string, teach [ce]tags to process them properly.
Suggested-by Steven Rostedt <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Michal Marek <[email protected]>Signed-off-by Namhyung Kim <[email protected]>--- scripts/tags.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/tags.sh +2
-1@@ -153,7 +153,8 @@ exuberant() --regex-c++='/CLEARPAGEFLAG_NOOP\(([^,)]*).*/ClearPage\1/' \ --regex-c++='/__CLEARPAGEFLAG_NOOP\(([^,)]*).*/__ClearPage\1/' \ --regex-c++='/TESTCLEARFLAG_FALSE\(([^,)]*).*/TestClearPage\1/' \ - --regex-c++='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' + --regex-c++='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' \ + --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' all_kconfigs | xargs $1 -a \ --langdef=kconfig --language-force=kconfig \
scripts/tags.sh emacs() · line 203
Quoted 9 lines
scripts/tags.sh +2
-1@@ -195,7 +196,8 @@ emacs() --regex='/CLEARPAGEFLAG_NOOP\(([^,)]*).*/ClearPage\1/' \ --regex='/__CLEARPAGEFLAG_NOOP\(([^,)]*).*/__ClearPage\1/' \ --regex='/TESTCLEARFLAG_FALSE\(([^,)]*).*/TestClearPage\1/' \ - --regex='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' + --regex='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' \ + --regex='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' all_kconfigs | xargs $1 -a \ --regex='/^[ \t]*\(\(menu\)*config\)[ \t]+\([a-zA-Z0-9_]+\)/\3/'
This works great if we run tags from the top level kernel tree, but we need to also add this in the perf directory as well.
I'll ack this patch for now, and we can fix up perf later. I think there should be an easy way to get perf to use the tags.sh script from the scripts directory.
-- Steve
Re: [PATCH v2 5/5] scripts/tags.sh: Teach [ce]tags about libtraceeevent error codes
In reply to Namhyung KimQuoted 5 lines · Namhyung Kim
From: Namhyung Kim <[email protected]>
As we use a macro trick to sync each error codes with its description string, teach [ce]tags to process them properly.
Quoted 8 lines
Suggested-by Steven Rostedt <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Michal Marek <[email protected]>Signed-off-by Namhyung Kim <[email protected]>--- scripts/tags.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Applied to kbuild.git#misc, thanks.
Michal
Quoted 10 lines · Namhyung Kim
From: Namhyung Kim <[email protected]>
Because the only caller of the
alloc_event()(pevent_parse_event) checks return value properly and all it does after allocation is initializing, it can be changed to use plain calloc.Thanks to Steven for suggesting this simpler way.
Cc Frederic Weisbecker <[email protected]>
-- Steve
Quoted 24 lines
Signed-off-by Namhyung Kim <[email protected]>--- tools/lib/traceevent/event-parse.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)tools/lib/traceevent/event-parse.c +1
-6@@ -619,12 +619,7 @@ void pevent_print_printk(struct pevent *pevent)static struct event_format *alloc_event (void ) { -struct event_format *event; - - event =malloc_or_die (sizeof (*event)); -memset (event,0 ,sizeof (*event)); - -return event; +return calloc (1 ,sizeof (struct event_format )); }static void add_event (struct pevent *pevent,struct event_format *event)
Arnaldo,
I've acked this series. Can you add my Acked-by tags on the patches that do not have it yet, and then pull it into your repo?
Thanks!
-- Steve
Em Mon, Jun 18, 2012 at 09:45:48AM -0400, Steven Rostedt escreveu:
Quoted 4 lines
Arnaldo,
I've acked this series. Can you add my Acked-by tags on the patches that do not have it yet, and then pull it into your repo?
Sure thing, I'll put it in my perf/core branch,
- Arnaldo
Hi, Arnaldo
Quoted 8 lines · Arnaldo Carvalho de Melo
Em Mon, Jun 18, 2012 at 09:45:48AM -0400, Steven Rostedt escreveu:
Quoted 4 lines
Arnaldo,
I've acked this series. Can you add my Acked-by tags on the patches that do not have it yet, and then pull it into your repo?
Sure thing, I'll put it in my perf/core branch,
As I said before [1], those patches are based on my libtraceevent/next branch which contains a couple of commits not merged into tip or your tree. They are my previous backport work, and Steve wanted to leave them out for 3.6 cycle [2].
So my question is that do you want to pull those bits too? If so, I can send a pull request to you if you want. :) - I noticed that your perf/core branch isn't on top of current tip/perf/core. So I've also setup libtraceevent/core branch to track your branch.
Thanks,
Namhyung
[1] article.gmane.org/gmane.linux.kernel/1310798
[2] article.gmane.org/gmane.linux.kernel/1302643
Quoted 9 lines · Namhyung Kim
Hi, Arnaldo
Quoted 6 lines · Arnaldo Carvalho de Melo
Em Mon, Jun 18, 2012 at 09:45:48AM -0400, Steven Rostedt escreveu:
Quoted 4 lines
Arnaldo,
I've acked this series. Can you add my Acked-by tags on the patches that do not have it yet, and then pull it into your repo?
Quoted 7 lines
Quoted 2 lines
Sure thing, I'll put it in my perf/core branch,
As I said before [1], those patches are based on my libtraceevent/next branch which contains a couple of commits not merged into tip or your tree. They are my previous backport work, and Steve wanted to leave them out for 3.6 cycle [2].
As the merge window has closed, I was under the assumption that these patches are for 3.6. That's what I wanted them queued up for.
-- Steve