LLDB  mainline
WatchpointOptions.cpp
Go to the documentation of this file.
1 //===-- WatchpointOptions.cpp -----------------------------------*- C++ -*-===//
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.reset(new ThreadSpec(*rhs.m_thread_spec_up));
40 }
41 
42 // WatchpointOptions assignment operator
45  m_callback = rhs.m_callback;
46  m_callback_baton_sp = rhs.m_callback_baton_sp;
47  m_callback_is_synchronous = rhs.m_callback_is_synchronous;
48  if (rhs.m_thread_spec_up != nullptr)
49  m_thread_spec_up.reset(new 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 
81  m_callback_is_synchronous = false;
82  m_callback_baton_sp.reset();
83 }
84 
85 Baton *WatchpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
86 
88  return m_callback_baton_sp.get();
89 }
90 
92  lldb::user_id_t watch_id) {
93  if (m_callback && context->is_synchronous == IsCallbackSynchronous()) {
94  return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
95  : nullptr,
96  context, watch_id);
97  } else
98  return true;
99 }
100 
102  return m_callback != WatchpointOptions::NullCallback;
103 }
104 
106  return m_thread_spec_up.get();
107 }
108 
110  if (m_thread_spec_up == nullptr)
111  m_thread_spec_up.reset(new 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, level);
125  }
126 }
127 
129  lldb::DescriptionLevel level) const {
130  // Figure out if there are any options not at their default value, and only
131  // print anything if there are:
132 
133  if ((GetThreadSpecNoCreate() != nullptr &&
134  GetThreadSpecNoCreate()->HasSpecification())) {
135  if (level == lldb::eDescriptionLevelVerbose) {
136  s->EOL();
137  s->IndentMore();
138  s->Indent();
139  s->PutCString("Watchpoint Options:\n");
140  s->IndentMore();
141  s->Indent();
142  } else
143  s->PutCString(" Options: ");
144 
145  if (m_thread_spec_up)
146  m_thread_spec_up->GetDescription(s, level);
147  else if (level == eDescriptionLevelBrief)
148  s->PutCString("thread spec: no ");
149  if (level == lldb::eDescriptionLevelFull) {
150  s->IndentLess();
151  s->IndentMore();
152  }
153  }
154 
155  GetCallbackDescription(s, level);
156 }
157 
159  Stream *s, lldb::DescriptionLevel level) const {
160  const CommandData *data = getItem();
161 
162  if (level == eDescriptionLevelBrief) {
163  s->Printf(", commands = %s",
164  (data && data->user_source.GetSize() > 0) ? "yes" : "no");
165  return;
166  }
167 
168  s->IndentMore();
169  s->Indent("watchpoint commands:\n");
170 
171  s->IndentMore();
172  if (data && data->user_source.GetSize() > 0) {
173  const size_t num_strings = data->user_source.GetSize();
174  for (size_t i = 0; i < num_strings; ++i) {
175  s->Indent(data->user_source.GetStringAtIndex(i));
176  s->EOL();
177  }
178  } else {
179  s->PutCString("No commands.\n");
180  }
181  s->IndentLess();
182  s->IndentLess();
183 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
void SetThreadID(lldb::tid_t thread_id)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static WatchpointOptions * CopyOptionsNoCallback(WatchpointOptions &rhs)
void GetCallbackDescription(Stream *s, lldb::DescriptionLevel level) const
Get description for callback only.
uint64_t user_id_t
Definition: lldb-types.h:84
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:82
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
static bool NullCallback(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
This is the default empty callback.
void ClearCallback()
Remove the callback from this option set.
uint64_t tid_t
Definition: lldb-types.h:86
ThreadSpec * GetThreadSpec()
Returns a pointer to the ThreadSpec for this option, creating it.
bool HasCallback()
Returns true if the watchpoint option has a callback set.
void SetCallback(WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the watchpoint option set.
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
Baton * GetBaton()
Fetch the baton from the callback.
bool InvokeCallback(StoppointCallbackContext *context, lldb::user_id_t watch_id)
Use this function to invoke the callback for a specific stop.
size_t GetSize() const
Definition: StringList.cpp:70
bool IsCallbackSynchronous()
Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
WatchpointOptions()
Default constructor.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
void SetTID(lldb::tid_t tid)
Definition: ThreadSpec.h:51
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
const WatchpointOptions & operator=(const WatchpointOptions &rhs)
const ThreadSpec * GetThreadSpecNoCreate() const
Return the current thread spec for this option.
A class designed to wrap callback batons so they can cleanup any acquired resources.
Definition: Baton.h:33
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
void GetDescription(Stream *s, lldb::DescriptionLevel level) const override