在Xcode自定义lldb命令pjson

目录

在主目录下新建.lldbinit和print_json.py

touch ~/.lldbinit
touch ~/print_json.py

在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')


在.lldbinit中输入以下代码

command script import ~/print_json.py

在xcode中使用

pjson dic

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦