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