1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBMEMUNREACHABLE_THREAD_CAPTURE_H_
18 #define LIBMEMUNREACHABLE_THREAD_CAPTURE_H_
19 
20 #include <utility>
21 
22 #include "Allocator.h"
23 
24 namespace android {
25 
26 struct ThreadInfo {
27   pid_t tid;
28   allocator::vector<uintptr_t> regs;
29   std::pair<uintptr_t, uintptr_t> stack;
30 };
31 
32 using TidList = allocator::vector<pid_t>;
33 using ThreadInfoList = allocator::vector<ThreadInfo>;
34 
35 class ThreadCaptureImpl;
36 
37 class ThreadCapture {
38  public:
39   ThreadCapture(pid_t pid, Allocator<ThreadCapture> allocator);
40   ~ThreadCapture();
41 
42   bool ListThreads(TidList& tids);
43   bool CaptureThreads();
44   bool ReleaseThreads();
45   bool ReleaseThread(pid_t tid);
46   bool CapturedThreadInfo(ThreadInfoList& threads);
47   void InjectTestFunc(std::function<void(pid_t)>&& f);
48 
49  private:
50   ThreadCapture(const ThreadCapture&) = delete;
51   void operator=(const ThreadCapture&) = delete;
52 
53   Allocator<ThreadCaptureImpl>::unique_ptr impl_;
54 };
55 
56 }  // namespace android
57 
58 #endif
59