16

I am trying to debug my Objective-C program, and I need to print my unsigned long long variable in hex. I am using the lldb debugger.

In order to print short as hex, you can use this:

(lldb) type format add --format hex short
(lldb) print bit
(short) $11 = 0x0000

However, I can't make it work for unsigned long long.

// failed attempts:
(lldb) type format add --format hex (unsigned long long)
(lldb) type format add --format hex unsigned long long
(lldb) type format add --format hex unsigned decimal
(lldb) type format add --format hex long long
(lldb) type format add --format hex long
(lldb) type format add --format hex int

I am running an iOS app on simulator, if that makes any difference.

Mazyod
  • 21,361
  • 9
  • 86
  • 147

3 Answers3

40

You can use format letters. Link to GDB docs (works for LLDB too): https://sourceware.org/gdb/current/onlinedocs/gdb/Output-Formats.html#Output-Formats

(lldb) p a
(unsigned long long) $0 = 10
(lldb) p/x a
(unsigned long long) $1 = 0x000000000000000a
Vlad
  • 1,418
  • 1
  • 14
  • 19
  • 3
    Note that whereas gdb accepts a space in between `p` and `/x`, lldb does not, so `p /x` works in gdb, but in lldb it has to be `p/x`. – Paul R Feb 14 '17 at 13:39
  • @Vlad can you point to more specific link to the all the commands? – Developer Sheldon Jun 16 '19 at 16:52
  • @DeveloperSheldon Seems like the link has been expired. I've edited my answer, so now it contains working link to all output formats of GDB. Please check it. – Vlad Jun 29 '19 at 18:30
12

type format add expects the type name as a single word -- you need to quote the argument if it is multiple words. e.g.

   2    {
   3      unsigned long long a = 10;
-> 4      a += 5;
   5      return a;
   6    }
(lldb) type form add -f h "unsigned long long"
(lldb) p a
(unsigned long long) $0 = 0x000000000000000a
(lldb) 
Jason Molenda
  • 13,365
  • 1
  • 51
  • 54
1

After reading the rest of the document, I found out it is possible to do something like this:

// ObjC code
typedef int A;

then,

(lldb) type format add --format hex A

This gave me the idea to typedef unsigned long long BigInt :

// ObjC code
typedef unsigned long long BigInt;

then,

(lldb) type format add --format hex BigInt

Works like a charm.

Mazyod
  • 21,361
  • 9
  • 86
  • 147