blob: 3412d5885b0875f02006f378ce8e51e809d7628c [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
Christopher Ferrisdf290612014-01-22 19:21:07 -080023#include "BacktraceImpl.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070024
Christopher Ferris46756822014-01-14 20:16:30 -080025enum state_e {
Christopher Ferris17e91d42013-10-21 13:30:52 -070026 STATE_WAITING = 0,
27 STATE_DUMPING,
28 STATE_DONE,
29 STATE_CANCEL,
Christopher Ferris46756822014-01-14 20:16:30 -080030};
Christopher Ferris17e91d42013-10-21 13:30:52 -070031
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
Christopher Ferris17e91d42013-10-21 13:30:52 -070061 virtual void ThreadUnwind(
62 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) = 0;
63};
64
65class BacktraceThread : public BacktraceCurrent {
66public:
67 // impl and thread_intf should point to the same object, this allows
68 // the compiler to catch if an implementation does not properly
69 // subclass both.
70 BacktraceThread(
Christopher Ferris98464972014-01-06 19:16:33 -080071 BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid,
Christopher Ferris46756822014-01-14 20:16:30 -080072 BacktraceMap* map);
Christopher Ferris17e91d42013-10-21 13:30:52 -070073 virtual ~BacktraceThread();
74
75 virtual bool Unwind(size_t num_ignore_frames);
76
77 virtual void ThreadUnwind(
78 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
79 thread_intf_->ThreadUnwind(siginfo, sigcontext, num_ignore_frames);
80 }
81
82private:
83 virtual bool TriggerUnwindOnThread(ThreadEntry* entry);
84
85 virtual void FinishUnwind();
86
87 BacktraceThreadInterface* thread_intf_;
88};
89
90#endif // _LIBBACKTRACE_BACKTRACE_THREAD_H