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