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