Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 _LIBBACKTRACE_BACKTRACE_THREAD_H |
| 18 | #define _LIBBACKTRACE_BACKTRACE_THREAD_H |
| 19 | |
| 20 | #include <inttypes.h> |
| 21 | #include <pthread.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include "Backtrace.h" |
| 25 | |
| 26 | typedef enum { |
| 27 | STATE_WAITING = 0, |
| 28 | STATE_DUMPING, |
| 29 | STATE_DONE, |
| 30 | STATE_CANCEL, |
| 31 | } state_e; |
| 32 | |
| 33 | class BacktraceThreadInterface; |
| 34 | |
| 35 | class ThreadEntry { |
| 36 | public: |
| 37 | ThreadEntry( |
| 38 | BacktraceThreadInterface* impl, pid_t pid, pid_t tid, |
| 39 | size_t num_ignore_frames); |
| 40 | ~ThreadEntry(); |
| 41 | |
| 42 | bool Match(pid_t pid, pid_t tid) { return (pid == pid_ && tid == tid_); } |
| 43 | |
| 44 | static ThreadEntry* AddThreadToUnwind( |
| 45 | BacktraceThreadInterface* thread_intf, pid_t pid, pid_t tid, |
| 46 | size_t num_ignored_frames); |
| 47 | |
| 48 | BacktraceThreadInterface* thread_intf_; |
| 49 | pid_t pid_; |
| 50 | pid_t tid_; |
| 51 | ThreadEntry* next_; |
| 52 | ThreadEntry* prev_; |
| 53 | int32_t state_; |
| 54 | int num_ignore_frames_; |
| 55 | }; |
| 56 | |
| 57 | // Interface class that does not contain any local storage, only defines |
| 58 | // virtual functions to be defined by subclasses. |
| 59 | class BacktraceThreadInterface { |
| 60 | public: |
| 61 | virtual ~BacktraceThreadInterface() { } |
| 62 | |
| 63 | virtual bool Init() = 0; |
| 64 | |
| 65 | virtual void ThreadUnwind( |
| 66 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) = 0; |
| 67 | }; |
| 68 | |
| 69 | class BacktraceThread : public BacktraceCurrent { |
| 70 | public: |
| 71 | // impl and thread_intf should point to the same object, this allows |
| 72 | // the compiler to catch if an implementation does not properly |
| 73 | // subclass both. |
| 74 | BacktraceThread( |
| 75 | BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid); |
| 76 | virtual ~BacktraceThread(); |
| 77 | |
| 78 | virtual bool Unwind(size_t num_ignore_frames); |
| 79 | |
| 80 | virtual void ThreadUnwind( |
| 81 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) { |
| 82 | thread_intf_->ThreadUnwind(siginfo, sigcontext, num_ignore_frames); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | virtual bool TriggerUnwindOnThread(ThreadEntry* entry); |
| 87 | |
| 88 | virtual void FinishUnwind(); |
| 89 | |
| 90 | BacktraceThreadInterface* thread_intf_; |
| 91 | }; |
| 92 | |
| 93 | #endif // _LIBBACKTRACE_BACKTRACE_THREAD_H |