LLDB  mainline
WatchpointOptions.cpp
Go to the documentation of this file.
1 //===-- WatchpointOptions.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
12 #include "lldb/Core/Value.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/ThreadSpec.h"
16 #include "lldb/Utility/Stream.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 bool WatchpointOptions::NullCallback(void *baton,
23  StoppointCallbackContext *context,
24  lldb::user_id_t watch_id) {
25  return true;
26 }
27 
28 // WatchpointOptions constructor
29 WatchpointOptions::WatchpointOptions()
30  : m_callback(WatchpointOptions::NullCallback), m_callback_baton_sp(),
31  m_callback_is_synchronous(false), m_thread_spec_up() {}
32 
33 // WatchpointOptions copy constructor
35  : m_callback(rhs.m_callback), m_callback_baton_sp(rhs.m_callback_baton_sp),
36  m_callback_is_synchronous(rhs.m_callback_is_synchronous),
37  m_thread_spec_up() {
38  if (rhs.m_thread_spec_up != nullptr)
39  m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
40 }
41 
42 // WatchpointOptions assignment operator
45  m_callback = rhs.m_callback;
48  if (rhs.m_thread_spec_up != nullptr)
49  m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
50  return *this;
51 }
52 
55  WatchpointHitCallback orig_callback = orig.m_callback;
56  lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
57  bool orig_is_sync = orig.m_callback_is_synchronous;
58 
59  orig.ClearCallback();
60  WatchpointOptions *ret_val = new WatchpointOptions(orig);
61 
62  orig.SetCallback(orig_callback, orig_callback_baton_sp, orig_is_sync);
63 
64  return ret_val;
65 }
66 
67 // Destructor
69 
70 // Callbacks
71 void WatchpointOptions::SetCallback(WatchpointHitCallback callback,
72  const BatonSP &callback_baton_sp,
73  bool callback_is_synchronous) {
74  m_callback_is_synchronous = callback_is_synchronous;
75  m_callback = callback;
76  m_callback_baton_sp = callback_baton_sp;
77 }
78 
82  m_callback_baton_sp.reset();
83 }
84 
86 
87 const Baton *WatchpointOptions::GetBaton() const {
88  return m_callback_baton_sp.get();
89 }
90 
92  lldb::user_id_t watch_id) {
93  if (m_callback && context->is_synchronous == IsCallbackSynchronous()) {
95  : nullptr,
96  context, watch_id);
97  } else
98  return true;
99 }
100 
103 }
104 
106  return m_thread_spec_up.get();
107 }
108 
110  if (m_thread_spec_up == nullptr)
111  m_thread_spec_up = std::make_unique<ThreadSpec>();
112 
113  return m_thread_spec_up.get();
114 }
115 
117  GetThreadSpec()->SetTID(thread_id);
118 }
119 
121  Stream *s, lldb::DescriptionLevel level) const {
122  if (m_callback_baton_sp.get()) {
123  s->EOL();
124  m_callback_baton_sp->GetDescription(s->AsRawOstream(), level,
125  s->GetIndentLevel());
126  }
127 }
128 
130  lldb::DescriptionLevel level) const {
131  // Figure out if there are any options not at their default value, and only
132  // print anything if there are:
133 
134  if ((GetThreadSpecNoCreate() != nullptr &&
135  GetThreadSpecNoCreate()->HasSpecification())) {
136  if (level == lldb::eDescriptionLevelVerbose) {
137  s->EOL();
138  s->IndentMore();
139  s->Indent();
140  s->PutCString("Watchpoint Options:\n");
141  s->IndentMore();
142  s->Indent();
143  } else
144  s->PutCString(" Options: ");
145 
146  if (m_thread_spec_up)
147  m_thread_spec_up->GetDescription(s, level);
148  else if (level == eDescriptionLevelBrief)
149  s->PutCString("thread spec: no ");
150  if (level == lldb::eDescriptionLevelFull) {
151  s->IndentLess();
152  s->IndentMore();
153  }
154  }
155 
156  GetCallbackDescription(s, level);
157 }
158 
160  llvm::raw_ostream &s, lldb::DescriptionLevel level,
161  unsigned indentation) const {
162  const CommandData *data = getItem();
163 
164  if (level == eDescriptionLevelBrief) {
165  s << ", commands = %s"
166  << ((data && data->user_source.GetSize() > 0) ? "yes" : "no");
167  return;
168  }
169 
170  indentation += 2;
171  s.indent(indentation);
172  s << "watchpoint commands:\n";
173 
174  indentation += 2;
175  if (data && data->user_source.GetSize() > 0) {
176  for (const std::string &line : data->user_source) {
177  s.indent(indentation);
178  s << line << "\n";
179  }
180  } else
181  s << "No commands.\n";
182 }
lldb_private::Stream::IndentLess
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:171
lldb_private::WatchpointOptions::GetThreadSpecNoCreate
const ThreadSpec * GetThreadSpecNoCreate() const
Return the current thread spec for this option.
Definition: WatchpointOptions.cpp:105
lldb_private::WatchpointOptions::SetThreadID
void SetThreadID(lldb::tid_t thread_id)
Definition: WatchpointOptions.cpp:116
lldb_private::StoppointCallbackContext
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Definition: StoppointCallbackContext.h:26
lldb_private::WatchpointOptions::GetCallbackDescription
void GetCallbackDescription(Stream *s, lldb::DescriptionLevel level) const
Get description for callback only.
Definition: WatchpointOptions.cpp:120
lldb_private::WatchpointOptions::WatchpointOptions
WatchpointOptions()
Default constructor.
Definition: WatchpointOptions.cpp:29
lldb_private::TypedBaton< CommandData >::data
void * data() override
Definition: Baton.h:69
lldb_private::WatchpointOptions::SetCallback
void SetCallback(WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the watchpoint option set.
Definition: WatchpointOptions.cpp:71
StoppointCallbackContext.h
lldb_private::WatchpointOptions::InvokeCallback
bool InvokeCallback(StoppointCallbackContext *context, lldb::user_id_t watch_id)
Use this function to invoke the callback for a specific stop.
Definition: WatchpointOptions.cpp:91
lldb_private::WatchpointOptions::GetBaton
Baton * GetBaton()
Fetch the baton from the callback.
Definition: WatchpointOptions.cpp:85
lldb_private::Stream
Definition: Stream.h:28
lldb_private::WatchpointOptions::m_thread_spec_up
std::unique_ptr< ThreadSpec > m_thread_spec_up
Definition: WatchpointOptions.h:196
Process.h
Target.h
lldb_private::StoppointCallbackContext::is_synchronous
bool is_synchronous
Definition: StoppointCallbackContext.h:44
WatchpointOptions.h
lldb_private::WatchpointOptions::GetThreadSpec
ThreadSpec * GetThreadSpec()
Returns a pointer to the ThreadSpec for this option, creating it.
Definition: WatchpointOptions.cpp:109
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
lldb_private::WatchpointOptions::CommandBaton::GetDescription
void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, unsigned indentation) const override
Definition: WatchpointOptions.cpp:159
ThreadSpec.h
lldb_private::WatchpointOptions::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
Definition: WatchpointOptions.cpp:129
lldb_private::WatchpointOptions::~WatchpointOptions
virtual ~WatchpointOptions()
lldb_private::WatchpointOptions
Definition: WatchpointOptions.h:25
lldb_private::WatchpointOptions::operator=
const WatchpointOptions & operator=(const WatchpointOptions &rhs)
Definition: WatchpointOptions.cpp:44
lldb::eDescriptionLevelFull
@ eDescriptionLevelFull
Definition: lldb-enumerations.h:209
lldb_private::TypedBaton< CommandData >::getItem
CommandData * getItem()
Definition: Baton.h:66
lldb_private::Stream::GetIndentLevel
unsigned GetIndentLevel() const
Get the current indentation level.
Definition: Stream.cpp:160
lldb_private::WatchpointOptions::IsCallbackSynchronous
bool IsCallbackSynchronous()
Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
Definition: WatchpointOptions.h:124
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
StringList.h
lldb_private::ThreadSpec::SetTID
void SetTID(lldb::tid_t tid)
Definition: ThreadSpec.h:47
lldb_private::WatchpointOptions::CommandData
Definition: WatchpointOptions.h:168
lldb_private::ThreadSpec
Definition: ThreadSpec.h:33
lldb_private::WatchpointOptions::m_callback_baton_sp
lldb::BatonSP m_callback_baton_sp
Definition: WatchpointOptions.h:193
lldb_private::WatchpointOptions::HasCallback
bool HasCallback()
Returns true if the watchpoint option has a callback set.
Definition: WatchpointOptions.cpp:101
lldb_private::Stream::IndentMore
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:168
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
lldb_private::WatchpointOptions::NullCallback
static bool NullCallback(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
This is the default empty callback.
Definition: WatchpointOptions.cpp:22
lldb_private::Baton
Definition: Baton.h:35
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
lldb_private::WatchpointOptions::ClearCallback
void ClearCallback()
Remove the callback from this option set.
Definition: WatchpointOptions.cpp:79
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eDescriptionLevelVerbose
@ eDescriptionLevelVerbose
Definition: lldb-enumerations.h:210
lldb_private::WatchpointOptions::m_callback
WatchpointHitCallback m_callback
Definition: WatchpointOptions.h:192
lldb_private::WatchpointOptions::CopyOptionsNoCallback
static WatchpointOptions * CopyOptionsNoCallback(WatchpointOptions &rhs)
Definition: WatchpointOptions.cpp:54
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::WatchpointOptions::m_callback_is_synchronous
bool m_callback_is_synchronous
Definition: WatchpointOptions.h:194
lldb
Definition: SBAddress.h:15
Value.h
lldb::eDescriptionLevelBrief
@ eDescriptionLevelBrief
Definition: lldb-enumerations.h:208
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86