Cannot find debug symbol (No symbol in current context)

I just start to learn Zig. I have a very simple program

const std = @import("std");

pub const User = struct {
    power: u64,
    name: []const u8,
};

pub fn main() void {
    const user = User{ .power = 9001, .name = "Goku" };
    std.debug.print("{s}'s power is {d}\n", .{ user.name, user.power });
}

I tried to use gdb to debug the program to make sure I can use debugger to debug Zig program.

I build above Zig program with zig build-exe src/main.zig -Doptimize=Debug and when I invoke gdb main, there is no symbol found by gdb as indicated below

Reading symbols from main...
(gdb) b main.main
Breakpoint 1 at 0x10de234: file main.zig, line 58.
(gdb) run
Starting program: /home/zeyuanhu/projects/hello-world/main 

Breakpoint 1, main.main () at main.zig:58
58	    std.debug.print("{s}'s power is {d}\n", .{ user.name, user.power });
(gdb) p user
No symbol "user" in current context.

I was expecting to print user variable but I cannot do so. I was wondering if there is anything wrong with my steps to allow me to use debugger on Zig program? Thank you very much!

I did a bit more research by adding the following code into main

const a = 10;
std.debug.print("{d}\n", .{a});

const b: u32 = 10;
std.debug.print("{d}\n", .{b});

I find that gdb cannot print value of a as well. However, gdb can print value of b. Not sure why.

My two cents: here as you don’t define a type for a it will be of type comptime_int. Maybe those comptime constants are stripped from the exe (reminds me of #define in C where you have to ask to gcc to keep those informations to be accessible from a debugger).
Maybe a cli arg could do that?

2 Likes

For better (experimental) debugging experience you can try zig-adjusted lldb fork by jacobly0 together with zig x86 backend (-fno-llvm).

(lldb) target create "main"
Current executable set to '/tmp/tmp.XQSvAW5vhG/main' (x86_64).
(lldb) b main
Breakpoint 1: where = main`main.main + 8 at main.zig:9:5, address = 0x0000000001150a36
(lldb) r
Process 114579 launched: '/tmp/tmp.XQSvAW5vhG/main' (x86_64)
Process 114579 stopped
* thread #1, name = 'main', stop reason = breakpoint 1.1
    frame #0: 0x0000000001150a36 main`main.main at main.zig:9:5
   6   	};
   7   	
   8   	pub fn main() void {
-> 9   	    const user = User{ .power = 9001, .name = "Goku" };
   10  	    std.debug.print("{s}'s power is {d}\n", .{ user.name, user.power });
   11  	}
(lldb) n
Process 114579 stopped
* thread #1, name = 'main', stop reason = step over
    frame #0: 0x0000000001150a48 main`main.main at main.zig:10:20
   7   	
   8   	pub fn main() void {
   9   	    const user = User{ .power = 9001, .name = "Goku" };
-> 10  	    std.debug.print("{s}'s power is {d}\n", .{ user.name, user.power });
   11  	}
(lldb) p user
(main.main.User) {
  .power = 9001
  .name = len=4 {
    [0] = 71
    [1] = 111
    [2] = 107
    [3] = 117
  }
}
(lldb)
1 Like

Thank you very much for the pointer! I tried to build the custom zig-specific LLDB and I hit some issues. Here is what I did:

  1. git clone --depth=1 git@github.com:jacobly0/llvm-project.git zig-llvm-project
  2. git checkout lldb-zig
  3. mkdir ~/projects/zig-llvm-project-build/
  4. cd ~/projects/zig-llvm-project-build/
  5. cmake -B ~/projects/zig-llvm-project/llvm-build -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS="clang;lldb" -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ~/projects/zig-llvm-project/llvm
  6. cd ~/projects/zig-llvm-project/llvm-build
  7. make -j4

Now, I got the following error. I’m not sure if you have ever seen the issue before?

EDIT: it seems someone else hit the same issue. I was wondering which branch and commit you get a successful compilation? I would like to have those to try again. Thank you!

In file included from /usr/include/c++/9/utility:70,
                 from /usr/include/c++/9/tuple:38,
                 from /home/zeyuanhu/projects/zig-llvm-project/llvm/include/llvm/ADT/DenseMapInfo.h:20,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/include/lldb/Utility/ConstString.h:12,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/ZigValue.h:12,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.h:12,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:9:
/usr/include/c++/9/bits/stl_pair.h: In instantiation of ‘struct std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE>’:
/usr/include/c++/9/ext/aligned_buffer.h:91:28:   required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE> >’
/usr/include/c++/9/bits/hashtable_policy.h:233:43:   required from ‘struct std::__detail::_Hash_node_value_base<std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE> >’
/usr/include/c++/9/bits/hashtable_policy.h:279:12:   required from ‘struct std::__detail::_Hash_node<std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE>, false>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13:   required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE>, false> > >’
/usr/include/c++/9/bits/hashtable.h:173:11:   required from ‘class std::_Hashtable<lldb_private::ZigScope*, std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE>, std::allocator<std::pair<lldb_private::ZigScope* const, lldb_private::plugin::dwarf::DWARFDIE> >, std::__detail::_Select1st, std::equal_to<lldb_private::ZigScope*>, std::hash<lldb_private::ZigScope*>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, false> >’
/usr/include/c++/9/bits/unordered_map.h:1254:18:   required from ‘class std::unordered_multimap<lldb_private::ZigScope*, lldb_private::plugin::dwarf::DWARFDIE>’
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserZig.h:102:7:   required from here
/usr/include/c++/9/bits/stl_pair.h:215:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
  215 |       _T2 second;                /// @c second is a copy of the second object
      |           ^~~~~~
In file included from /home/zeyuanhu/projects/zig-llvm-project/lldb/include/lldb/Core/Module.h:17,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.h:14,
                 from /home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:9:
/home/zeyuanhu/projects/zig-llvm-project/lldb/include/lldb/Symbol/TypeSystem.h:41:7: note: forward declaration of ‘class lldb_private::plugin::dwarf::DWARFDIE’
   41 | class DWARFDIE;
      |       ^~~~~~~~
[ 82%] Building CXX object lib/Target/X86/CMakeFiles/LLVMX86CodeGen.dir/GISel/X86LegalizerInfo.cpp.o
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In function ‘llvm::Align MaxIntAlignment(lldb_private::ArchSpec)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:1030:1: warning: control reaches end of non-void function [-Wreturn-type]
 1030 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual bool lldb_private::TypeSystemZig::IsAggregateType(lldb::opaque_compiler_type_t)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2256:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
 2256 |     switch (llvm::cast<ZigPointerType>(zig_type)->GetSize()) {
      |     ^~~~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2264:3: note: here
 2264 |   case ZigValue::Kind::OptionalType:
      |   ^~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2276:1: warning: control reaches end of non-void function [-Wreturn-type]
 2276 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual bool lldb_private::TypeSystemZig::IsScalarType(lldb::opaque_compiler_type_t)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2509:1: warning: control reaches end of non-void function [-Wreturn-type]
 2509 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual uint32_t lldb_private::TypeSystemZig::GetTypeInfo(lldb::opaque_compiler_type_t, lldb_private::CompilerType*)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2616:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
 2616 |     }
      |     ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2618:3: note: here
 2618 |   case ZigValue::Kind::ArrayType:
      |   ^~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2636:1: warning: control reaches end of non-void function [-Wreturn-type]
 2636 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual lldb::TypeClass lldb_private::TypeSystemZig::GetTypeClass(lldb::opaque_compiler_type_t)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2683:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
 2683 |     switch (llvm::cast<ZigPointerType>(zig_type)->GetSize()) {
      |     ^~~~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2691:3: note: here
 2691 |   case ZigValue::Kind::ArrayType:
      |   ^~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:2710:1: warning: control reaches end of non-void function [-Wreturn-type]
 2710 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual lldb::Encoding lldb_private::TypeSystemZig::GetEncoding(lldb::opaque_compiler_type_t, uint64_t&)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:3064:1: warning: control reaches end of non-void function [-Wreturn-type]
 3064 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘virtual lldb::Format lldb_private::TypeSystemZig::GetFormat(lldb::opaque_compiler_type_t)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:3136:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
 3136 |     switch (llvm::cast<ZigPointerType>(zig_type)->GetSize()) {
      |     ^~~~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:3144:3: note: here
 3144 |   case ZigValue::Kind::ErrorSetType:
      |   ^~~~
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:3149:1: warning: control reaches end of non-void function [-Wreturn-type]
 3149 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘bool lldb_private::TypeSystemZig::DumpValue(lldb_private::ZigValue*, lldb_private::Stream&)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:4135:1: warning: control reaches end of non-void function [-Wreturn-type]
 4135 | }
      | ^
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp: In member function ‘bool lldb_private::TypeSystemZig::DumpTypeDecl(lldb::opaque_compiler_type_t, lldb_private::Stream&)’:
/home/zeyuanhu/projects/zig-llvm-project/lldb/source/Plugins/TypeSystem/Zig/TypeSystemZig.cpp:4395:1: warning: control reaches end of non-void function [-Wreturn-type]
 4395 | }
      | ^
make[2]: *** [tools/lldb/source/Plugins/TypeSystem/Zig/CMakeFiles/lldbPluginTypeSystemZig.dir/build.make:79: tools/lldb/source/Plugins/TypeSystem/Zig/CMakeFiles/lldbPluginTypeSystemZig.dir/TypeSystemZig.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:153611: tools/lldb/source/Plugins/TypeSystem/Zig/CMakeFiles/lldbPluginTypeSystemZig.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 82%] Building CXX object lib/Target/X86/CMakeFiles/LLVMX86CodeGen.dir/GISel/X86RegisterBankInfo.cpp.o
[ 82%] Linking CXX static library ../../libLLVMX86CodeGen.a
[ 82%] Built target LLVMX86CodeGen
make: *** [Makefile:156: all] Error 2

Mine was compiled on March 3rd, d95039d, this commit is not available now. The issue you linked was reported on Feb 1st, so I think the issue is not in lldb-zig.

/usr/include/c++/9/

Your gcc is kinda old, I think the problem is in compiler’s std. I used gcc 14 to compile the debugger. I will try to compile latest commit again, will report the results later, it will take a while :grin:

EDIT: build is successful, so I would blame old gcc.

1 Like