180814287SRaphael Isemann //===-- SBBreakpointOptionCommon.cpp --------------------------------------===//
2b842f2ecSJim Ingham //
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
6b842f2ecSJim Ingham //
7b842f2ecSJim Ingham //===----------------------------------------------------------------------===//
8b842f2ecSJim Ingham 
9b842f2ecSJim Ingham #include "lldb/API/SBBreakpointName.h"
10b842f2ecSJim Ingham #include "lldb/API/SBBreakpointLocation.h"
11b842f2ecSJim Ingham #include "lldb/API/SBDebugger.h"
12b842f2ecSJim Ingham #include "lldb/API/SBEvent.h"
13b842f2ecSJim Ingham #include "lldb/API/SBProcess.h"
14b842f2ecSJim Ingham #include "lldb/API/SBStream.h"
15b842f2ecSJim Ingham #include "lldb/API/SBStringList.h"
16b842f2ecSJim Ingham #include "lldb/API/SBThread.h"
17b842f2ecSJim Ingham 
18b842f2ecSJim Ingham #include "lldb/Breakpoint/BreakpointName.h"
19b842f2ecSJim Ingham #include "lldb/Breakpoint/StoppointCallbackContext.h"
20b842f2ecSJim Ingham #include "lldb/Core/Address.h"
21b842f2ecSJim Ingham #include "lldb/Core/Debugger.h"
22b842f2ecSJim Ingham #include "lldb/Core/StreamFile.h"
23b842f2ecSJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
24b842f2ecSJim Ingham #include "lldb/Interpreter/ScriptInterpreter.h"
25b842f2ecSJim Ingham #include "lldb/Target/Process.h"
26b842f2ecSJim Ingham #include "lldb/Target/Target.h"
27b842f2ecSJim Ingham #include "lldb/Target/Thread.h"
28b842f2ecSJim Ingham #include "lldb/Target/ThreadSpec.h"
29*1755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
30b842f2ecSJim Ingham #include "lldb/Utility/Log.h"
31b842f2ecSJim Ingham #include "lldb/Utility/Stream.h"
32b842f2ecSJim Ingham 
33b842f2ecSJim Ingham #include "lldb/lldb-enumerations.h"
34b842f2ecSJim Ingham 
35b842f2ecSJim Ingham #include "SBBreakpointOptionCommon.h"
36b842f2ecSJim Ingham 
37b842f2ecSJim Ingham #include "llvm/ADT/STLExtras.h"
38b842f2ecSJim Ingham 
39b842f2ecSJim Ingham using namespace lldb;
40b842f2ecSJim Ingham using namespace lldb_private;
41b842f2ecSJim Ingham 
SBBreakpointCallbackBaton(SBBreakpointHitCallback callback,void * baton)42*1755f5b1SJonas Devlieghere SBBreakpointCallbackBaton::SBBreakpointCallbackBaton(
43*1755f5b1SJonas Devlieghere     SBBreakpointHitCallback callback, void *baton)
44a8f3ae7cSJonas Devlieghere     : TypedBaton(std::make_unique<CallbackData>()) {
45*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, callback, baton);
46b842f2ecSJim Ingham   getItem()->callback = callback;
47b842f2ecSJim Ingham   getItem()->callback_baton = baton;
48b842f2ecSJim Ingham }
49b842f2ecSJim Ingham 
PrivateBreakpointHitCallback(void * baton,StoppointCallbackContext * ctx,lldb::user_id_t break_id,lldb::user_id_t break_loc_id)50*1755f5b1SJonas Devlieghere bool SBBreakpointCallbackBaton::PrivateBreakpointHitCallback(
51*1755f5b1SJonas Devlieghere     void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
52*1755f5b1SJonas Devlieghere     lldb::user_id_t break_loc_id) {
53*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(baton, ctx, break_id, break_loc_id);
54b842f2ecSJim Ingham   ExecutionContext exe_ctx(ctx->exe_ctx_ref);
55b842f2ecSJim Ingham   BreakpointSP bp_sp(
56b842f2ecSJim Ingham       exe_ctx.GetTargetRef().GetBreakpointList().FindBreakpointByID(break_id));
57b842f2ecSJim Ingham   if (baton && bp_sp) {
58b842f2ecSJim Ingham     CallbackData *data = (CallbackData *)baton;
59b842f2ecSJim Ingham     lldb_private::Breakpoint *bp = bp_sp.get();
60b842f2ecSJim Ingham     if (bp && data->callback) {
61b842f2ecSJim Ingham       Process *process = exe_ctx.GetProcessPtr();
62b842f2ecSJim Ingham       if (process) {
63b842f2ecSJim Ingham         SBProcess sb_process(process->shared_from_this());
64b842f2ecSJim Ingham         SBThread sb_thread;
65b842f2ecSJim Ingham         SBBreakpointLocation sb_location;
66b842f2ecSJim Ingham         assert(bp_sp);
67b842f2ecSJim Ingham         sb_location.SetLocation(bp_sp->FindLocationByID(break_loc_id));
68b842f2ecSJim Ingham         Thread *thread = exe_ctx.GetThreadPtr();
69b842f2ecSJim Ingham         if (thread)
70b842f2ecSJim Ingham           sb_thread.SetThread(thread->shared_from_this());
71b842f2ecSJim Ingham 
72b842f2ecSJim Ingham         return data->callback(data->callback_baton, sb_process, sb_thread,
73b842f2ecSJim Ingham                               sb_location);
74b842f2ecSJim Ingham       }
75b842f2ecSJim Ingham     }
76b842f2ecSJim Ingham   }
77b842f2ecSJim Ingham   return true; // Return true if we should stop at this breakpoint
78b842f2ecSJim Ingham }
79b842f2ecSJim Ingham 
80e2564051SDavide Italiano SBBreakpointCallbackBaton::~SBBreakpointCallbackBaton() = default;
81