unwinder.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2015-2019 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. """Unwinder class and register_unwinder function."""
  15. import gdb
  16. class Unwinder(object):
  17. """Base class (or a template) for frame unwinders written in Python.
  18. An unwinder has a single method __call__ and the attributes
  19. described below.
  20. Attributes:
  21. name: The name of the unwinder.
  22. enabled: A boolean indicating whether the unwinder is enabled.
  23. """
  24. def __init__(self, name):
  25. """Constructor.
  26. Args:
  27. name: An identifying name for the unwinder.
  28. """
  29. self.name = name
  30. self.enabled = True
  31. def __call__(self, pending_frame):
  32. """GDB calls this method to unwind a frame.
  33. Arguments:
  34. pending_frame: gdb.PendingFrame instance.
  35. Returns:
  36. gdb.UnwindInfo instance.
  37. """
  38. raise NotImplementedError("Unwinder __call__.")
  39. def register_unwinder(locus, unwinder, replace=False):
  40. """Register unwinder in given locus.
  41. The unwinder is prepended to the locus's unwinders list. Unwinder
  42. name should be unique.
  43. Arguments:
  44. locus: Either an objfile, progspace, or None (in which case
  45. the unwinder is registered globally).
  46. unwinder: An object of a gdb.Unwinder subclass
  47. replace: If True, replaces existing unwinder with the same name.
  48. Otherwise, raises exception if unwinder with the same
  49. name already exists.
  50. Returns:
  51. Nothing.
  52. Raises:
  53. RuntimeError: Unwinder name is not unique
  54. TypeError: Bad locus type
  55. """
  56. if locus is None:
  57. if gdb.parameter("verbose"):
  58. gdb.write("Registering global %s unwinder ...\n" % unwinder.name)
  59. locus = gdb
  60. elif isinstance(locus, gdb.Objfile) or isinstance(locus, gdb.Progspace):
  61. if gdb.parameter("verbose"):
  62. gdb.write("Registering %s unwinder for %s ...\n" %
  63. (unwinder.name, locus.filename))
  64. else:
  65. raise TypeError("locus should be gdb.Objfile or gdb.Progspace or None")
  66. i = 0
  67. for needle in locus.frame_unwinders:
  68. if needle.name == unwinder.name:
  69. if replace:
  70. del locus.frame_unwinders[i]
  71. else:
  72. raise RuntimeError("Unwinder %s already exists." %
  73. unwinder.name)
  74. i += 1
  75. locus.frame_unwinders.insert(0, unwinder)
  76. gdb.invalidate_cached_frames()