This is an archive of the discontinued LLVM Phabricator instance.

Add formatting support for VSCode logpoints message
ClosedPublic

Authored by yinghuitan on Oct 25 2022, 9:50 AM.

Details

Summary

https://reviews.llvm.org/D127702 adds the initial logpoints support in
lldb-vscode. This patch further improves it by:

  1. Adding a newline at the end of each log message
  2. Support most of the format specifiers like \t, \n, \x etc..

The implementation is borrowed from FormatEntity::ParseInternal(). Future
patch should merge these two implementations.

Diff Detail

Event Timeline

yinghuitan created this revision.Oct 25 2022, 9:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 25 2022, 9:50 AM
yinghuitan requested review of this revision.Oct 25 2022, 9:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 25 2022, 9:50 AM
clayborg requested changes to this revision.Oct 25 2022, 4:24 PM
clayborg added inline comments.
lldb/test/API/tools/lldb-vscode/breakpoint/TestVSCode_logpoints.py
52

Are we now auto appending a newline if there isn't one? If so we need to test this functionality. We should append on if the string doesn't end with "\n" and not if it does?

lldb/tools/lldb-vscode/BreakpointBase.cpp
47–52

Please use StringRef::find(char). Something like:

Please use StringRef::find_first_of(...) instead of iterating per character. Something like:

formatted.clear();
while (!text.empty()) {
  size_t backslash_pos = text.find_first_of('\\');
  if (backslash_pos = StringRef::npos) {
    // No more backslash chars append the rest of the string
    formatted += text.str();
    return error;
  }
  // Append anything before the backslash character.
  if (backslash_pos > 0)
    formatted += text.drop_front(backslash_pos).str();
This revision now requires changes to proceed.Oct 25 2022, 4:24 PM
yinghuitan added inline comments.Oct 26 2022, 8:52 AM
lldb/test/API/tools/lldb-vscode/breakpoint/TestVSCode_logpoints.py
52

Yes, we are always appending a newline now. I decided not to detect if string ends with "\n" or not but always append so that the behavior can be consistent.

By removing the trailing the trailing newline in testcase here we are already testing the newline behavior right? Otherwise the testcase would fail.

lldb/tools/lldb-vscode/BreakpointBase.cpp
47–52

Ah, definitely.

Address review comments

clayborg requested changes to this revision.Oct 26 2022, 6:43 PM
clayborg added inline comments.
lldb/tools/lldb-vscode/BreakpointBase.cpp
29

indentation is off here.

45

indentation is off here

53

Remove assert, this should be already tested in StringRef::find(...)

55

Is this going to append the backslash character as well? Seems like a bug. Above suggested code did this correctly I think with:

// Append anything before the backslash character.
if (backslash_pos > 0)
  formatted += text.drop_front(backslash_pos).str();

Which was always then followed by:

text = text.drop_front(); // Drop the backslash
305

I would still only push one if the output doesn't end with one and add a test for this.

if (!output.empty() && output.back() != '\n')
  output.push_back('\n'); // Ensure log message has line break.
This revision now requires changes to proceed.Oct 26 2022, 6:43 PM
yinghuitan added inline comments.Oct 27 2022, 11:13 AM
lldb/tools/lldb-vscode/BreakpointBase.cpp
29

Hmm, it is chosen by clang-format. I tried again it is still the same result. I will leave the decision to clang-format.

53

I know but I like to use assertion as a contract documentation here so that it is easier to reason later code. I can remove if you feel strong about it.

55

Thanks!

I wonder why the testcase did not catch it, turns out the testcase has a bug -- it uses escaped string for logmessage which did not trigger the code path at all!!
Fixed the testcase caused this bug to show up.

yinghuitan added inline comments.Oct 27 2022, 11:16 AM
lldb/tools/lldb-vscode/BreakpointBase.cpp
305

Ok, I can do this. But the existing tests (which has ending newline removed) should be testing this -- they do not have newline in log-message, but we still can correctly split-lines to get message boundary.

Address review comments.

clayborg requested changes to this revision.Oct 27 2022, 4:05 PM

Just a few nits and this is good to go

lldb/tools/lldb-vscode/BreakpointBase.cpp
29

no, it is fixed now, so all good.

53

I would remove it because it is already part of the StringRef::find() contract and it isn't needed.

56
This revision now requires changes to proceed.Oct 27 2022, 4:05 PM

Address review comments

What happens when we have a log message error? I see we log to the console, but we should test this. Also, if there is an error, do we fail the breakpoint setting completely or do we just not set the log message? We need a test for these cases.

Add failure case testcase

clayborg accepted this revision.Oct 31 2022, 2:47 PM
This revision is now accepted and ready to land.Oct 31 2022, 2:47 PM
This revision was automatically updated to reflect the committed changes.