outcome_gdb.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Inline GDB pretty printer for result
  2. # (C) 2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  3. # File Created: Jun 2024
  4. #
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License in the accompanying file
  9. # Licence.txt or at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. #
  20. # Distributed under the Boost Software License, Version 1.0.
  21. # (See accompanying file Licence.txt or copy at
  22. # http://www.boost.org/LICENSE_1_0.txt)
  23. import gdb.printing
  24. import os
  25. def synthesise_gdb_value_from_string(s):
  26. '''For when you want to return a synthetic string from children()'''
  27. return gdb.Value(s + '\0').cast(gdb.lookup_type('char').pointer())
  28. class OutcomeBasicOutcomePrinter(object):
  29. '''Print an outcome::basic_outcome<T> and outcome::basic_result<T>'''
  30. def __init__(self, val):
  31. self.val = val
  32. def children(self):
  33. if self.val['_state']['_status']['status_value'] & 1 == 1:
  34. yield ('value', self.val['_state']['_value'])
  35. if self.val['_state']['_status']['status_value'] & 2 == 2:
  36. yield ('error', self.val['_state']['_error'])
  37. if self.val['_state']['_status']['status_value'] & 4 == 4:
  38. yield ('exception', self.val['_ptr'])
  39. def display_hint(self):
  40. return None
  41. def to_string(self):
  42. if self.val['_state']['_status']['status_value'] & 54 == 54:
  43. return 'errored (errno, moved from) + exceptioned'
  44. if self.val['_state']['_status']['status_value'] & 50 == 50:
  45. return 'errored (errno, moved from)'
  46. if self.val['_state']['_status']['status_value'] & 38 == 38:
  47. return 'errored + exceptioned (moved from)'
  48. if self.val['_state']['_status']['status_value'] & 36 == 36:
  49. return 'exceptioned (moved from)'
  50. if self.val['_state']['_status']['status_value'] & 35 == 35:
  51. return 'errored (moved from)'
  52. if self.val['_state']['_status']['status_value'] & 33 == 33:
  53. return 'valued (moved from)'
  54. if self.val['_state']['_status']['status_value'] & 22 == 22:
  55. return 'errored (errno) + exceptioned'
  56. if self.val['_state']['_status']['status_value'] & 18 == 18:
  57. return 'errored (errno)'
  58. if self.val['_state']['_status']['status_value'] & 6 == 6:
  59. return 'errored + exceptioned'
  60. if self.val['_state']['_status']['status_value'] & 4 == 4:
  61. return 'exceptioned'
  62. if self.val['_state']['_status']['status_value'] & 2 == 2:
  63. return 'errored'
  64. if self.val['_state']['_status']['status_value'] & 1 == 1:
  65. return 'valued'
  66. if self.val['_state']['_status']['status_value'] & 0xff == 0:
  67. return 'empty'
  68. class OutcomeCResultStatusPrinter(object):
  69. '''Print a C result'''
  70. def __init__(self, val):
  71. self.val = val
  72. def children(self):
  73. if self.val['flags'] & 1 == 1:
  74. yield ('value', self.val['value'])
  75. if self.val['flags'] & 2 == 2:
  76. yield ('error', self.val['error'])
  77. def display_hint(self):
  78. return None
  79. def to_string(self):
  80. if self.val['flags'] & 50 == 50:
  81. return 'errored (errno, moved from)'
  82. if self.val['flags'] & 35 == 35:
  83. return 'errored (moved from)'
  84. if self.val['flags'] & 33 == 33:
  85. return 'valued (moved from)'
  86. if self.val['flags'] & 18 == 18:
  87. return 'errored (errno)'
  88. if self.val['flags'] & 2 == 2:
  89. return 'errored'
  90. if self.val['flags'] & 1 == 1:
  91. return 'valued'
  92. if self.val['flags'] & 0xff == 0:
  93. return 'empty'
  94. class OutcomeCStatusCodePrinter(object):
  95. '''Print a C status code'''
  96. def __init__(self, val):
  97. self.val = val
  98. def children(self):
  99. yield ('domain', self.val['domain'])
  100. yield ('value', self.val['value'])
  101. def display_hint(self):
  102. return None
  103. def to_string(self):
  104. s = str(self.val['domain'])
  105. if 'posix_code_domain' in s or 'generic_code_domain' in s:
  106. return str(self.val['value']) + ' (' + os.strerror(int(self.val['value'])) + ')'
  107. else:
  108. return self.val['value']
  109. def build_pretty_printer():
  110. pp = gdb.printing.RegexpCollectionPrettyPrinter('outcome_v2')
  111. pp.add_printer('outcome_v2::basic_result', '^(boost::)?outcome_v2[_0-9a-f]*::basic_result<.*>$', OutcomeBasicOutcomePrinter)
  112. pp.add_printer('outcome_v2::basic_outcome', '^(boost::)?outcome_v2[_0-9a-f]*::basic_outcome<.*>$', OutcomeBasicOutcomePrinter)
  113. pp.add_printer('cxx_result_status_code_*', '^cxx_result_status_code_.*$', OutcomeCResultStatusPrinter)
  114. pp.add_printer('cxx_status_code_*', '^cxx_status_code_.*$', OutcomeCStatusCodePrinter)
  115. return pp
  116. def register_printers(obj = None):
  117. gdb.printing.register_pretty_printer(obj, build_pretty_printer(), replace = True)
  118. register_printers(gdb.current_objfile())