Without the patch:
  crash> bt
  PID: 3520     TASK: ffff8f240f670000  CPU: 1    COMMAND: "insmod"
   #0 [ffffd08c4f063a20] machine_kexec at ffffffff9575e60e
   #1 [ffffd08c4f063a40] __crash_kexec at ffffffff958db711
   #2 [ffffd08c4f063b00] panic at ffffffff9560cede
   #3 [ffffd08c4f063b80] _RNvCscb18lrEyTSA_10rust_panic10area_in_hp at 
ffffffffc07fe107 [rust_panic]
   #4 [ffffd08c4f063c20] 
_RNvMCscb18lrEyTSA_10rust_panicNtB2_10HelloPanic8step_two at ffffffffc07fe160 
[rust_panic]
      ...

  crash> sym _RNvCscb18lrEyTSA_10rust_panic10area_in_hp
  ffffffffc07fe010 (t) _RNvCscb18lrEyTSA_10rust_panic10area_in_hp [rust_panic] 
/root/linux-6.16.3/samples/rust/rust_panic.rs: 22

With the patch:
  crash> bt
  PID: 3520     TASK: ffff8f240f670000  CPU: 1    COMMAND: "insmod"
   #0 [ffffd08c4f063a20] machine_kexec at ffffffff9575e60e
   #1 [ffffd08c4f063a40] __crash_kexec at ffffffff958db711
   #2 [ffffd08c4f063b00] panic at ffffffff9560cede
   #3 [ffffd08c4f063b80] rust_panic::area_in_hp at ffffffffc07fe107 [rust_panic]
   #4 [ffffd08c4f063c20] <rust_panic::HelloPanic>::step_two at ffffffffc07fe160 
[rust_panic]
      ...

  crash> sym "rust_panic::area_in_hp"
  ffffffffc07fe010 (t) rust_panic::area_in_hp [rust_panic] 
/root/linux-6.16.3/samples/rust/rust_panic.rs: 22

Signed-off-by: Lianbo Jiang <[email protected]>
---
 symbols.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/symbols.c b/symbols.c
index 052196265be0..7e9f57be7cec 100644
--- a/symbols.c
+++ b/symbols.c
@@ -3277,6 +3277,46 @@ load_module_filter(char *s, int type)
 
 #define AVERAGE_SYMBOL_SIZE (16)
 
+static size_t rust_demangle_symbol(const char *symbol, char *out, size_t 
out_size)
+{
+       int i;
+       size_t loc = 0;
+       size_t len = strlen(symbol);
+       char *buf = NULL;
+       /*
+        * Rust symbols always start with _R (v0) or _ZN (legacy)
+        */
+       const char *mangled_rust[] = {
+               "_R",
+               "_ZN",
+               NULL
+       };
+
+       if (!out || out_size < len)
+               return 0;
+
+       for (i = 0; mangled_rust[i]; i++) {
+               size_t sz = strlen(mangled_rust[i]);
+               char *p = memmem(symbol, len, mangled_rust[i], sz);
+               if (p) {
+                       loc = p - symbol;
+                       if (loc)
+                               memcpy(out, symbol, loc);
+                       break;
+               }
+       }
+
+       buf = rust_demangle(symbol + loc, DMGL_RUST);
+       if (buf) {
+               memcpy(out + loc, buf, strlen(buf));
+               free(buf);
+               return 1;
+       } else if (loc != 0)
+               memset(out, 0, loc);
+
+       return 0;
+}
+
 static int
 namespace_ctl(int cmd, struct symbol_namespace *ns, void *nsarg1, void *nsarg2)
 {
@@ -3315,9 +3355,14 @@ namespace_ctl(int cmd, struct symbol_namespace *ns, void 
*nsarg1, void *nsarg2)
                return TRUE;
 
        case NAMESPACE_INSTALL:
+               char demangled[BUFSIZE] = {0};
                sp = (struct syment *)nsarg1;
                name = (char *)nsarg2;
                len = strlen(name)+1;
+               if (rust_demangle_symbol(name, demangled, sizeof(demangled))) {
+                       len = strlen(demangled) + 1;
+                       name = demangled;
+               }
                if ((ns->index + len) >= ns->size) { 
                         if (!(addr = realloc(ns->address, ns->size*2))) 
                                error(FATAL, "symbol name space malloc: %s\n",
-- 
2.50.1
--
Crash-utility mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://${domain_name}/admin/lists/devel.lists.crash-utility.osci.io/
Contribution Guidelines: https://github.com/crash-utility/crash/wiki

Reply via email to