sanitizer_stoptheworld.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===-- sanitizer_stoptheworld.h --------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Defines the StopTheWorld function which suspends the execution of the current
  9. // process and runs the user-supplied callback in the same address space.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_STOPTHEWORLD_H
  13. #define SANITIZER_STOPTHEWORLD_H
  14. #include "sanitizer_internal_defs.h"
  15. #include "sanitizer_common.h"
  16. namespace __sanitizer {
  17. typedef int SuspendedThreadID;
  18. // Holds the list of suspended threads and provides an interface to dump their
  19. // register contexts.
  20. class SuspendedThreadsList {
  21. public:
  22. SuspendedThreadsList()
  23. : thread_ids_(1024) {}
  24. SuspendedThreadID GetThreadID(uptr index) const {
  25. CHECK_LT(index, thread_ids_.size());
  26. return thread_ids_[index];
  27. }
  28. int GetRegistersAndSP(uptr index, uptr *buffer, uptr *sp) const;
  29. // The buffer in GetRegistersAndSP should be at least this big.
  30. static uptr RegisterCount();
  31. uptr thread_count() const { return thread_ids_.size(); }
  32. bool Contains(SuspendedThreadID thread_id) const {
  33. for (uptr i = 0; i < thread_ids_.size(); i++) {
  34. if (thread_ids_[i] == thread_id)
  35. return true;
  36. }
  37. return false;
  38. }
  39. void Append(SuspendedThreadID thread_id) {
  40. thread_ids_.push_back(thread_id);
  41. }
  42. private:
  43. InternalMmapVector<SuspendedThreadID> thread_ids_;
  44. // Prohibit copy and assign.
  45. SuspendedThreadsList(const SuspendedThreadsList&);
  46. void operator=(const SuspendedThreadsList&);
  47. };
  48. typedef void (*StopTheWorldCallback)(
  49. const SuspendedThreadsList &suspended_threads_list,
  50. void *argument);
  51. // Suspend all threads in the current process and run the callback on the list
  52. // of suspended threads. This function will resume the threads before returning.
  53. // The callback should not call any libc functions.
  54. // This function should NOT be called from multiple threads simultaneously.
  55. void StopTheWorld(StopTheWorldCallback callback, void *argument);
  56. } // namespace __sanitizer
  57. #endif // SANITIZER_STOPTHEWORLD_H