180814287SRaphael Isemann //===-- WatchpointOptions.cpp ---------------------------------------------===//
2e9a5627eSJohnny Chen //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e9a5627eSJohnny Chen //
7e9a5627eSJohnny Chen //===----------------------------------------------------------------------===//
8e9a5627eSJohnny Chen
916fd7511SEugene Zelenko #include "lldb/Breakpoint/WatchpointOptions.h"
1016fd7511SEugene Zelenko
11b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
12e9a5627eSJohnny Chen #include "lldb/Core/Value.h"
13e9a5627eSJohnny Chen #include "lldb/Target/Process.h"
14e9a5627eSJohnny Chen #include "lldb/Target/Target.h"
15e9a5627eSJohnny Chen #include "lldb/Target/ThreadSpec.h"
16bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
17573ab909SZachary Turner #include "lldb/Utility/StringList.h"
18e9a5627eSJohnny Chen
19e9a5627eSJohnny Chen using namespace lldb;
20e9a5627eSJohnny Chen using namespace lldb_private;
21e9a5627eSJohnny Chen
NullCallback(void * baton,StoppointCallbackContext * context,lldb::user_id_t watch_id)22b9c1b51eSKate Stone bool WatchpointOptions::NullCallback(void *baton,
23b9c1b51eSKate Stone StoppointCallbackContext *context,
24b9c1b51eSKate Stone lldb::user_id_t watch_id) {
25e9a5627eSJohnny Chen return true;
26e9a5627eSJohnny Chen }
27e9a5627eSJohnny Chen
28e9a5627eSJohnny Chen // WatchpointOptions constructor
WatchpointOptions()29b9c1b51eSKate Stone WatchpointOptions::WatchpointOptions()
30*ee4b6cf5SKazu Hirata : m_callback(WatchpointOptions::NullCallback) {}
31e9a5627eSJohnny Chen
32e9a5627eSJohnny Chen // WatchpointOptions copy constructor
WatchpointOptions(const WatchpointOptions & rhs)33b9c1b51eSKate Stone WatchpointOptions::WatchpointOptions(const WatchpointOptions &rhs)
34b9c1b51eSKate Stone : m_callback(rhs.m_callback), m_callback_baton_sp(rhs.m_callback_baton_sp),
35*ee4b6cf5SKazu Hirata m_callback_is_synchronous(rhs.m_callback_is_synchronous) {
36d5b44036SJonas Devlieghere if (rhs.m_thread_spec_up != nullptr)
3706412daeSJonas Devlieghere m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
38e9a5627eSJohnny Chen }
39e9a5627eSJohnny Chen
40e9a5627eSJohnny Chen // WatchpointOptions assignment operator
41b9c1b51eSKate Stone const WatchpointOptions &WatchpointOptions::
operator =(const WatchpointOptions & rhs)42b9c1b51eSKate Stone operator=(const WatchpointOptions &rhs) {
43e9a5627eSJohnny Chen m_callback = rhs.m_callback;
44e9a5627eSJohnny Chen m_callback_baton_sp = rhs.m_callback_baton_sp;
45e9a5627eSJohnny Chen m_callback_is_synchronous = rhs.m_callback_is_synchronous;
46d5b44036SJonas Devlieghere if (rhs.m_thread_spec_up != nullptr)
4706412daeSJonas Devlieghere m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
48e9a5627eSJohnny Chen return *this;
49e9a5627eSJohnny Chen }
50e9a5627eSJohnny Chen
51e9a5627eSJohnny Chen WatchpointOptions *
CopyOptionsNoCallback(WatchpointOptions & orig)52b9c1b51eSKate Stone WatchpointOptions::CopyOptionsNoCallback(WatchpointOptions &orig) {
53e9a5627eSJohnny Chen WatchpointHitCallback orig_callback = orig.m_callback;
54e9a5627eSJohnny Chen lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
55e9a5627eSJohnny Chen bool orig_is_sync = orig.m_callback_is_synchronous;
56e9a5627eSJohnny Chen
57e9a5627eSJohnny Chen orig.ClearCallback();
58e9a5627eSJohnny Chen WatchpointOptions *ret_val = new WatchpointOptions(orig);
59e9a5627eSJohnny Chen
60e9a5627eSJohnny Chen orig.SetCallback(orig_callback, orig_callback_baton_sp, orig_is_sync);
61e9a5627eSJohnny Chen
62e9a5627eSJohnny Chen return ret_val;
63e9a5627eSJohnny Chen }
64e9a5627eSJohnny Chen
65e9a5627eSJohnny Chen // Destructor
6616fd7511SEugene Zelenko WatchpointOptions::~WatchpointOptions() = default;
67e9a5627eSJohnny Chen
68e9a5627eSJohnny Chen // Callbacks
SetCallback(WatchpointHitCallback callback,const BatonSP & callback_baton_sp,bool callback_is_synchronous)69b9c1b51eSKate Stone void WatchpointOptions::SetCallback(WatchpointHitCallback callback,
70b9c1b51eSKate Stone const BatonSP &callback_baton_sp,
71b9c1b51eSKate Stone bool callback_is_synchronous) {
72e9a5627eSJohnny Chen m_callback_is_synchronous = callback_is_synchronous;
73e9a5627eSJohnny Chen m_callback = callback;
74e9a5627eSJohnny Chen m_callback_baton_sp = callback_baton_sp;
75e9a5627eSJohnny Chen }
76e9a5627eSJohnny Chen
ClearCallback()77b9c1b51eSKate Stone void WatchpointOptions::ClearCallback() {
78e9a5627eSJohnny Chen m_callback = WatchpointOptions::NullCallback;
79e9a5627eSJohnny Chen m_callback_is_synchronous = false;
80e9a5627eSJohnny Chen m_callback_baton_sp.reset();
81e9a5627eSJohnny Chen }
82e9a5627eSJohnny Chen
GetBaton()83b9c1b51eSKate Stone Baton *WatchpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
84b9c1b51eSKate Stone
GetBaton() const85b9c1b51eSKate Stone const Baton *WatchpointOptions::GetBaton() const {
86e9a5627eSJohnny Chen return m_callback_baton_sp.get();
87e9a5627eSJohnny Chen }
88e9a5627eSJohnny Chen
InvokeCallback(StoppointCallbackContext * context,lldb::user_id_t watch_id)89b9c1b51eSKate Stone bool WatchpointOptions::InvokeCallback(StoppointCallbackContext *context,
90b9c1b51eSKate Stone lldb::user_id_t watch_id) {
91b9c1b51eSKate Stone if (m_callback && context->is_synchronous == IsCallbackSynchronous()) {
924e4fbe82SZachary Turner return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
93b9c1b51eSKate Stone : nullptr,
94b9c1b51eSKate Stone context, watch_id);
95b9c1b51eSKate Stone } else
96e9a5627eSJohnny Chen return true;
97e9a5627eSJohnny Chen }
98e9a5627eSJohnny Chen
HasCallback()99b9c1b51eSKate Stone bool WatchpointOptions::HasCallback() {
100e9a5627eSJohnny Chen return m_callback != WatchpointOptions::NullCallback;
101e9a5627eSJohnny Chen }
102e9a5627eSJohnny Chen
GetThreadSpecNoCreate() const103b9c1b51eSKate Stone const ThreadSpec *WatchpointOptions::GetThreadSpecNoCreate() const {
104d5b44036SJonas Devlieghere return m_thread_spec_up.get();
105e9a5627eSJohnny Chen }
106e9a5627eSJohnny Chen
GetThreadSpec()107b9c1b51eSKate Stone ThreadSpec *WatchpointOptions::GetThreadSpec() {
108d5b44036SJonas Devlieghere if (m_thread_spec_up == nullptr)
10906412daeSJonas Devlieghere m_thread_spec_up = std::make_unique<ThreadSpec>();
110e9a5627eSJohnny Chen
111d5b44036SJonas Devlieghere return m_thread_spec_up.get();
112e9a5627eSJohnny Chen }
113e9a5627eSJohnny Chen
SetThreadID(lldb::tid_t thread_id)114b9c1b51eSKate Stone void WatchpointOptions::SetThreadID(lldb::tid_t thread_id) {
115e9a5627eSJohnny Chen GetThreadSpec()->SetTID(thread_id);
116e9a5627eSJohnny Chen }
117e9a5627eSJohnny Chen
GetCallbackDescription(Stream * s,lldb::DescriptionLevel level) const118b9c1b51eSKate Stone void WatchpointOptions::GetCallbackDescription(
119b9c1b51eSKate Stone Stream *s, lldb::DescriptionLevel level) const {
120b9c1b51eSKate Stone if (m_callback_baton_sp.get()) {
121e9a5627eSJohnny Chen s->EOL();
1224f728bfcSRaphael Isemann m_callback_baton_sp->GetDescription(s->AsRawOstream(), level,
1234f728bfcSRaphael Isemann s->GetIndentLevel());
124e9a5627eSJohnny Chen }
125e9a5627eSJohnny Chen }
12616fd7511SEugene Zelenko
GetDescription(Stream * s,lldb::DescriptionLevel level) const127b9c1b51eSKate Stone void WatchpointOptions::GetDescription(Stream *s,
128b9c1b51eSKate Stone lldb::DescriptionLevel level) const {
129b9c1b51eSKate Stone // Figure out if there are any options not at their default value, and only
13005097246SAdrian Prantl // print anything if there are:
131e9a5627eSJohnny Chen
132b9c1b51eSKate Stone if ((GetThreadSpecNoCreate() != nullptr &&
133b9c1b51eSKate Stone GetThreadSpecNoCreate()->HasSpecification())) {
134b9c1b51eSKate Stone if (level == lldb::eDescriptionLevelVerbose) {
135e9a5627eSJohnny Chen s->EOL();
136e9a5627eSJohnny Chen s->IndentMore();
137e9a5627eSJohnny Chen s->Indent();
138e9a5627eSJohnny Chen s->PutCString("Watchpoint Options:\n");
139e9a5627eSJohnny Chen s->IndentMore();
140e9a5627eSJohnny Chen s->Indent();
141b9c1b51eSKate Stone } else
142e9a5627eSJohnny Chen s->PutCString(" Options: ");
143e9a5627eSJohnny Chen
144d5b44036SJonas Devlieghere if (m_thread_spec_up)
145d5b44036SJonas Devlieghere m_thread_spec_up->GetDescription(s, level);
146e9a5627eSJohnny Chen else if (level == eDescriptionLevelBrief)
147e9a5627eSJohnny Chen s->PutCString("thread spec: no ");
148b9c1b51eSKate Stone if (level == lldb::eDescriptionLevelFull) {
149e9a5627eSJohnny Chen s->IndentLess();
150e9a5627eSJohnny Chen s->IndentMore();
151e9a5627eSJohnny Chen }
152e9a5627eSJohnny Chen }
153e9a5627eSJohnny Chen
154e9a5627eSJohnny Chen GetCallbackDescription(s, level);
155e9a5627eSJohnny Chen }
156e9a5627eSJohnny Chen
GetDescription(llvm::raw_ostream & s,lldb::DescriptionLevel level,unsigned indentation) const157b9c1b51eSKate Stone void WatchpointOptions::CommandBaton::GetDescription(
1584f728bfcSRaphael Isemann llvm::raw_ostream &s, lldb::DescriptionLevel level,
1594f728bfcSRaphael Isemann unsigned indentation) const {
1604e4fbe82SZachary Turner const CommandData *data = getItem();
161e9a5627eSJohnny Chen
162b9c1b51eSKate Stone if (level == eDescriptionLevelBrief) {
1634f728bfcSRaphael Isemann s << ", commands = %s"
1644f728bfcSRaphael Isemann << ((data && data->user_source.GetSize() > 0) ? "yes" : "no");
165e9a5627eSJohnny Chen return;
166e9a5627eSJohnny Chen }
167e9a5627eSJohnny Chen
1684f728bfcSRaphael Isemann indentation += 2;
1694f728bfcSRaphael Isemann s.indent(indentation);
1704f728bfcSRaphael Isemann s << "watchpoint commands:\n";
171e9a5627eSJohnny Chen
1724f728bfcSRaphael Isemann indentation += 2;
173b9c1b51eSKate Stone if (data && data->user_source.GetSize() > 0) {
1744c78b788SRaphael Isemann for (const std::string &line : data->user_source) {
1754f728bfcSRaphael Isemann s.indent(indentation);
1764f728bfcSRaphael Isemann s << line << "\n";
177e9a5627eSJohnny Chen }
1784f728bfcSRaphael Isemann } else
1794f728bfcSRaphael Isemann s << "No commands.\n";
180e9a5627eSJohnny Chen }
181