Skip to content

Commit b4f23f5

Browse files
committed
Add a Formatter for .net Strings
1 parent c42c223 commit b4f23f5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

samples/NativeAOT/lldb_commands.py

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import select
55
import os
6+
import lldb
67

78
def clearjdb(debugger, command, result, internal_dict):
89
run_jdb_quit()
@@ -36,6 +37,29 @@ def run_jdb_quit():
3637
except Exception as e:
3738
print(f"Error running jdb: {e}")
3839

40+
def System_String_Summary(valobj, internal_dict):
41+
try:
42+
data_ptr = valobj.GetChildMemberWithName("_firstChar")
43+
length = valobj.GetChildMemberWithName("_stringLength").GetValueAsUnsigned()
44+
if data_ptr and length:
45+
process = valobj.GetProcess()
46+
error = lldb.SBError()
47+
address = data_ptr.GetLoadAddress()
48+
if address == lldb.LLDB_INVALID_ADDRESS:
49+
return "<invalid address>"
50+
string_data = process.ReadMemory(address, length * 2, error) # UTF-16 encoding uses 2 bytes per character
51+
if error.Success():
52+
return string_data.decode("utf-16")
53+
else:
54+
return f"<error reading memory: {error}>"
55+
except Exception as e:
56+
return f"<error reading string: {e}>"
57+
return "<empty>"
58+
59+
def __lldb_init_module(debugger, internal_dict):
60+
debugger.HandleCommand('type summary add --python-function lldb_commands.System_String_Summary "String"')
61+
print('The "formatter" command has been installed!')
62+
3963
# Allow direct execution of this script
4064
if __name__ == "__main__":
4165
run_jdb_quit()

0 commit comments

Comments
 (0)