linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Kusanagi Kouichi <slash@ac.auone-net.jp>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH] trace-cmd: Improve /proc/kallsyms parsing
Date: Sat, 23 Nov 2019 11:42:01 -0500	[thread overview]
Message-ID: <20191123113810.46819667@oasis.local.home> (raw)
In-Reply-To: <20191123084130027.IPKB.12758.ppp.dion.ne.jp@dmta0005.auone-net.jp>

On Sat, 23 Nov 2019 17:41:29 +0900
Kusanagi Kouichi <slash@ac.auone-net.jp> wrote:

> - Use the stricter format string.
> - Avoid allocating memory.
> - Check sscanf return value.
> 

Hi Kusanagi,

Thanks for the patch, small comment below.

> Signed-off-by: Kusanagi Kouichi <slash@ac.auone-net.jp>
> ---
>  lib/trace-cmd/trace-util.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
> 
> diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
> index 8aa3b6c..ef8e4f7 100644
> --- a/lib/trace-cmd/trace-util.c
> +++ b/lib/trace-cmd/trace-util.c
> @@ -80,39 +80,40 @@ void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent,
>  	char *func;
>  	char *line;
>  	char *next = NULL;
> -	char *addr_str;
>  	char *mod;
>  	char ch;
>  
>  	line = strtok_r(file, "\n", &next);
>  	while (line) {
> +		int n, func_start, func_end = 0, mod_start, mod_end = 0;

Small nit. I usually prefer to not have so many variables defined on
one line. Something more like this:

		int func_start, func_end = 0;
		int mod_start, mod_end = 0;
		int n;

Just easier to review.

The rest looks good. I'll play with it to see if there's any instances
that it doesn't work.

Thanks!

-- Steve


> +
>  		mod = NULL;
>  		errno = 0;
> -		sscanf(line, "%ms %c %ms\t[%ms",
> -			     &addr_str, &ch, &func, &mod);
> +		n = sscanf(line, "%16llx %c %n%*s%n%*1[\t][%n%*s%n",
> +			   &addr, &ch, &func_start, &func_end, &mod_start, &mod_end);
> 		if (errno) {
> -			free(addr_str);
> -			free(func);
> -			free(mod);
>  			perror("sscanf");
>  			return;
>  		}
> -		addr = strtoull(addr_str, NULL, 16);
> -		free(addr_str);
>  
> -		/* truncate the extra ']' */
> -		if (mod)
> -			mod[strlen(mod) - 1] = 0;
> +		if (n != 2 || !func_end)
> +			return;
>  
> +		func = line + func_start;
>  		/*
>  		 * Hacks for
>  		 *  - arm arch that adds a lot of bogus '$a' functions
>  		 *  - x86-64 that reports per-cpu variable offsets as absolute */
> -		if (func[0] != '$' && ch != 'A' && ch != 'a')
> +		if (func[0] != '$' && ch != 'A' && ch != 'a') {
> +			line[func_end] = 0;
> +			if (mod_end) {
> +				mod = line + mod_start;
> +				/* truncate the extra ']' */
> +				line[mod_end - 1] = 0;
> +			}
>  			tep_register_function(pevent, func, addr, mod);
> -		free(func);
> -		free(mod);
> +		}
>  
>  		line = strtok_r(NULL, "\n", &next);
>  	}


      reply	other threads:[~2019-11-23 16:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-23  8:41 [PATCH] trace-cmd: Improve /proc/kallsyms parsing Kusanagi Kouichi
2019-11-23 16:42 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191123113810.46819667@oasis.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=slash@ac.auone-net.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).