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