linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kusanagi Kouichi <slash@ac.auone-net.jp>
To: linux-trace-devel@vger.kernel.org
Subject: [PATCH] trace-cmd: Improve /proc/kallsyms parsing
Date: Sat, 23 Nov 2019 17:41:29 +0900	[thread overview]
Message-ID: <20191123084130027.IPKB.12758.ppp.dion.ne.jp@dmta0005.auone-net.jp> (raw)

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

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;
+
 		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);
 	}
-- 
2.24.0


             reply	other threads:[~2019-11-23  8:47 UTC|newest]

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

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=20191123084130027.IPKB.12758.ppp.dion.ne.jp@dmta0005.auone-net.jp \
    --to=slash@ac.auone-net.jp \
    --cc=linux-trace-devel@vger.kernel.org \
    /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).