Debug.h revision 341825
198684Sdes//===-- Debug.h -------------------------------------------------*- C++ -*-===//
299051Sdes//
357429Smarkm//                     The LLVM Compiler Infrastructure
498684Sdes//
598684Sdes// This file is distributed under the University of Illinois Open Source
676262Sgreen// License. See LICENSE.TXT for details.
798941Sdes//
898941Sdes//===----------------------------------------------------------------------===//
992559Sdes
1092559Sdes#ifndef liblldb_Debug_h_
1192559Sdes#define liblldb_Debug_h_
1292559Sdes
1392559Sdes// C Includes
1499051Sdes// C++ Includes
1599051Sdes#include <vector>
1699051Sdes
1799051Sdes// Other libraries and framework includes
1899051Sdes// Project includes
1992559Sdes#include "lldb/lldb-private.h"
2060576Skris
2157429Smarkmnamespace lldb_private {
2257429Smarkm
2369591Sgreen//------------------------------------------------------------------
2492559Sdes// Tells a thread what it needs to do when the process is resumed.
2592559Sdes//------------------------------------------------------------------
2692559Sdesstruct ResumeAction {
2792559Sdes  lldb::tid_t tid;       // The thread ID that this action applies to,
2892559Sdes                         // LLDB_INVALID_THREAD_ID for the default thread
2957429Smarkm                         // action
3092559Sdes  lldb::StateType state; // Valid values are eStateStopped/eStateSuspended,
3192559Sdes                         // eStateRunning, and eStateStepping.
3292559Sdes  int signal; // When resuming this thread, resume it with this signal if this
3392559Sdes              // value is > 0
3457429Smarkm};
3557429Smarkm
3692559Sdes//------------------------------------------------------------------
3792559Sdes// A class that contains instructions for all threads for
3857429Smarkm// NativeProcessProtocol::Resume(). Each thread can either run, stay suspended,
3992559Sdes// or step when the process is resumed. We optionally have the ability to also
4092559Sdes// send a signal to the thread when the action is run or step.
4199051Sdes//------------------------------------------------------------------
4299051Sdesclass ResumeActionList {
4392559Sdespublic:
4492559Sdes  ResumeActionList() : m_actions(), m_signal_handled() {}
4592559Sdes
4692559Sdes  ResumeActionList(lldb::StateType default_action, int signal)
4792559Sdes      : m_actions(), m_signal_handled() {
4892559Sdes    SetDefaultThreadActionIfNeeded(default_action, signal);
4992559Sdes  }
5092559Sdes
5192559Sdes  ResumeActionList(const ResumeAction *actions, size_t num_actions)
5292559Sdes      : m_actions(), m_signal_handled() {
5392559Sdes    if (actions && num_actions) {
5492559Sdes      m_actions.assign(actions, actions + num_actions);
5576262Sgreen      m_signal_handled.assign(num_actions, false);
5692559Sdes    }
5792559Sdes  }
5892559Sdes
5992559Sdes  ~ResumeActionList() = default;
6057429Smarkm
6157429Smarkm  bool IsEmpty() const { return m_actions.empty(); }
6292559Sdes
6392559Sdes  void Append(const ResumeAction &action) {
6476262Sgreen    m_actions.push_back(action);
6595456Sdes    m_signal_handled.push_back(false);
6695456Sdes  }
6757429Smarkm
6892559Sdes  void AppendAction(lldb::tid_t tid, lldb::StateType state, int signal = 0) {
6998684Sdes    ResumeAction action = {tid, state, signal};
7057429Smarkm    Append(action);
7192559Sdes  }
7257429Smarkm
7398684Sdes  void AppendResumeAll() {
7457429Smarkm    AppendAction(LLDB_INVALID_THREAD_ID, lldb::eStateRunning);
7592559Sdes  }
7692559Sdes
7792559Sdes  void AppendSuspendAll() {
7898941Sdes    AppendAction(LLDB_INVALID_THREAD_ID, lldb::eStateStopped);
7998941Sdes  }
8098941Sdes
8198941Sdes  void AppendStepAll() {
8299051Sdes    AppendAction(LLDB_INVALID_THREAD_ID, lldb::eStateStepping);
8392559Sdes  }
8492559Sdes
8592559Sdes  const ResumeAction *GetActionForThread(lldb::tid_t tid,
8692559Sdes                                         bool default_ok) const {
8792559Sdes    const size_t num_actions = m_actions.size();
8857429Smarkm    for (size_t i = 0; i < num_actions; ++i) {
8998941Sdes      if (m_actions[i].tid == tid)
9098684Sdes        return &m_actions[i];
9165674Skris    }
9292559Sdes    if (default_ok && tid != LLDB_INVALID_THREAD_ID)
9392559Sdes      return GetActionForThread(LLDB_INVALID_THREAD_ID, false);
9492559Sdes    return nullptr;
9592559Sdes  }
9676262Sgreen
9792559Sdes  size_t NumActionsWithState(lldb::StateType state) const {
9876262Sgreen    size_t count = 0;
99    const size_t num_actions = m_actions.size();
100    for (size_t i = 0; i < num_actions; ++i) {
101      if (m_actions[i].state == state)
102        ++count;
103    }
104    return count;
105  }
106
107  bool SetDefaultThreadActionIfNeeded(lldb::StateType action, int signal) {
108    if (GetActionForThread(LLDB_INVALID_THREAD_ID, true) == nullptr) {
109      // There isn't a default action so we do need to set it.
110      ResumeAction default_action = {LLDB_INVALID_THREAD_ID, action, signal};
111      m_actions.push_back(default_action);
112      m_signal_handled.push_back(false);
113      return true; // Return true as we did add the default action
114    }
115    return false;
116  }
117
118  void SetSignalHandledForThread(lldb::tid_t tid) const {
119    if (tid != LLDB_INVALID_THREAD_ID) {
120      const size_t num_actions = m_actions.size();
121      for (size_t i = 0; i < num_actions; ++i) {
122        if (m_actions[i].tid == tid)
123          m_signal_handled[i] = true;
124      }
125    }
126  }
127
128  const ResumeAction *GetFirst() const { return m_actions.data(); }
129
130  size_t GetSize() const { return m_actions.size(); }
131
132  void Clear() {
133    m_actions.clear();
134    m_signal_handled.clear();
135  }
136
137protected:
138  std::vector<ResumeAction> m_actions;
139  mutable std::vector<bool> m_signal_handled;
140};
141
142struct ThreadStopInfo {
143  lldb::StopReason reason;
144  union {
145    // eStopReasonSignal
146    struct {
147      uint32_t signo;
148    } signal;
149
150    // eStopReasonException
151    struct {
152      uint64_t type;
153      uint32_t data_count;
154      lldb::addr_t data[8];
155    } exception;
156  } details;
157};
158}
159
160#endif // liblldb_Debug_h_
161