This is an archive of the discontinued LLVM Phabricator instance.

[lldb/Target] Slide source display for artificial locations at function start
ClosedPublic

Authored by mib on Dec 7 2021, 7:31 PM.

Details

Summary

It can happen that a line entry reports that some source code is located
at line 0. In DWARF, line 0 is a special location which indicates that
code has no 1-1 mapping with source.

When stopping in one of those artificial locations, lldb doesn't know which
line to display and shows the beginning of the file instead.

This patch mitigates this behaviour by checking if the current symbol context
of the line entry has a matching function, in which case, it slides the
source listing to the start of that function.

This patch also shows the user a warning explaining why lldb couldn't
show sources at that location.

rdar://83118425

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>

Diff Detail

Event Timeline

mib requested review of this revision.Dec 7 2021, 7:31 PM
mib created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2021, 7:31 PM
mib updated this revision to Diff 392622.Dec 7 2021, 7:33 PM
mib added reviewers: aprantl, jingham.

Side note: The src_file is not to be trusted/used either - once line 0 is specified, nothing else in that line entry is valid. LLVM lets the previous line entries file persist because this reduces encoding size (by not having to switch all the fields in the line table - only the line number - back and forth over a line number 0 area). eg: previous entry in the line table might be from a #include of some code (as in clang/llvm's use of .def files, for instance) into a function, or from some inlining above the line 0 region, etc.

So maybe "artificial location in function <X>" might be suitable? (the actual code at line 0 might still be from some inlining (LLVM does try to scope it - so the instruction should have the nearest common scope (in terms of lexical scopes or inlined functions) so if A has B inlined, B has C and D inlined into it and some code is commoned between C and D, it should be attributed to the inlined region of B - but if it's hoisted out of a basic block, I don't think we can properly attribute it to any scope, and so we'd have to attribute it to A in the scope DIE information (in all these cases it'd still have line 0, though).

Side note: The src_file is not to be trusted/used either - once line 0 is specified, nothing else in that line entry is valid. LLVM lets the previous line entries file persist because this reduces encoding size (by not having to switch all the fields in the line table - only the line number - back and forth over a line number 0 area). eg: previous entry in the line table might be from a #include of some code (as in clang/llvm's use of .def files, for instance) into a function, or from some inlining above the line 0 region, etc.

So maybe "artificial location in function <X>" might be suitable? (the actual code at line 0 might still be from some inlining (LLVM does try to scope it - so the instruction should have the nearest common scope (in terms of lexical scopes or inlined functions) so if A has B inlined, B has C and D inlined into it and some code is commoned between C and D, it should be attributed to the inlined region of B - but if it's hoisted out of a basic block, I don't think we can properly attribute it to any scope, and so we'd have to attribute it to A in the scope DIE information (in all these cases it'd still have line 0, though).

Thanks for clarifying @DavidSpickett ! I'll change the warning accordingly.

mib updated this revision to Diff 392834.Dec 8 2021, 10:54 AM
mib retitled this revision from [lldb/Target] Slide source display for artificial location at function start to [lldb/Target] Slide source display for artificial locations at function start.

Change warning to specify function name instead of source file name when possible.

jingham requested changes to this revision.Dec 8 2021, 12:13 PM

I don't think the num_lines check is handled correctly.

lldb/source/Target/StackFrame.cpp
1898–1912

I don't think it is correct to include !num_lines in this if. You get 0 back from DIsplaySourceLines... if you found a good line table entry but the source file was not available. But that doesn't mean that this is artificial code.

You also shouldn't remove the TO DO since you didn't do it.

1907

This seems a confusing way to say "but we can't tell what function it is in..."

This revision now requires changes to proceed.Dec 8 2021, 12:13 PM
mib marked 2 inline comments as done.Dec 8 2021, 1:25 PM
mib added inline comments.
lldb/source/Target/StackFrame.cpp
1898–1912

Right (wrt to the condition).

I reverted the TODO and will take care of it in a separate patch.

mib updated this revision to Diff 392915.Dec 8 2021, 1:25 PM
mib marked an inline comment as done.

Address @jingham feedbacks.

mib updated this revision to Diff 392943.Dec 8 2021, 3:02 PM

Update the condition for printing the warning.

mib updated this revision to Diff 392947.Dec 8 2021, 3:15 PM

Dropping the LLDB_INVALID_LINE_NUMBER check as it should only occur when we have no debug info

jingham accepted this revision.Dec 8 2021, 3:18 PM

That looks right.

This revision is now accepted and ready to land.Dec 8 2021, 3:18 PM
JDevlieghere added inline comments.Dec 8 2021, 3:20 PM
lldb/test/API/source-manager/TestSourceManager.py
279

Should this print the "in function" part too? If so, why not check for it here?

mib updated this revision to Diff 392949.Dec 8 2021, 3:23 PM
mib marked an inline comment as done.

Address @JDevlieghere comment regarding test.

Thanks for clarifying @DavidSpickett ! I'll change the warning accordingly.

Too many Davids :)

Thanks! I have some late wording suggestions.

lldb/source/Target/StackFrame.cpp
1904

This message might be confusing to developers. End-users won't know what "PC" and "artificial" means. What do you think about:

"Note: this address is compiler-generated code in function %s that has no source code associated with it."

?

1909

"Note: this address is compiler-generated code that has no source code associated with it."

mib added a comment.Dec 9 2021, 2:38 PM

Thanks for clarifying @DavidSpickett ! I'll change the warning accordingly.

Too many Davids :)

😅😅