| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 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_PTRACER_THREAD_H_ | 
|  | 18 | #define LIBMEMUNREACHABLE_PTRACER_THREAD_H_ | 
|  | 19 |  | 
|  | 20 | #include <functional> | 
|  | 21 | #include <mutex> | 
|  | 22 |  | 
|  | 23 | #include "android-base/macros.h" | 
|  | 24 |  | 
|  | 25 | #include "Allocator.h" | 
|  | 26 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 27 | namespace android { | 
|  | 28 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 29 | class Stack; | 
|  | 30 |  | 
|  | 31 | // PtracerThread is similar to std::thread, except that it creates a "thread" | 
|  | 32 | // that can ptrace the other threads.  The thread is actually a separate | 
|  | 33 | // process, with its own thread group, but shares address space and fds with | 
|  | 34 | // the parent. | 
|  | 35 | class PtracerThread { | 
|  | 36 | public: | 
| Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 37 | explicit PtracerThread(const std::function<int()>& func); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 38 | ~PtracerThread(); | 
|  | 39 | bool Start(); | 
|  | 40 | int Join(); | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 41 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 42 | private: | 
|  | 43 | void SetTracer(pid_t); | 
|  | 44 | void ClearTracer(); | 
|  | 45 | void Kill(); | 
|  | 46 | DISALLOW_COPY_AND_ASSIGN(PtracerThread); | 
|  | 47 | std::unique_ptr<Stack> stack_; | 
|  | 48 | std::function<int()> func_; | 
|  | 49 | std::mutex m_; | 
|  | 50 | pid_t child_pid_; | 
|  | 51 | }; | 
|  | 52 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 53 | }  // namespace android | 
|  | 54 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 55 | #endif  // LIBMEMUNREACHABLE_PTRACER_THREAD_H_ |