blob: ff3e9f3f4d2f67d4df9f4e47832865a4475a3121 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
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 Ferrisa2efd3a2014-05-06 15:23:59 -070021#include <pthread.h>
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -070022#include <signal.h>
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070023#include <string.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <sys/types.h>
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070025#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070026
Christopher Ferrisdf290612014-01-22 19:21:07 -080027#include "BacktraceImpl.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070028
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -070029// 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 Ferrisa2efd3a2014-05-06 15:23:59 -070037class ThreadEntry {
38public:
39 static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true);
Christopher Ferris17e91d42013-10-21 13:30:52 -070040
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070041 static void Remove(ThreadEntry* entry);
42
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070043 void Wake();
44
45 void Wait(int);
46
Christopher Ferrise4846072014-05-23 14:46:36 -070047 void CopyUcontextFromSigcontext(void*);
48
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070049 inline void Lock() {
50 pthread_mutex_lock(&mutex_);
51 // Reset the futex value in case of multiple unwinds of the same thread.
52 futex_ = 0;
53 }
54
55 inline void Unlock() {
56 pthread_mutex_unlock(&mutex_);
57 }
58
59 inline ucontext_t* GetUcontext() { return &ucontext_; }
60
61private:
62 ThreadEntry(pid_t pid, pid_t tid);
Christopher Ferris17e91d42013-10-21 13:30:52 -070063 ~ThreadEntry();
64
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070065 bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); }
Christopher Ferris17e91d42013-10-21 13:30:52 -070066
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070067 pid_t pid_;
68 pid_t tid_;
69 int futex_;
70 int ref_count_;
71 pthread_mutex_t mutex_;
72 ThreadEntry* next_;
73 ThreadEntry* prev_;
74 ucontext_t ucontext_;
Christopher Ferris17e91d42013-10-21 13:30:52 -070075
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070076 static ThreadEntry* list_;
77 static pthread_mutex_t list_mutex_;
Christopher Ferris17e91d42013-10-21 13:30:52 -070078};
79
80class BacktraceThread : public BacktraceCurrent {
81public:
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070082 BacktraceThread(BacktraceImpl* impl, pid_t tid, BacktraceMap* map);
Christopher Ferris17e91d42013-10-21 13:30:52 -070083 virtual ~BacktraceThread();
84
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070085 virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext);
Christopher Ferris17e91d42013-10-21 13:30:52 -070086};
87
88#endif // _LIBBACKTRACE_BACKTRACE_THREAD_H