# Tips for debugging SMTK

**URL:** https://discourse.kitware.com/t/tips-for-debugging-smtk/304
**Category:** Tips and Tricks
**Created:** [August 29, 2019, 3:34pm UTC](https://discourse.kitware.com/t/tips-for-debugging-smtk/304 "2019-08-29T15:34:33Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)
#### Post date: [August 29, 2019, 3:34pm UTC](https://discourse.kitware.com/t/tips-for-debugging-smtk/304/1 "2019-08-29T15:34:33Z")

</div>

NB: This topic is a “wiki page,” which means anyone (not just the author) can edit it. Please add your own tips below.

# LLDB

Below are some niceties for using lldb with SMTK. There may be equivalents for gdb; if so, please add them to a new top-level section below this one.

## Model entity type bits

If you deal with `smtk::model::Entity` objects a lot, you’ll notice that the `m_entityFlags` member is always a combination of values from [`smtk/model/EntityTypeBits.h`](https://gitlab.kitware.com/cmb/smtk/tree/master/smtk/model/EntityTypeBits.h#L101) . The enum values in the header file are hexadecimal and easier to read in a base that is some power of 2. You can have lldb print them in hexadecimal like so:

```auto
    ty fo add --format hex smtk::model::BitFlags

```

Adding the line above to your `~/.lldbinit` file will make it the default behavior.

## Inspecting UUIDs

If you use lldb, you can have it print UUIDs out compactly instead of the verbose and unhelpful unsigned char array. To do so,

1. Create an `.lldb` directory in your home directory

```auto
    mkdir ~/.lldb

```

1. Create a text file in `~/.lldb` named `uuid.py` with the following contents:

```python
import lldb

def uuid_summary(value, *rest):
    """valobj: an SBValue which you want to provide a summary for
       internal_dict: an LLDB support object not to be used"""
    rawd = value.GetChildMemberWithName('data')
    s = ''.join('%02x'%i for i in rawd.data.uint8)
    return 'UUID(' + s[0:8] +'-' + s[8:12] + '-' + s[12:16] + '-' + s[16:20] + '-' + s[20:32] + ')'

def __lldb_init_module(debugger, *rest):
    summary = lldb.SBTypeSummary.CreateWithFunctionName('uuid.uuid_summary')
    summary.SetOptions(lldb.eTypeOptionHideChildren)
    debugger.GetDefaultCategory().AddTypeSummary(lldb.SBTypeNameSpecifier('boost::uuids::uuid', False), summary)

```

1. Either add the line below to your `~/.lldbinit` file or create a new file named `~/.lldbinit` with the line below

```auto
command script import ~/.lldb/uuid.py

```

## Qt

If you are working with SMTK’s Qt extensions, you may be interested in [these Qt5 formatters](https://github.com/sdonegan/Qt5DataFormatters) that let you inspect QString and some of Qt’s container classes.

## Shared Pointers

SMTK provides some lldb python functions in `utilities/lldb/shared_ptr.py` that allow you to stop execution when a particular object’s shared-pointer use-count changes. You can import these functions into your lldb function by running

```auto
  command script import /path/to/smtk/utilities/lldb/shared_ptr.py

```

Then, at a point where you have a pointer to some class that inherits `enable_shared_from_this` (like all smtk::resource::PersistentObject and smtk::operation::Operation classes), you can run the following

```auto
  useCountWatch <scoped class name> <address of class instance>
  # e.g.,
  # useCountWatch smtk::resource::Resource 0x142f240f0

```

to break when that object’s use count changes. Once you have a watchpoint on the use-count, there’s also a function to dump a stack trace to a file whenever the use count changes:

```auto

  useCountTrace <instance address> <filename> [<size of use-count in bytes>]
  # e.g.,
  # useCountTrace 0x142f240f0 /tmp/trace.txt 4

```

If calling the `useCountWatch` example above created watchpoint 1, then the following will add a script to the watchpoint to dump a stack trace and continue:

```auto
  wa co add 1
  > useCountTrace 0x142f240f0 /tmp/trace.txt
  > c
  > DONE 

```
