customdebugtype.nim 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ## This is a demo file containing an example of how to
  2. ## create custom LLDB summaries and objects with synthetic
  3. ## children. These are implemented in Nim and called from the Python
  4. ## nimlldb.py module.
  5. ##
  6. ## For summaries, prefix your proc names with "lldbDebugSummary", use
  7. ## the `{.exportc.}` pragma, and return a string. Also, any `$` proc
  8. ## that is available will be used for a given type.
  9. ##
  10. ## For creating a synthetic object (LLDB will display the children), use
  11. ## the prefix "lldbDebugSynthetic", use the `{.exportc.}` pragma, and
  12. ## return any Nim object, array, or sequence. Returning a Nim object
  13. ## will display the fields and values of the object as children.
  14. ## Returning an array or sequence will display children with the index
  15. ## surrounded by square brackets as the key name
  16. ##
  17. ## You may also return a Nim table that contains the string
  18. ## "LLDBDynamicObject" (case insensitive). This allows for dynamic
  19. ## fields to be created at runtime instead of at compile time if you
  20. ## return a Nim object as mentioned above. See the proc
  21. ## `lldbDebugSyntheticDynamicFields` below for an example
  22. import intsets
  23. import tables
  24. type
  25. CustomType* = object of RootObj # RootObj is not necessary, but can be used
  26. myField*: int
  27. DynamicFields* = object
  28. customField*: string
  29. CustomSyntheticReturn* = object
  30. differentField*: float
  31. LLDBDynamicObject = object
  32. fields: TableRef[string, int]
  33. LLDBDynamicObjectDynamicFields = object
  34. fields: TableRef[string, string]
  35. proc lldbDebugSummaryCustomType*(ty: CustomType): string {.exportc.} =
  36. ## Will display "CustomType(myField: <int_val>)" as a summary
  37. result = "CustomType" & $ty
  38. proc lldbDebugSyntheticCustomType*(ty: CustomType): CustomSyntheticReturn {.exportc.} =
  39. ## Will display differentField: <float_val> as a child of CustomType instead of
  40. ## myField: <int_val>
  41. result = CustomSyntheticReturn(differentField: ty.myField.float)
  42. proc lldbDebugSyntheticDynamicFields*(ty: DynamicFields): LLDBDynamicObjectDynamicFields {.exportc.} =
  43. ## Returning an object that contains "LLDBDynamicObject" in the type name will expect an
  44. ## object with one property that is a Nim Table/TableRef. If the key is a string,
  45. ## it will appear in the debugger like an object field name. The value will be whatever you
  46. ## set it to here as well.
  47. let fields = {"customFieldName": ty.customField & " MORE TEXT"}.newTable()
  48. return LLDBDynamicObjectDynamicFields(fields: fields)
  49. proc lldbDebugSummaryIntSet*(intset: IntSet): string {.exportc.} =
  50. ## This will print the object in the LLDB summary just as Nim prints it
  51. result = $intset
  52. proc lldbDebugSyntheticIntSet*(intset: IntSet): seq[int] {.exportc.} =
  53. ## This will create a synthetic object to make it so that IntSet
  54. ## will appear as a Nim object in the LLDB debugger window
  55. ##
  56. ## returning a seq here will display children like:
  57. ## [0]: <child_value>
  58. ##
  59. result = newSeqOfCap[int](intset.len)
  60. for val in intset:
  61. result.add(val)