| 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 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 23 | #include "BacktraceImpl.h" | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 61 | virtual void ThreadUnwind( | 
|  | 62 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) = 0; | 
|  | 63 | }; | 
|  | 64 |  | 
|  | 65 | class BacktraceThread : public BacktraceCurrent { | 
|  | 66 | public: | 
|  | 67 | // impl and thread_intf should point to the same object, this allows | 
|  | 68 | // the compiler to catch if an implementation does not properly | 
|  | 69 | // subclass both. | 
|  | 70 | BacktraceThread( | 
| Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 71 | BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid, | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 72 | BacktraceMap* map); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 73 | virtual ~BacktraceThread(); | 
|  | 74 |  | 
|  | 75 | virtual bool Unwind(size_t num_ignore_frames); | 
|  | 76 |  | 
|  | 77 | virtual void ThreadUnwind( | 
|  | 78 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) { | 
|  | 79 | thread_intf_->ThreadUnwind(siginfo, sigcontext, num_ignore_frames); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | private: | 
|  | 83 | virtual bool TriggerUnwindOnThread(ThreadEntry* entry); | 
|  | 84 |  | 
|  | 85 | virtual void FinishUnwind(); | 
|  | 86 |  | 
|  | 87 | BacktraceThreadInterface* thread_intf_; | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | #endif // _LIBBACKTRACE_BACKTRACE_THREAD_H |