| 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 | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 21 | #include <pthread.h> | 
| Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 22 | #include <signal.h> | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 23 | #include <string.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | #include <sys/types.h> | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 25 | #include <ucontext.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 26 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 27 | #include "BacktraceImpl.h" | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 28 |  | 
| Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 29 | // The signal used to cause a thread to dump the stack. | 
|  | 30 | #if defined(__GLIBC__) | 
|  | 31 | // GLIBC reserves __SIGRTMIN signals, so use SIGRTMIN to avoid errors. | 
|  | 32 | #define THREAD_SIGNAL SIGRTMIN | 
|  | 33 | #else | 
|  | 34 | #define THREAD_SIGNAL (__SIGRTMIN+1) | 
|  | 35 | #endif | 
|  | 36 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 37 | class ThreadEntry { | 
|  | 38 | public: | 
|  | 39 | static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 40 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 41 | static void Remove(ThreadEntry* entry); | 
|  | 42 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 43 | void Wake(); | 
|  | 44 |  | 
|  | 45 | void Wait(int); | 
|  | 46 |  | 
| Christopher Ferris | e484607 | 2014-05-23 14:46:36 -0700 | [diff] [blame] | 47 | void CopyUcontextFromSigcontext(void*); | 
|  | 48 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 49 | inline void Lock() { | 
|  | 50 | pthread_mutex_lock(&mutex_); | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | // Always reset the wait value since this could be the first or nth | 
|  | 53 | // time this entry is locked. | 
|  | 54 | wait_value_ = 0; | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
|  | 57 | inline void Unlock() { | 
|  | 58 | pthread_mutex_unlock(&mutex_); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | inline ucontext_t* GetUcontext() { return &ucontext_; } | 
|  | 62 |  | 
|  | 63 | private: | 
|  | 64 | ThreadEntry(pid_t pid, pid_t tid); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 65 | ~ThreadEntry(); | 
|  | 66 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 67 | 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] | 68 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 69 | pid_t pid_; | 
|  | 70 | pid_t tid_; | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 71 | int ref_count_; | 
|  | 72 | pthread_mutex_t mutex_; | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 73 | pthread_mutex_t wait_mutex_; | 
|  | 74 | pthread_cond_t wait_cond_; | 
|  | 75 | int wait_value_; | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 76 | ThreadEntry* next_; | 
|  | 77 | ThreadEntry* prev_; | 
|  | 78 | ucontext_t ucontext_; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 79 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 80 | static ThreadEntry* list_; | 
|  | 81 | static pthread_mutex_t list_mutex_; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 82 | }; | 
|  | 83 |  | 
|  | 84 | class BacktraceThread : public BacktraceCurrent { | 
|  | 85 | public: | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 86 | BacktraceThread(BacktraceImpl* impl, pid_t tid, BacktraceMap* map); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 87 | virtual ~BacktraceThread(); | 
|  | 88 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 89 | virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 90 | }; | 
|  | 91 |  | 
|  | 92 | #endif // _LIBBACKTRACE_BACKTRACE_THREAD_H |