How to add a hexadecimal watch in CLion?
Asked Answered
T

3

9

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.

Tangier answered 12/12, 2016 at 19:22 Comment(0)
T
8
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).

Terpsichorean answered 14/1, 2017 at 22:29 Comment(0)
B
1

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.

Balder answered 16/11, 2020 at 7:23 Comment(0)
T
0

...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
Tangier answered 12/12, 2016 at 21:0 Comment(2)
Here's the corresponding feature request in CLion/AppCode tracker: youtrack.jetbrains.com/issue/OC-2305 Please, feel free to vote.Dunghill
Thanks. I also found a workaround there. With these two, one per variable and one global, we can live.Tangier

© 2022 - 2024 — McMap. All rights reserved.