How to add a hexadecimal watch in CLion?

5k Views Asked by At

I need to add a watch in hexadecimal format in CLion.

ltoa(variable, 16) doesn't work, at least, on my system.

In Java/Python, I can have a workaround: write a custom toString()/__str__ for my class and have it displayed the way I need. gdb has p/x. How do I do it in CLion?

Edit: ltoa(variable, 16) works if I define ltoa in my code, as it's not always present in standard library.

3

There are 3 best solutions below

0
On
set output-radix 16

You can set this as a default option in a file called .gdbinit, which you can put in your home directory, or the working directory from which you start GDB (project root, for instance).

2
On

...after refining the formulation, I see it.

I wrote my own char *lltoa(long long value, int radix) function. I can use it in watches now.

Update: in the respective feature request, Chris White found a workaround on OS X with lldb:

I decided to do a bit more digging and found a way to set lldb on OS X to force HEX output for unsigned char data types:

 ​type format add –format hex "unsigned char"

If you want to make this setting persistent you can also create a .lldbinit file and add this command to it. Once you do this CLion will display this data type in HEX format.

This makes ALL the variables of this type display in hex.

Update 2: My first workaround is pretty dirty, here's a better one.

You can assign formats to more specific types. The debugger keeps track of type inheritance. So, adding a hex format to uint8_t will not affect unsigned char. You can fine-tune the displays.

You can assign formats to structs also. Here's an example from my .lldbinit:

type format add --format dec int32_t

# https://lldb.llvm.org/varformats.html
type summary add --summary-string "addr=${var.address} depth=${var.depth}" Position
0
On

They added hex view as an experimental feature: Hexadecimal view

To enable:

  1. Invoke the Maintenance popup: press Ctrl+Alt+Shift+/, or call Help | Find Action and search for Maintenance. Choose Experimental features.
  2. Select the cidr.debugger.value.numberFormatting.hex checkbox
  3. Go to Settings / Preferences | Build, Execution, Deployment | Debugger | Data Views | C/C++ and set the checkbox Show hex values for numbers. Choose to have hexadecimal values displayed instead or alongside the original values.

Now the hexadecimal formatting is shown both in the Variables pane of the Debug tool window and in the editor's inline variables view.