llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Could `__counted_by(...)` and similar emit DWARF to let LLDB and GDB render arrays?

Open chandlerc opened this issue 8 months ago • 6 comments

Idea from: https://hachyderm.io/@seanmiddleditch/116254641521951864

This seems like a pretty great idea, especially for the new annotations that are part of the direction of -fbounds-safety to add extra value for folks adopting these annotations!

chandlerc avatar Mar 19 '26 07:03 chandlerc

@llvm/issue-subscribers-debuginfo

Author: Chandler Carruth (chandlerc)

Idea from: https://hachyderm.io/@seanmiddleditch/116254641521951864

This seems like a pretty great idea, especially for the new annotations that are part of the direction of -fbounds-safety to add extra value for folks adopting these annotations!

llvmbot avatar Mar 19 '26 15:03 llvmbot

CC @adrian-prantl @delcypher

rapidsna avatar Mar 19 '26 16:03 rapidsna

Just to clarify, the idea is to have DWARF record the fact that some pointer was annotated with __counted_by so the debugger could do cleverer things with it. When I initially read the title I mistakenly thought this had to do with emitting the size of actual C-arrays (the DW_AT_count on DW_TAG_subrange_types) more accurately.

Worth noting that Apple LLDB has special provisions for pointers annotated with __counted_by: https://github.com/swiftlang/llvm-project/blob/3a6c5e9f3dd7e294f9ab0dca7567bfbe63e8b8f3/lldb/source/DataFormatters/FormatManager.cpp#L819-L822

Such pointer would be rendered as follows in LLDB:

(int *) buf = (ptr: 0x00016fdfe940 counted_by: len)

Looks like we just parse it out of the type of the sized pointer:

0x000000cd:     DW_TAG_variable                                                                                                          
                  DW_AT_location        (DW_OP_fbreg -64)                                                                                
                  DW_AT_name    ("buf")                                                                                                  
                  DW_AT_type    (0x0000013e "__bounds_safety::counted_by::len")               

Michael137 avatar Mar 19 '26 17:03 Michael137

fyi, __counted_by attribute is available llvm/llvm-project. May be it's good time to upstream the debugger support?

rapidsna avatar Mar 19 '26 19:03 rapidsna

fyi, __counted_by attribute is available llvm/llvm-project. May be it's good time to upstream the debugger support?

Yea that'd make sense. Though if this attribute is going to be in the C standard, we probably don't want to rely on the name that the compiler chooses to emit into debug-info, and rather encode it into DWARF properly. Using existing DWARF attributes one could possibly represent this as:

DW_TAG_pointer_type
   DW_AT_type ("int*")
   DW_AT_count (0x123 "len") <<< reference to the `len` variable DIE

The DWARF spec already allows DW_AT_count on type modifiers (currently only for DW_TAG_shared_type though, see 5.3 Type Modifiers of the DWARFv5 spec). For other kinds of bounds-safety pointers that could also be extended to use DW_AT_lower_bound/DW_AT_upper_bound attributes.

Michael137 avatar Mar 20 '26 11:03 Michael137

Just for reference, this is the "textual" debug info that swift/llvm-project currently emits:

   15  	    int *__counted_by(len) buf;

...

0x00000128: DW_TAG_typedef
              DW_AT_type	(0x0000012e "int *")
              DW_AT_name	("__bounds_safety::counted_by::len")

@Michael137 's suggestion is definitely nicer, especially if this becomes part of C. But until then upstreaming the code into llvm.org seems perfectly fine while it is an extension. I'd be happy to review patches for this!

adrian-prantl avatar Mar 20 '26 23:03 adrian-prantl