BacktraceThread.h revision a2efd3ac7abe223aa7a8ba8b5ba448216c4953b4
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 <signal.h> 23#include <string.h> 24#include <sys/types.h> 25#include <ucontext.h> 26 27#include "BacktraceImpl.h" 28 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 37class ThreadEntry { 38public: 39 static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true); 40 41 static void Remove(ThreadEntry* entry); 42 43 inline void CopyUcontext(ucontext_t* ucontext) { 44 memcpy(&ucontext_, ucontext, sizeof(ucontext_)); 45 } 46 47 void Wake(); 48 49 void Wait(int); 50 51 inline void Lock() { 52 pthread_mutex_lock(&mutex_); 53 // Reset the futex value in case of multiple unwinds of the same thread. 54 futex_ = 0; 55 } 56 57 inline void Unlock() { 58 pthread_mutex_unlock(&mutex_); 59 } 60 61 inline ucontext_t* GetUcontext() { return &ucontext_; } 62 63private: 64 ThreadEntry(pid_t pid, pid_t tid); 65 ~ThreadEntry(); 66 67 bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); } 68 69 pid_t pid_; 70 pid_t tid_; 71 int futex_; 72 int ref_count_; 73 pthread_mutex_t mutex_; 74 ThreadEntry* next_; 75 ThreadEntry* prev_; 76 ucontext_t ucontext_; 77 78 static ThreadEntry* list_; 79 static pthread_mutex_t list_mutex_; 80}; 81 82class BacktraceThread : public BacktraceCurrent { 83public: 84 BacktraceThread(BacktraceImpl* impl, pid_t tid, BacktraceMap* map); 85 virtual ~BacktraceThread(); 86 87 virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext); 88}; 89 90#endif // _LIBBACKTRACE_BACKTRACE_THREAD_H 91