Skip to content

[lldb] lldb can't invoke function when param or return value with anonymous namespaces class. #104712

@zhiqiangzz

Description

@zhiqiangzz

I found that if a function definition with anonymous namespaces class in the parameter or return value.
While debugging with lldb, these functions can't be invoked with errors like

error: error: function 'xxx' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage

See the following example:

namespace {
class A {
  int a;

public:
  explicit A(int a) : a{a} {}
  int func() { return a + 1; }
};

} // namespace

void *param(A /*a*/) { return nullptr; }
A *returnvalue() { return nullptr; }

int main() {
  A a = A{1};

  // avoid eliminated by compiler optimization
  (void)a.func();
  (void)param(a);
  (void)returnvalue();

  return 0;
}

Debug above program by cli lldb -- bin/lldbAnonymousVariable

(lldb) target create "bin/lldbAnonymousVariable"
Current executable set to '/Users/zhiqiangz/llvm/llvm-demo/bin/lldbAnonymousVariable' (arm64).
(lldb) b 23
Breakpoint 1: where = lldbAnonymousVariable`main + 76 at lldbAnonymousVariable.cpp:23:3, address = 0x0000000100003efc
(lldb) r
Process 33550 launched: '/Users/zhiqiangz/llvm/llvm-demo/bin/lldbAnonymousVariable' (arm64)
Process 33550 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003efc lldbAnonymousVariable`main at lldbAnonymousVariable.cpp:23:3
   20  	  (void)param(a);
   21  	  (void)returnvalue();
   22
-> 23  	  return 0;
   24  	}
(lldb) e param(a)
error: error: function 'param' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
<user expression 0>:1:1: used here
    1 | param(a)
      | ^

(lldb) e returnvalue()
error: error: function 'returnvalue' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
<user expression 1>:1:1: used here
    1 | returnvalue()
      | ^

definitely the exec program contain corresponding symbols, verified by follow command.

nm bin/lldbAnonymousVariable | c++filt | rg "param"
0000000100003f58 t param((anonymous namespace)::A)
  • machine info
uname -a
Darwin zhiqiangz-mlt 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:59 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6030 arm64
  • llvm version
llvm-config --version
19.0.0git

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions