This is an archive of the discontinued LLVM Phabricator instance.

Support debug info for alias variable
ClosedPublic

Authored by kavitha-natarajan on Mar 4 2022, 4:29 AM.

Details

Summary

This is in continuation with the patch posted earlier for bug-50052:

https://reviews.llvm.org/D103131

For the below testcase, when compiled with clang compiler, debugger is not able to print alias variable type or value.
$ cat test.c
int oldname = 1;
extern int newname attribute((alias("oldname")));

int main ()
{

return 0;

}

$ clang -g -O0 test.c

$ gdb a.out
(gdb) ptype oldname
type = int
(gdb) ptype newname
type = <data variable, no debug info>
(gdb) p oldname
$1 = 1
(gdb) p newname
'newname' has unknown type; cast it to its declared type

This is because clang is not emitting dwarf information for alias variable. The above mentioned patch supports clang to emit debug info for alias variable as imported entity (DW_TAG_imported_declaration). However, gdb cannot handle the imported declaration for alias variables. GCC emits debug info for alias variables as DW_TAG_variable which gdb can handle. The discussions in the above bug report and patch review links talk about why it is appropriate to emit alias variable as DW_TAG_imported_declaration rather than DW_TAG_variable. Refer section "3.2.3 Imported (or Renamed) Declaration Entries" in DWARF 5 specification.

In the clang patch, CGDebugInfo.cpp:EmitGlobalAlias() function is rewritten to handle nested (recursive) imported declaration and developed a testcase as well. A corresponding gdb patch that can handle DW_TAG_imported_declaration as alias variables is also developed and will be posted to gdb community for review in parallel.

After clang and gdb fixes:

$ gdb a.out
(gdb) ptype oldname
type = int
(gdb) ptype newname
type = int
(gdb) p oldname
$1 = 1
(gdb) p newname
$2 = 1
(gdb)

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMar 4 2022, 4:29 AM
kavitha-natarajan requested review of this revision.Mar 4 2022, 4:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 4 2022, 4:29 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
dblaikie added a comment.EditedMar 4 2022, 10:38 AM

[removing comment - this was intended for a different issue (leaving this here rather than deleting so it's not confusing when people come from the email threads, etc)]

Broad question about debug info for aliases: How's this proposed solution compare/contrast to GCC's behavior?

Broad question about debug info for aliases: How's this proposed solution compare/contrast to GCC's behavior?

Aaand, I see that was described in thhe patch description. Sorry for not reading...

Right, paging in all the context - this review hinges on whether gdb/lldb can handle/be made to handle this sort of debug info correctly.

Rebased and applied pre-merge check formatting.

Broad question about debug info for aliases: How's this proposed solution compare/contrast to GCC's behavior?

Aaand, I see that was described in thhe patch description. Sorry for not reading...

Right, paging in all the context - this review hinges on whether gdb/lldb can handle/be made to handle this sort of debug info correctly.

I have posted a gdb patch to handle this change for review:
https://sourceware.org/pipermail/gdb-patches/2022-March/186329.html

lldb can handle this debug info correctly.

Broad question about debug info for aliases: How's this proposed solution compare/contrast to GCC's behavior?

Aaand, I see that was described in thhe patch description. Sorry for not reading...

Right, paging in all the context - this review hinges on whether gdb/lldb can handle/be made to handle this sort of debug info correctly.

I have posted a gdb patch to handle this change for review:
https://sourceware.org/pipermail/gdb-patches/2022-March/186329.html

lldb can handle this debug info correctly.

Thanks for the details - can you ping this thread once the gdb thread has progressed/seems like it's moving in this direction?

Maybe APple folks would want to move forward with this only when targeting lldb, though - which might unblock this patch. @aprantl @JDevlieghere ? What do you folks think?

This looks mostly fine to me, I have a couple of superficial comments inline.

clang/lib/CodeGen/CGDebugInfo.cpp
3862

Nit: The LLVM coding style wants all comments to be full sentences, including a trailing .

3869

When would we enter a nullptr into the cache?

5422

See LLVM coding style

clang/lib/CodeGen/CodeGenModule.cpp
1506

std::find()?

@aprantl Updated the comments following LLVM coding style.

clang/lib/CodeGen/CGDebugInfo.cpp
3869

In this change, only llvm::DIImportedEntity entries are pushed to ImportedDeclCache. So it will never return nullptr. It is only a guard to not to fall into the case of no definition in case of non llvm::DIImportedEntity in the cache (undesired and to be handled by the caller).

clang/lib/CodeGen/CodeGenModule.cpp
1506

MangledDeclNames is a llvm::MapVector implementation, so can't use std::find(). The key for MangledDeclNames MapVector is GlobalDecl and StringRef is the value. llvm::Mapvector::find() is search by key. Here we are doing search by value.

aprantl added inline comments.Mar 18 2022, 4:05 PM
clang/lib/CodeGen/CGDebugInfo.cpp
3870

if we don't expect anything but non-null DINodes to be in the cache, then this whole condition should be

return cast<llvm::DINode>(N);

That will assert if N is null.

5433

LLVM coding style says to remove the redundant else after a return.

5443

.

@aprantl, thanks for your comments. Updated the patch with the fixes.

@dblaikie, haven't got any response from gdb reviewers about the gdb patch yet.

Thanks for the details - can you ping this thread once the gdb thread has progressed/seems like it's moving in this direction?

@dblaikie, received comments for the gdb patch https://sourceware.org/pipermail/gdb-patches/2022-March/186960.html and the next message with my response. Can you please look through?

dblaikie accepted this revision.Apr 6 2022, 1:09 PM

Thanks for the details - can you ping this thread once the gdb thread has progressed/seems like it's moving in this direction?

@dblaikie, received comments for the gdb patch https://sourceware.org/pipermail/gdb-patches/2022-March/186960.html and the next message with my response. Can you please look through?

Sure, sounds like things are moving in the right direction there - so from my perspective, this sounds good. Please wait for @aprantl's sign off as well.

This revision is now accepted and ready to land.Apr 6 2022, 1:09 PM
aprantl accepted this revision.Apr 6 2022, 2:08 PM
thakis added a subscriber: thakis.Apr 7 2022, 10:49 AM

This breaks tests on mac: https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket/8817534773179515649/+/u/package_clang/stdout?format=raw

Script:
 --
 : 'RUN: at line 1';   /opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/clang -cc1 -internal-isystem /opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/lib/clang/15.0.0/include -nostdsysteminc -emit-llvm -debug-info-kind=limited /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c -o - | /opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/FileCheck /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c
 --
 Exit Code: 2
 
 Command Output (stderr):
 --
 /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:10:27: error: aliases are not supported on darwin
 extern int __attribute__((alias("aliased_global"))) __global_alias;
                           ^
 /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:14:27: error: aliases are not supported on darwin
 extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
                           ^
 /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:15:27: error: aliases are not supported on darwin
 extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
                           ^
 3 errors generated.
 FileCheck error: '<stdin>' is empty.
 FileCheck command line:  /opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/FileCheck /opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c
 
 --
 
 ********************
 Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
 ********************
 Failed Tests (1):
   Clang :: CodeGen/debug-info-alias.c

Probably easiest to pass an explicit triple?

Aliases are not supported on mac. Below commit has made the test to be skipped for mac. Thanks @abrachet for the quick fix.

commit 50de659adcc19c4c197ba5e9a7719325af7151ee
Author: Alex Brachet <abrachet@google.com>
Date: Thu Apr 7 18:19:54 2022 +0000

[clang] Use -triple, not -target for %clang_cc1

commit 3329dae5cb8a8c91c518dd87c09e88c4fad507bd
Author: Alex Brachet <abrachet@google.com>
Date: Thu Apr 7 18:17:29 2022 +0000

[clang] Fix macos build broken after D120989