15ffd83dbSDimitry Andric //===-- WatchpointOptions.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Breakpoint/WatchpointOptions.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
120b57cec5SDimitry Andric #include "lldb/Core/Value.h"
130b57cec5SDimitry Andric #include "lldb/Target/Process.h"
140b57cec5SDimitry Andric #include "lldb/Target/Target.h"
150b57cec5SDimitry Andric #include "lldb/Target/ThreadSpec.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric #include "lldb/Utility/StringList.h"
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric using namespace lldb;
200b57cec5SDimitry Andric using namespace lldb_private;
210b57cec5SDimitry Andric
NullCallback(void * baton,StoppointCallbackContext * context,lldb::user_id_t watch_id)220b57cec5SDimitry Andric bool WatchpointOptions::NullCallback(void *baton,
230b57cec5SDimitry Andric StoppointCallbackContext *context,
240b57cec5SDimitry Andric lldb::user_id_t watch_id) {
250b57cec5SDimitry Andric return true;
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric // WatchpointOptions constructor
WatchpointOptions()290b57cec5SDimitry Andric WatchpointOptions::WatchpointOptions()
30*81ad6265SDimitry Andric : m_callback(WatchpointOptions::NullCallback) {}
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric // WatchpointOptions copy constructor
WatchpointOptions(const WatchpointOptions & rhs)330b57cec5SDimitry Andric WatchpointOptions::WatchpointOptions(const WatchpointOptions &rhs)
340b57cec5SDimitry Andric : m_callback(rhs.m_callback), m_callback_baton_sp(rhs.m_callback_baton_sp),
35*81ad6265SDimitry Andric m_callback_is_synchronous(rhs.m_callback_is_synchronous) {
360b57cec5SDimitry Andric if (rhs.m_thread_spec_up != nullptr)
375ffd83dbSDimitry Andric m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric // WatchpointOptions assignment operator
410b57cec5SDimitry Andric const WatchpointOptions &WatchpointOptions::
operator =(const WatchpointOptions & rhs)420b57cec5SDimitry Andric operator=(const WatchpointOptions &rhs) {
430b57cec5SDimitry Andric m_callback = rhs.m_callback;
440b57cec5SDimitry Andric m_callback_baton_sp = rhs.m_callback_baton_sp;
450b57cec5SDimitry Andric m_callback_is_synchronous = rhs.m_callback_is_synchronous;
460b57cec5SDimitry Andric if (rhs.m_thread_spec_up != nullptr)
475ffd83dbSDimitry Andric m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
480b57cec5SDimitry Andric return *this;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric WatchpointOptions *
CopyOptionsNoCallback(WatchpointOptions & orig)520b57cec5SDimitry Andric WatchpointOptions::CopyOptionsNoCallback(WatchpointOptions &orig) {
530b57cec5SDimitry Andric WatchpointHitCallback orig_callback = orig.m_callback;
540b57cec5SDimitry Andric lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
550b57cec5SDimitry Andric bool orig_is_sync = orig.m_callback_is_synchronous;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric orig.ClearCallback();
580b57cec5SDimitry Andric WatchpointOptions *ret_val = new WatchpointOptions(orig);
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric orig.SetCallback(orig_callback, orig_callback_baton_sp, orig_is_sync);
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric return ret_val;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric // Destructor
660b57cec5SDimitry Andric WatchpointOptions::~WatchpointOptions() = default;
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric // Callbacks
SetCallback(WatchpointHitCallback callback,const BatonSP & callback_baton_sp,bool callback_is_synchronous)690b57cec5SDimitry Andric void WatchpointOptions::SetCallback(WatchpointHitCallback callback,
700b57cec5SDimitry Andric const BatonSP &callback_baton_sp,
710b57cec5SDimitry Andric bool callback_is_synchronous) {
720b57cec5SDimitry Andric m_callback_is_synchronous = callback_is_synchronous;
730b57cec5SDimitry Andric m_callback = callback;
740b57cec5SDimitry Andric m_callback_baton_sp = callback_baton_sp;
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
ClearCallback()770b57cec5SDimitry Andric void WatchpointOptions::ClearCallback() {
780b57cec5SDimitry Andric m_callback = WatchpointOptions::NullCallback;
790b57cec5SDimitry Andric m_callback_is_synchronous = false;
800b57cec5SDimitry Andric m_callback_baton_sp.reset();
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
GetBaton()830b57cec5SDimitry Andric Baton *WatchpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
840b57cec5SDimitry Andric
GetBaton() const850b57cec5SDimitry Andric const Baton *WatchpointOptions::GetBaton() const {
860b57cec5SDimitry Andric return m_callback_baton_sp.get();
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
InvokeCallback(StoppointCallbackContext * context,lldb::user_id_t watch_id)890b57cec5SDimitry Andric bool WatchpointOptions::InvokeCallback(StoppointCallbackContext *context,
900b57cec5SDimitry Andric lldb::user_id_t watch_id) {
910b57cec5SDimitry Andric if (m_callback && context->is_synchronous == IsCallbackSynchronous()) {
920b57cec5SDimitry Andric return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
930b57cec5SDimitry Andric : nullptr,
940b57cec5SDimitry Andric context, watch_id);
950b57cec5SDimitry Andric } else
960b57cec5SDimitry Andric return true;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
HasCallback()990b57cec5SDimitry Andric bool WatchpointOptions::HasCallback() {
1000b57cec5SDimitry Andric return m_callback != WatchpointOptions::NullCallback;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
GetThreadSpecNoCreate() const1030b57cec5SDimitry Andric const ThreadSpec *WatchpointOptions::GetThreadSpecNoCreate() const {
1040b57cec5SDimitry Andric return m_thread_spec_up.get();
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
GetThreadSpec()1070b57cec5SDimitry Andric ThreadSpec *WatchpointOptions::GetThreadSpec() {
1080b57cec5SDimitry Andric if (m_thread_spec_up == nullptr)
1095ffd83dbSDimitry Andric m_thread_spec_up = std::make_unique<ThreadSpec>();
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric return m_thread_spec_up.get();
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
SetThreadID(lldb::tid_t thread_id)1140b57cec5SDimitry Andric void WatchpointOptions::SetThreadID(lldb::tid_t thread_id) {
1150b57cec5SDimitry Andric GetThreadSpec()->SetTID(thread_id);
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
GetCallbackDescription(Stream * s,lldb::DescriptionLevel level) const1180b57cec5SDimitry Andric void WatchpointOptions::GetCallbackDescription(
1190b57cec5SDimitry Andric Stream *s, lldb::DescriptionLevel level) const {
1200b57cec5SDimitry Andric if (m_callback_baton_sp.get()) {
1210b57cec5SDimitry Andric s->EOL();
122480093f4SDimitry Andric m_callback_baton_sp->GetDescription(s->AsRawOstream(), level,
123480093f4SDimitry Andric s->GetIndentLevel());
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
GetDescription(Stream * s,lldb::DescriptionLevel level) const1270b57cec5SDimitry Andric void WatchpointOptions::GetDescription(Stream *s,
1280b57cec5SDimitry Andric lldb::DescriptionLevel level) const {
1290b57cec5SDimitry Andric // Figure out if there are any options not at their default value, and only
1300b57cec5SDimitry Andric // print anything if there are:
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric if ((GetThreadSpecNoCreate() != nullptr &&
1330b57cec5SDimitry Andric GetThreadSpecNoCreate()->HasSpecification())) {
1340b57cec5SDimitry Andric if (level == lldb::eDescriptionLevelVerbose) {
1350b57cec5SDimitry Andric s->EOL();
1360b57cec5SDimitry Andric s->IndentMore();
1370b57cec5SDimitry Andric s->Indent();
1380b57cec5SDimitry Andric s->PutCString("Watchpoint Options:\n");
1390b57cec5SDimitry Andric s->IndentMore();
1400b57cec5SDimitry Andric s->Indent();
1410b57cec5SDimitry Andric } else
1420b57cec5SDimitry Andric s->PutCString(" Options: ");
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric if (m_thread_spec_up)
1450b57cec5SDimitry Andric m_thread_spec_up->GetDescription(s, level);
1460b57cec5SDimitry Andric else if (level == eDescriptionLevelBrief)
1470b57cec5SDimitry Andric s->PutCString("thread spec: no ");
1480b57cec5SDimitry Andric if (level == lldb::eDescriptionLevelFull) {
1490b57cec5SDimitry Andric s->IndentLess();
1500b57cec5SDimitry Andric s->IndentMore();
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric GetCallbackDescription(s, level);
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric
GetDescription(llvm::raw_ostream & s,lldb::DescriptionLevel level,unsigned indentation) const1570b57cec5SDimitry Andric void WatchpointOptions::CommandBaton::GetDescription(
158480093f4SDimitry Andric llvm::raw_ostream &s, lldb::DescriptionLevel level,
159480093f4SDimitry Andric unsigned indentation) const {
1600b57cec5SDimitry Andric const CommandData *data = getItem();
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric if (level == eDescriptionLevelBrief) {
163480093f4SDimitry Andric s << ", commands = %s"
164480093f4SDimitry Andric << ((data && data->user_source.GetSize() > 0) ? "yes" : "no");
1650b57cec5SDimitry Andric return;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
168480093f4SDimitry Andric indentation += 2;
169480093f4SDimitry Andric s.indent(indentation);
170480093f4SDimitry Andric s << "watchpoint commands:\n";
1710b57cec5SDimitry Andric
172480093f4SDimitry Andric indentation += 2;
1730b57cec5SDimitry Andric if (data && data->user_source.GetSize() > 0) {
1749dba64beSDimitry Andric for (const std::string &line : data->user_source) {
175480093f4SDimitry Andric s.indent(indentation);
176480093f4SDimitry Andric s << line << "\n";
1770b57cec5SDimitry Andric }
178480093f4SDimitry Andric } else
179480093f4SDimitry Andric s << "No commands.\n";
1800b57cec5SDimitry Andric }
181