debugobjects.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_DEBUGOBJECTS_H
  3. #define _LINUX_DEBUGOBJECTS_H
  4. #include <linux/list.h>
  5. #include <linux/spinlock.h>
  6. enum debug_obj_state {
  7. ODEBUG_STATE_NONE,
  8. ODEBUG_STATE_INIT,
  9. ODEBUG_STATE_INACTIVE,
  10. ODEBUG_STATE_ACTIVE,
  11. ODEBUG_STATE_DESTROYED,
  12. ODEBUG_STATE_NOTAVAILABLE,
  13. ODEBUG_STATE_MAX,
  14. };
  15. struct debug_obj_descr;
  16. /**
  17. * struct debug_obj - representaion of an tracked object
  18. * @node: hlist node to link the object into the tracker list
  19. * @state: tracked object state
  20. * @astate: current active state
  21. * @object: pointer to the real object
  22. * @descr: pointer to an object type specific debug description structure
  23. */
  24. struct debug_obj {
  25. struct hlist_node node;
  26. enum debug_obj_state state;
  27. unsigned int astate;
  28. void *object;
  29. const struct debug_obj_descr *descr;
  30. };
  31. /**
  32. * struct debug_obj_descr - object type specific debug description structure
  33. *
  34. * @name: name of the object typee
  35. * @debug_hint: function returning address, which have associated
  36. * kernel symbol, to allow identify the object
  37. * @is_static_object: return true if the obj is static, otherwise return false
  38. * @fixup_init: fixup function, which is called when the init check
  39. * fails. All fixup functions must return true if fixup
  40. * was successful, otherwise return false
  41. * @fixup_activate: fixup function, which is called when the activate check
  42. * fails
  43. * @fixup_destroy: fixup function, which is called when the destroy check
  44. * fails
  45. * @fixup_free: fixup function, which is called when the free check
  46. * fails
  47. * @fixup_assert_init: fixup function, which is called when the assert_init
  48. * check fails
  49. */
  50. struct debug_obj_descr {
  51. const char *name;
  52. void *(*debug_hint)(void *addr);
  53. bool (*is_static_object)(void *addr);
  54. bool (*fixup_init)(void *addr, enum debug_obj_state state);
  55. bool (*fixup_activate)(void *addr, enum debug_obj_state state);
  56. bool (*fixup_destroy)(void *addr, enum debug_obj_state state);
  57. bool (*fixup_free)(void *addr, enum debug_obj_state state);
  58. bool (*fixup_assert_init)(void *addr, enum debug_obj_state state);
  59. };
  60. #ifdef CONFIG_DEBUG_OBJECTS
  61. extern void debug_object_init (void *addr, const struct debug_obj_descr *descr);
  62. extern void
  63. debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr);
  64. extern int debug_object_activate (void *addr, const struct debug_obj_descr *descr);
  65. extern void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr);
  66. extern void debug_object_destroy (void *addr, const struct debug_obj_descr *descr);
  67. extern void debug_object_free (void *addr, const struct debug_obj_descr *descr);
  68. extern void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr);
  69. /*
  70. * Active state:
  71. * - Set at 0 upon initialization.
  72. * - Must return to 0 before deactivation.
  73. */
  74. extern void
  75. debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
  76. unsigned int expect, unsigned int next);
  77. extern void debug_objects_early_init(void);
  78. extern void debug_objects_mem_init(void);
  79. #else
  80. static inline void
  81. debug_object_init (void *addr, const struct debug_obj_descr *descr) { }
  82. static inline void
  83. debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr) { }
  84. static inline int
  85. debug_object_activate (void *addr, const struct debug_obj_descr *descr) { return 0; }
  86. static inline void
  87. debug_object_deactivate(void *addr, const struct debug_obj_descr *descr) { }
  88. static inline void
  89. debug_object_destroy (void *addr, const struct debug_obj_descr *descr) { }
  90. static inline void
  91. debug_object_free (void *addr, const struct debug_obj_descr *descr) { }
  92. static inline void
  93. debug_object_assert_init(void *addr, const struct debug_obj_descr *descr) { }
  94. static inline void debug_objects_early_init(void) { }
  95. static inline void debug_objects_mem_init(void) { }
  96. #endif
  97. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  98. extern void debug_check_no_obj_freed(const void *address, unsigned long size);
  99. #else
  100. static inline void
  101. debug_check_no_obj_freed(const void *address, unsigned long size) { }
  102. #endif
  103. #endif