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