BacktraceThread.h revision f67c6410d12a1829b0c6d009c848d72cd0158ee4
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 <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
34struct ThreadEntry {
35  ThreadEntry(
36      BacktraceThreadInterface* impl, pid_t pid, pid_t tid,
37      size_t num_ignore_frames);
38  ~ThreadEntry();
39
40  bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid && chk_tid == tid); }
41
42  static ThreadEntry* AddThreadToUnwind(
43      BacktraceThreadInterface* thread_intf, pid_t pid, pid_t tid,
44      size_t num_ignored_frames);
45
46  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;
53};
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(
73      BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid,
74      backtrace_map_info_t* map_info);
75  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
93