touch ~/.lldbinit
touch ~/print_json.py
import lldb
def print_json(debugger, command, result, internal_dict):
target = debugger.GetSelectedTarget()
process = target.GetProcess()
thread = process.GetSelectedThread()
frame = thread.GetSelectedFrame()
# Evaluate the expression
val = frame.EvaluateExpression(command)
# Check if the value is valid
if val.IsValid():
# Prepare the expression to convert dictionary to JSON string
expr = f'''
@import Foundation;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:(id){val.GetValue()} options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString;
'''
# Evaluate the JSON conversion expression
json_val = frame.EvaluateExpression(expr)
if json_val.IsValid():
# Print the JSON string
print(json_val.GetObjectDescription())
else:
print("Error: Unable to convert dictionary to JSON.")
else:
print("Error: Invalid dictionary object.")
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('command script add -f print_json.print_json pjson')
command script import ~/print_json.py