[Lldb-commits] [lldb] [lldb] Support custom printf formatting for variables (PR #81196)
Dave Lee via lldb-commits
lldb-commits at lists.llvm.org
Tue Feb 13 13:34:20 PST 2024
https://github.com/kastiglione updated https://github.com/llvm/llvm-project/pull/81196
>From 81a2034ff2b41e30a1f5b82c86b4d5d4c429ed52 Mon Sep 17 00:00:00 2001
From: Dave Lee <davelee.com at gmail.com>
Date: Thu, 8 Feb 2024 13:59:12 -0800
Subject: [PATCH 1/2] [lldb] Support custom printf formatting for variables
---
lldb/source/Core/FormatEntity.cpp | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index fa5eadc6ff4e9a..0e929203935304 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -883,8 +883,29 @@ static bool DumpValue(Stream &s, const SymbolContext *sc,
}
if (!is_array_range) {
- LLDB_LOGF(log,
- "[Debugger::FormatPrompt] dumping ordinary printable output");
+ if (!entry.printf_format.empty()) {
+ auto type_info = target->GetTypeInfo();
+ if (type_info & eTypeIsInteger) {
+ if (type_info & eTypeIsSigned) {
+ bool success = false;
+ auto integer = target->GetValueAsSigned(0, &success);
+ if (success) {
+ LLDB_LOGF(log, "dumping using printf format");
+ s.Printf(entry.printf_format.c_str(), integer);
+ return true;
+ }
+ } else {
+ bool success = false;
+ auto integer = target->GetValueAsUnsigned(0, &success);
+ if (success) {
+ LLDB_LOGF(log, "dumping using printf format");
+ s.Printf(entry.printf_format.c_str(), integer);
+ return true;
+ }
+ }
+ }
+ }
+ LLDB_LOGF(log, "dumping ordinary printable output");
return target->DumpPrintableRepresentation(s, val_obj_display,
custom_format);
} else {
>From 335ab1de4b39257e3bbb3bd969a0dd6991747558 Mon Sep 17 00:00:00 2001
From: Dave Lee <davelee.com at gmail.com>
Date: Tue, 13 Feb 2024 13:26:35 -0800
Subject: [PATCH 2/2] Factor out DumpValueWithPrintf; Add test
---
lldb/source/Core/FormatEntity.cpp | 45 +++++++++++--------
.../custom-printf-summary/Makefile | 2 +
.../TestCustomPrintfSummary.py | 11 +++++
.../custom-printf-summary/main.c | 13 ++++++
4 files changed, 52 insertions(+), 19 deletions(-)
create mode 100644 lldb/test/API/functionalities/data-formatter/custom-printf-summary/Makefile
create mode 100644 lldb/test/API/functionalities/data-formatter/custom-printf-summary/TestCustomPrintfSummary.py
create mode 100644 lldb/test/API/functionalities/data-formatter/custom-printf-summary/main.c
diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index 0e929203935304..57a05507d844cf 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -658,6 +658,25 @@ static char ConvertValueObjectStyleToChar(
return '\0';
}
+static bool DumpValueWithPrintf(Stream &s, llvm::StringRef format,
+ ValueObject &target) {
+ auto type_info = target.GetTypeInfo();
+ if (type_info & eTypeIsInteger) {
+ if (type_info & eTypeIsSigned) {
+ if (auto integer = target.GetValueAsSigned()) {
+ s.Printf(format.data(), *integer);
+ return true;
+ }
+ } else {
+ if (auto integer = target.GetValueAsUnsigned()) {
+ s.Printf(format.data(), *integer);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
static bool DumpValue(Stream &s, const SymbolContext *sc,
const ExecutionContext *exe_ctx,
const FormatEntity::Entry &entry, ValueObject *valobj) {
@@ -884,25 +903,13 @@ static bool DumpValue(Stream &s, const SymbolContext *sc,
if (!is_array_range) {
if (!entry.printf_format.empty()) {
- auto type_info = target->GetTypeInfo();
- if (type_info & eTypeIsInteger) {
- if (type_info & eTypeIsSigned) {
- bool success = false;
- auto integer = target->GetValueAsSigned(0, &success);
- if (success) {
- LLDB_LOGF(log, "dumping using printf format");
- s.Printf(entry.printf_format.c_str(), integer);
- return true;
- }
- } else {
- bool success = false;
- auto integer = target->GetValueAsUnsigned(0, &success);
- if (success) {
- LLDB_LOGF(log, "dumping using printf format");
- s.Printf(entry.printf_format.c_str(), integer);
- return true;
- }
- }
+ if (DumpValueWithPrintf(s, entry.printf_format, *target)) {
+ LLDB_LOGF(log, "dumping using printf format");
+ return true;
+ } else {
+ LLDB_LOG(log,
+ "unsupported printf format '{0}' - for type info flags {1}",
+ entry.printf_format, target->GetTypeInfo());
}
}
LLDB_LOGF(log, "dumping ordinary printable output");
diff --git a/lldb/test/API/functionalities/data-formatter/custom-printf-summary/Makefile b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/Makefile
new file mode 100644
index 00000000000000..c9319d6e6888a4
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/Makefile
@@ -0,0 +1,2 @@
+C_SOURCES := main.c
+include Makefile.rules
diff --git a/lldb/test/API/functionalities/data-formatter/custom-printf-summary/TestCustomPrintfSummary.py b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/TestCustomPrintfSummary.py
new file mode 100644
index 00000000000000..7d196c66caaa79
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/TestCustomPrintfSummary.py
@@ -0,0 +1,11 @@
+import lldb
+from lldbsuite.test.lldbtest import *
+import lldbsuite.test.lldbutil as lldbutil
+
+
+class TestCase(TestBase):
+ def test(self):
+ self.build()
+ lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.c"))
+ self.runCmd("type summary add -s '${var.ubyte%%2.2X}${var.sbyte%%2.2X}!' Bytes")
+ self.expect("v bytes", substrs=[" = 1001!"])
diff --git a/lldb/test/API/functionalities/data-formatter/custom-printf-summary/main.c b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/main.c
new file mode 100644
index 00000000000000..8f92b9dafd32f3
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/custom-printf-summary/main.c
@@ -0,0 +1,13 @@
+#include <stdint.h>
+#include <stdio.h>
+
+struct Bytes {
+ uint8_t ubyte;
+ int8_t sbyte;
+};
+
+int main() {
+ struct Bytes bytes = {0x10, 0x01};
+ (void)bytes;
+ printf("break here\n");
+}
More information about the lldb-commits
mailing list