[Lldb-commits] [PATCH] D156732: Display PC instead of for stack trace in vscode

2023-07-31 Thread Tom Yang via Phabricator via lldb-commits
zhyty created this revision.
Herald added a project: All.
zhyty requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

It isn't useful for users to see "" as a stack trace when lldb fails 
to symbolicate a stack frame. I've replaced "" with the value of the 
program counter instead.

Test Plan:

To test this, I opened a target that lldb fails to symbolicate in
VSCode, and observed in the CALL STACK section that instead of being
shown as "", those stack frames are represented by their
program counters.

I also ran `lldb-dotest -p TestVSCode` and saw that the tests passed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156732

Files:
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -762,8 +762,14 @@
   const char *func_name = frame.GetFunctionName();
   if (func_name)
 frame_name = func_name;
-  else
-frame_name = "";
+  else {
+// If the function name is unavailable, display the pc address as a 
16-digit
+// hex string.
+frame_name.clear();
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+os.flush();
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -762,8 +762,14 @@
   const char *func_name = frame.GetFunctionName();
   if (func_name)
 frame_name = func_name;
-  else
-frame_name = "";
+  else {
+// If the function name is unavailable, display the pc address as a 16-digit
+// hex string.
+frame_name.clear();
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+os.flush();
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D156970: Fix crash in lldb-vscode when missing function name

2023-08-03 Thread Tom Yang via Phabricator via lldb-commits
zhyty created this revision.
Herald added a project: All.
zhyty requested review of this revision.
Herald added subscribers: lldb-commits, wangpc.
Herald added a project: LLDB.

In cases where the PC has no function name, lldb-vscode crashes.

`lldb::SBFrame::GetDisplayFunctionName()` returns a `nullptr`, and when we
attempt to construct an `std::string`, it raises an exception.

Test plan:
This can be observed with creating a test file (credit to @clayborg for the
example):

  int main() {
typedef void (*FooCallback)();
FooCallback foo_callback = (FooCallback)0;
foo_callback(); // Crash at zero!
return 0;
  }

and attempting to debug the file through VSCode.

I add a test case in D156732  which should 
cover this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156970

Files:
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -696,7 +696,11 @@
   int64_t frame_id = MakeVSCodeFrameID(frame);
   object.try_emplace("id", frame_id);
 
-  std::string frame_name = frame.GetDisplayFunctionName();
+  // `function_name` can be a nullptr, which throws an error when assigned to 
an
+  // `std::string`.
+  const char *function_name = frame.GetDisplayFunctionName();
+  std::string frame_name =
+  function_name == nullptr ? std::string() : function_name;
   if (frame_name.empty())
 frame_name = "";
   bool is_optimized = frame.GetFunction().GetIsOptimized();


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -696,7 +696,11 @@
   int64_t frame_id = MakeVSCodeFrameID(frame);
   object.try_emplace("id", frame_id);
 
-  std::string frame_name = frame.GetDisplayFunctionName();
+  // `function_name` can be a nullptr, which throws an error when assigned to an
+  // `std::string`.
+  const char *function_name = frame.GetDisplayFunctionName();
+  std::string frame_name =
+  function_name == nullptr ? std::string() : function_name;
   if (frame_name.empty())
 frame_name = "";
   bool is_optimized = frame.GetFunction().GetIsOptimized();
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D156732: Display PC instead of for stack trace in vscode

2023-08-03 Thread Tom Yang via Phabricator via lldb-commits
zhyty updated this revision to Diff 546738.
zhyty added a comment.

- added a test case
- rebased this commit on top of D156970 , 
which fixes a crash preventing me from testing this feature
- addressed other comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156732/new/

https://reviews.llvm.org/D156732

Files:
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
  
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/a.out
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 
16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_testcase
+from lldbsuite.test import lldbtest, lldbutil
+
+
+class 
TestVSCode_stackTraceMissingFunctionName(lldbvscode_testcase.VSCodeTestCaseBase):
+@skipIfWindows
+@skipIfRemote
+def test_missingFunctionName(self):
+"""
+Test that the stack frame without a function name is given its pc in 
the response.
+"""
+program = self.getBuildArtifact("a.out")
+self.build_and_launch(program)
+
+self.continue_to_next_stop()
+frame_without_function_name = self.get_stackFrames()[0]
+self.assertEquals(frame_without_function_name["name"], 
"0x")
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_

[Lldb-commits] [PATCH] D156970: Fix crash in lldb-vscode when missing function name

2023-08-03 Thread Tom Yang via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9a3f0cd717f6: Fix crash in lldb-vscode when missing function 
name (authored by zhyty).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156970/new/

https://reviews.llvm.org/D156970

Files:
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -696,7 +696,11 @@
   int64_t frame_id = MakeVSCodeFrameID(frame);
   object.try_emplace("id", frame_id);
 
-  std::string frame_name = frame.GetDisplayFunctionName();
+  // `function_name` can be a nullptr, which throws an error when assigned to 
an
+  // `std::string`.
+  const char *function_name = frame.GetDisplayFunctionName();
+  std::string frame_name =
+  function_name == nullptr ? std::string() : function_name;
   if (frame_name.empty())
 frame_name = "";
   bool is_optimized = frame.GetFunction().GetIsOptimized();


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -696,7 +696,11 @@
   int64_t frame_id = MakeVSCodeFrameID(frame);
   object.try_emplace("id", frame_id);
 
-  std::string frame_name = frame.GetDisplayFunctionName();
+  // `function_name` can be a nullptr, which throws an error when assigned to an
+  // `std::string`.
+  const char *function_name = frame.GetDisplayFunctionName();
+  std::string frame_name =
+  function_name == nullptr ? std::string() : function_name;
   if (frame_name.empty())
 frame_name = "";
   bool is_optimized = frame.GetFunction().GetIsOptimized();
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D156732: Display PC instead of for stack trace in vscode

2023-08-03 Thread Tom Yang via Phabricator via lldb-commits
zhyty updated this revision to Diff 546979.
zhyty added a comment.

remove a.out


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156732/new/

https://reviews.llvm.org/D156732

Files:
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
  
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 
16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_testcase
+from lldbsuite.test import lldbtest, lldbutil
+
+
+class 
TestVSCode_stackTraceMissingFunctionName(lldbvscode_testcase.VSCodeTestCaseBase):
+@skipIfWindows
+@skipIfRemote
+def test_missingFunctionName(self):
+"""
+Test that the stack frame without a function name is given its pc in 
the response.
+"""
+program = self.getBuildArtifact("a.out")
+self.build_and_launch(program)
+
+self.continue_to_next_stop()
+frame_without_function_name = self.get_stackFrames()[0]
+self.assertEquals(frame_without_function_name["name"], 
"0x")
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_testcase
+from lldbsuite.test import lldbtest, lldbutil
+
+
+class TestVSCode_stackTraceMissingFunctionName(lldbvscode_testcase.VSCodeTestCaseBase):
+@skipIfWindows
+@skipIfRemote
+def test_missingFunctionName(self):
+"""

[Lldb-commits] [PATCH] D156732: Display PC instead of for stack trace in vscode

2023-08-04 Thread Tom Yang via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG786bab433469: Display PC instead of  for 
stack trace in vscode (authored by zhyty).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156732/new/

https://reviews.llvm.org/D156732

Files:
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
  
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
  lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
  lldb/tools/lldb-vscode/JSONUtils.cpp


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 
16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ 
lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_testcase
+from lldbsuite.test import lldbtest, lldbutil
+
+
+class 
TestVSCode_stackTraceMissingFunctionName(lldbvscode_testcase.VSCodeTestCaseBase):
+@skipIfWindows
+@skipIfRemote
+def test_missingFunctionName(self):
+"""
+Test that the stack frame without a function name is given its pc in 
the response.
+"""
+program = self.getBuildArtifact("a.out")
+self.build_and_launch(program)
+
+self.continue_to_next_stop()
+frame_without_function_name = self.get_stackFrames()[0]
+self.assertEquals(frame_without_function_name["name"], 
"0x")
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules


Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -701,8 +701,12 @@
   const char *function_name = frame.GetDisplayFunctionName();
   std::string frame_name =
   function_name == nullptr ? std::string() : function_name;
-  if (frame_name.empty())
-frame_name = "";
+  if (frame_name.empty()) {
+// If the function name is unavailable, display the pc address as a 16-digit
+// hex string, e.g. "0x00012345"
+llvm::raw_string_ostream os(frame_name);
+os << llvm::format_hex(frame.GetPC(), 18);
+  }
   bool is_optimized = frame.GetFunction().GetIsOptimized();
   if (is_optimized)
 frame_name += " [opt]";
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/main.cpp
@@ -0,0 +1,6 @@
+int main() {
+  typedef void (*FooCallback)();
+  FooCallback foo_callback = (FooCallback)0;
+  foo_callback(); // Crash at zero!
+  return 0;
+}
Index: lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
===
--- /dev/null
+++ lldb/test/API/tools/lldb-vscode/stackTraceMissingFunctionName/TestVSCode_stackTraceMissingFunctionName.py
@@ -0,0 +1,26 @@
+"""
+Test lldb-vscode stack trace response
+"""
+
+
+import vscode
+from lldbsuite.test.decorators import *
+import os
+
+import lldbvscode_testcase
+from lldbsuite.test import lldbtest, lldbutil
+
+
+class TestVSCode_stackTraceMissingFunctionName(lldbvscode_testcase.VSCodeTe