CatalinM CatalinM - 7 months ago 47
C Question

LLDB: how to make a custom function that pretty prints a dynamic array of structs

I have an dynamically allocated array of structs that I need to print inside LLDB, while in a breakpoint. All this happening in some pure C code outside ObjC.

I've written a static function that does the printing job and while I can call it inside LLDB, it will only print about 60 lines before silently stopping. Sometimes, it will not print anything unless I press Enter again inside LLDB. I assume this is due to threading somehow, but I tried to do a dispatch_async inside that function without any success - it wouldn't print anything.

Therefore, I am looking into alternative solutions (maybe a Python script) to get a pretty print of that array as it is vital for my debugging efforts.


Are you talking about something like:

struct MyStruct {/* some stuff */};
struct MyStruct *struct_ptr = malloc(100 * sizeof(struct MyStruct));

and you want to print struct_ptr as if you had done:

struct MyStruct struct_arr[100];

If you are on lldb before Xcode 8.0, you would do this with:

(lldb) memory read -c 100 -t MyStruct struct_ptr

And if you have one of the Xcode 8.0 beta's, you can use the new parray command:

(lldb) parray 100 struct_ptr