-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lldb] Support custom LLVM formatting for variables (#81196)
Adds support for applying LLVM formatting to variables. The reason for this is to support cases such as the following. Let's say you have two separate bytes that you want to print as a combined hex value. Consider the following summary string: ``` ${var.byte1%x}${var.byte2%x} ``` The output of this will be: `0x120x34`. That is, a `0x` prefix is unconditionally applied to each byte. This is unlike printf formatting where you must include the `0x` yourself. Currently, there's no way to do this with summary strings, instead you'll need a summary provider in python or c++. This change introduces formatting support using LLVM's formatter system. This allows users to achieve the desired custom formatting using: ``` ${var.byte1:x-}${var.byte2:x-} ``` Here, each variable is suffixed with `:x-`. This is passed to the LLVM formatter as `{0:x-}`. For integer values, `x` declares the output as hex, and `-` declares that no `0x` prefix is to be used. Further, one could write: ``` ${var.byte1:x-2}${var.byte2:x-2} ``` Where the added `2` results in these bytes being written with a minimum of 2 digits. An alternative considered was to add a new format specifier that would print hex values without the `0x` prefix. The reason that approach was not taken is because in addition to forcing a `0x` prefix, hex values are also forced to use leading zeros. This approach lets the user have full control over formatting.
- Loading branch information
1 parent
40083cf
commit 7a8d15e
Showing
5 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
lldb/test/API/functionalities/data-formatter/custom-printf-summary/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
C_SOURCES := main.c | ||
include Makefile.rules |
20 changes: 20 additions & 0 deletions
20
...t/API/functionalities/data-formatter/custom-printf-summary/TestCustomSummaryLLVMFormat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import lldb | ||
from lldbsuite.test.lldbtest import * | ||
import lldbsuite.test.lldbutil as lldbutil | ||
|
||
|
||
class TestCase(TestBase): | ||
def test_raw_bytes(self): | ||
self.build() | ||
lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.c")) | ||
self.runCmd("type summary add -s '${var.ubyte:x-2}${var.sbyte:x-2}!' Bytes") | ||
self.expect("v bytes", substrs=[" = 3001!"]) | ||
|
||
def test_bad_format(self): | ||
self.build() | ||
lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.c")) | ||
self.expect( | ||
"type summary add -s '${var.ubyte:y}!' Bytes", | ||
error=True, | ||
substrs=["invalid llvm format"], | ||
) |
13 changes: 13 additions & 0 deletions
13
lldb/test/API/functionalities/data-formatter/custom-printf-summary/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
struct Bytes { | ||
uint8_t ubyte; | ||
int8_t sbyte; | ||
}; | ||
|
||
int main() { | ||
struct Bytes bytes = {0x30, 0x01}; | ||
(void)bytes; | ||
printf("break here\n"); | ||
} |