public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: linux-kernel <linux-kernel@vger.kernel.org>, systemtap@sourceware.org
Subject: [RFC] fix kallsyms to allow discrimination of local symbols
Date: Mon, 21 Jul 2008 16:43:14 -0500	[thread overview]
Message-ID: <1216676595.3433.80.camel@localhost.localdomain> (raw)

The problem is that local symbols, being hidden from the linker, might
not be unique.  Thus, they don't make good anchors for the symbol
relative addressing used by kprobes (it takes the first occurrence it
finds).  Likewise, when they appear in stack traces, it's sometimes not
obvious which local symbol it is (although context usually allows an
easy guess).

Fix all of this by prefixing local symbols with the actual C file name
they occur in separated by '|' (I had to use '|' since ':' is already in
use for module prefixes in kallsyms lookups.

I also had to rewrite mksysmap in perl because the necessary text
formatting changes in shell are painfully slow.

Comments?

James

---

diff --git a/Makefile b/Makefile
index 6192922..a416b35 100644
--- a/Makefile
+++ b/Makefile
@@ -685,7 +685,7 @@ quiet_cmd_vmlinux_version = GEN     .version
 
 # Generate System.map
 quiet_cmd_sysmap = SYSMAP
-      cmd_sysmap = $(CONFIG_SHELL) $(srctree)/scripts/mksysmap
+      cmd_sysmap = $(PERL) $(srctree)/scripts/mksysmap
 
 # Link of vmlinux
 # If CONFIG_KALLSYMS is set .version is already updated
@@ -759,7 +759,7 @@ endef
 
 # Generate .S file with all kernel symbols
 quiet_cmd_kallsyms = KSYM    $@
-      cmd_kallsyms = $(NM) -n $< | $(KALLSYMS) \
+      cmd_kallsyms = $(NM) -n -l $< | sed "s|`pwd`/||" | $(KALLSYMS) \
                      $(if $(CONFIG_KALLSYMS_ALL),--all-symbols) > $@
 
 .tmp_kallsyms1.o .tmp_kallsyms2.o .tmp_kallsyms3.o: %.o: %.S scripts FORCE
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index ad2434b..0badae2 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -63,11 +63,12 @@ static inline int is_arm_mapping_symbol(const char *str)
 
 static int read_symbol(FILE *in, struct sym_entry *s)
 {
-	char str[500];
+	char str[500], file[500];
 	char *sym, stype;
-	int rc;
+	int rc, c, line;
 
-	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
+	file[0] = '\0';
+	rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str);
 	if (rc != 3) {
 		if (rc != EOF) {
 			/* skip line */
@@ -75,6 +76,12 @@ static int read_symbol(FILE *in, struct sym_entry *s)
 		}
 		return -1;
 	}
+	c = fgetc(in);
+	if (c != '\n') {
+		rc = fscanf(in, "%499[^:]:%d\n", file, &line);
+		if (rc != 2)
+			file[0] = '\0';
+	}
 
 	sym = str;
 	/* skip prefix char */
@@ -115,13 +122,22 @@ static int read_symbol(FILE *in, struct sym_entry *s)
 	/* include the type field in the symbol name, so that it gets
 	 * compressed together */
 	s->len = strlen(str) + 1;
+	if (islower(stype))
+		s->len += strlen(file) + 1;
 	s->sym = malloc(s->len + 1);
 	if (!s->sym) {
 		fprintf(stderr, "kallsyms failure: "
 			"unable to allocate required amount of memory\n");
 		exit(EXIT_FAILURE);
 	}
-	strcpy((char *)s->sym + 1, str);
+	if (islower(stype)) {
+		char *ss = (char *)s->sym + 1;
+		
+		strcpy(ss, file);
+		strcat(ss, "|");
+		strcat(ss, str);
+	} else
+		strcpy((char *)s->sym + 1, str);
 	s->sym[0] = stype;
 
 	return 0;
diff --git a/scripts/mksysmap b/scripts/mksysmap
index 6e133a0..496cadd 100644
--- a/scripts/mksysmap
+++ b/scripts/mksysmap
@@ -1,4 +1,4 @@
-#!/bin/sh -x
+#!/usr/bin/perl
 # Based on the vmlinux file create the System.map file
 # System.map is used by module-init tools and some debugging
 # tools to retrieve the actual addresses of symbols in the kernel.
@@ -41,5 +41,21 @@
 # so we just ignore them to let readprofile continue to work.
 # (At least sparc64 has __crc_ in the middle).
 
-$NM -n $1 | grep -v '\( [aNUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $2
+chomp($cwd = `pwd`);
+open(I, "nm -n -l $ARGV[0]|") || die;
+open(O, ">$ARGV[1]") || die;
+foreach(<I>) {
+    chomp;
+    ($addr, $type, $symbol, $file_and_line) = split(/[ 	]/, $_);
+    next if ($type =~ /[aNUw]/ || $type =~ /\$[adt]/);
+    next if ($symbol=~ /__crc_/);
+    if ($type =~ /[a-z]/ && $file_and_line) {
+	($_) = split(/:/, $file_and_line);
+	(undef, $file) = split(/^$cwd\//, $_);
+	$symbol = $file."|".$symbol;
+    }
+    print O "$addr $type $symbol\n";
+}
+
+
 



             reply	other threads:[~2008-07-21 21:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-21 21:43 James Bottomley [this message]
2008-07-22  1:44 ` [RFC] fix kallsyms to allow discrimination of local symbols Frank Ch. Eigler
2008-07-22  3:53   ` James Bottomley
2008-07-22 11:51     ` Frank Ch. Eigler
2008-07-22 15:14       ` James Bottomley
2008-07-22 16:05         ` Frank Ch. Eigler
2008-07-23  1:48           ` Theodore Tso
2008-07-23  4:16             ` Frank Ch. Eigler
2008-07-23 16:25               ` Theodore Tso
2008-07-23 16:40                 ` James Bottomley
2008-07-23 17:47                   ` Theodore Tso
  -- strict thread matches above, loose matches on Subject: below --
2008-07-24 16:03 Frank Ch. Eigler

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=1216676595.3433.80.camel@localhost.localdomain \
    --to=james.bottomley@hansenpartnership.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=systemtap@sourceware.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