blob: e8a0cd9a9dff3bf104b7782d9f4d87ad30f8a0eb [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- ProcessFreeBSD.h ------------------------------------------*- C++
2//-*-===//
Johnny Chen9ed5b492012-01-05 21:48:15 +00003//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Johnny Chen9ed5b492012-01-05 21:48:15 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_ProcessFreeBSD_H_
11#define liblldb_ProcessFreeBSD_H_
12
Pavel Labathf0a6d8a2017-04-11 12:26:25 +000013#include "Plugins/Process/POSIX/ProcessMessage.h"
14#include "lldb/Target/Process.h"
15#include "lldb/Target/ThreadList.h"
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000016#include <mutex>
Johnny Chen9ed5b492012-01-05 21:48:15 +000017#include <queue>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000018#include <set>
Johnny Chen9ed5b492012-01-05 21:48:15 +000019
Johnny Chen9ed5b492012-01-05 21:48:15 +000020class ProcessMonitor;
Ed Mastefe5a6422015-07-28 15:45:57 +000021class FreeBSDThread;
Johnny Chen9ed5b492012-01-05 21:48:15 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023class ProcessFreeBSD : public lldb_private::Process {
Johnny Chen9ed5b492012-01-05 21:48:15 +000024
25public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000026 //------------------------------------------------------------------
27 // Static functions.
28 //------------------------------------------------------------------
29 static lldb::ProcessSP
30 CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
31 const lldb_private::FileSpec *crash_file_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 static void Initialize();
Johnny Chen9ed5b492012-01-05 21:48:15 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 static void Terminate();
Johnny Chen9ed5b492012-01-05 21:48:15 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 static lldb_private::ConstString GetPluginNameStatic();
Johnny Chen9ed5b492012-01-05 21:48:15 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 static const char *GetPluginDescriptionStatic();
Johnny Chen9ed5b492012-01-05 21:48:15 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 //------------------------------------------------------------------
42 // Constructors and destructors
43 //------------------------------------------------------------------
44 ProcessFreeBSD(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
45 lldb::UnixSignalsSP &unix_signals_sp);
Johnny Chen9ed5b492012-01-05 21:48:15 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 ~ProcessFreeBSD();
Ed Maste7fd845c2013-12-09 15:51:17 +000048
Zachary Turner97206d52017-05-12 04:51:55 +000049 virtual lldb_private::Status WillResume() override;
Ed Maste7fd845c2013-12-09 15:51:17 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 //------------------------------------------------------------------
52 // PluginInterface protocol
53 //------------------------------------------------------------------
54 virtual lldb_private::ConstString GetPluginName() override;
Johnny Chen9ed5b492012-01-05 21:48:15 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 virtual uint32_t GetPluginVersion() override;
Johnny Chen9ed5b492012-01-05 21:48:15 +000057
Ed Mastefe5a6422015-07-28 15:45:57 +000058public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 //------------------------------------------------------------------
60 // Process protocol.
61 //------------------------------------------------------------------
62 void Finalize() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 bool CanDebug(lldb::TargetSP target_sp,
65 bool plugin_specified_by_name) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000066
Zachary Turner97206d52017-05-12 04:51:55 +000067 lldb_private::Status WillLaunch(lldb_private::Module *module) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000068
Zachary Turner97206d52017-05-12 04:51:55 +000069 lldb_private::Status DoAttachToProcessWithID(
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 lldb::pid_t pid,
71 const lldb_private::ProcessAttachInfo &attach_info) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000072
Zachary Turner97206d52017-05-12 04:51:55 +000073 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 DoLaunch(lldb_private::Module *exe_module,
75 lldb_private::ProcessLaunchInfo &launch_info) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 void DidLaunch() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000078
Zachary Turner97206d52017-05-12 04:51:55 +000079 lldb_private::Status DoResume() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000080
Zachary Turner97206d52017-05-12 04:51:55 +000081 lldb_private::Status DoHalt(bool &caused_stop) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000082
Zachary Turner97206d52017-05-12 04:51:55 +000083 lldb_private::Status DoDetach(bool keep_stopped) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000084
Zachary Turner97206d52017-05-12 04:51:55 +000085 lldb_private::Status DoSignal(int signal) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000086
Zachary Turner97206d52017-05-12 04:51:55 +000087 lldb_private::Status DoDestroy() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 void DoDidExec() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 void RefreshStateAfterStop() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 bool IsAlive() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000096 lldb_private::Status &error) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000099 lldb_private::Status &error) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100
101 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
Zachary Turner97206d52017-05-12 04:51:55 +0000102 lldb_private::Status &error) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103
Zachary Turner97206d52017-05-12 04:51:55 +0000104 lldb_private::Status DoDeallocateMemory(lldb::addr_t ptr) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105
106 virtual size_t
107 GetSoftwareBreakpointTrapOpcode(lldb_private::BreakpointSite *bp_site);
108
Zachary Turner97206d52017-05-12 04:51:55 +0000109 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
111
Zachary Turner97206d52017-05-12 04:51:55 +0000112 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
114
Zachary Turner97206d52017-05-12 04:51:55 +0000115 lldb_private::Status EnableWatchpoint(lldb_private::Watchpoint *wp,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 bool notify = true) override;
117
Zachary Turner97206d52017-05-12 04:51:55 +0000118 lldb_private::Status DisableWatchpoint(lldb_private::Watchpoint *wp,
119 bool notify = true) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120
Zachary Turner97206d52017-05-12 04:51:55 +0000121 lldb_private::Status GetWatchpointSupportInfo(uint32_t &num) override;
122
123 lldb_private::Status GetWatchpointSupportInfo(uint32_t &num,
124 bool &after) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125
126 virtual uint32_t UpdateThreadListIfNeeded();
127
128 bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
129 lldb_private::ThreadList &new_thread_list) override;
130
131 virtual lldb::ByteOrder GetByteOrder() const;
132
133 lldb::addr_t GetImageInfoAddress() override;
134
135 size_t PutSTDIN(const char *buf, size_t len,
Zachary Turner97206d52017-05-12 04:51:55 +0000136 lldb_private::Status &error) override;
Ed Mastefe5a6422015-07-28 15:45:57 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 const lldb::DataBufferSP GetAuxvData() override;
Ed Mastefe5a6422015-07-28 15:45:57 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 //--------------------------------------------------------------------------
141 // ProcessFreeBSD internal API.
Ed Mastefe5a6422015-07-28 15:45:57 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 /// Registers the given message with this process.
144 virtual void SendMessage(const ProcessMessage &message);
Ed Mastefe5a6422015-07-28 15:45:57 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 ProcessMonitor &GetMonitor() {
147 assert(m_monitor);
148 return *m_monitor;
149 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 lldb_private::FileSpec
152 GetFileSpec(const lldb_private::FileAction *file_action,
153 const lldb_private::FileSpec &default_file_spec,
154 const lldb_private::FileSpec &dbg_pts_file_spec);
Ed Mastefe5a6422015-07-28 15:45:57 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 /// Adds the thread to the list of threads for which we have received the
157 /// initial stopping signal.
158 /// The \p stop_tid parameter indicates the thread which the stop happened
159 /// for.
160 bool AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 bool WaitingForInitialStop(lldb::tid_t stop_tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 virtual FreeBSDThread *CreateNewFreeBSDThread(lldb_private::Process &process,
165 lldb::tid_t tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000166
Ed Maste31f01802017-01-24 14:34:49 +0000167 static bool SingleStepBreakpointHit(
168 void *baton, lldb_private::StoppointCallbackContext *context,
169 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
170
Zachary Turner97206d52017-05-12 04:51:55 +0000171 lldb_private::Status SetupSoftwareSingleStepping(lldb::tid_t tid);
Ed Maste31f01802017-01-24 14:34:49 +0000172
Zachary Turner97206d52017-05-12 04:51:55 +0000173 lldb_private::Status SetSoftwareSingleStepBreakpoint(lldb::tid_t tid,
174 lldb::addr_t addr);
Ed Maste31f01802017-01-24 14:34:49 +0000175
176 bool IsSoftwareStepBreakpoint(lldb::tid_t tid);
177
178 bool SupportHardwareSingleStepping() const;
179
180 typedef std::vector<lldb::tid_t> tid_collection;
181 tid_collection &GetStepTids() { return m_step_tids; }
182
Ed Maste7fd845c2013-12-09 15:51:17 +0000183protected:
Ed Maste31f01802017-01-24 14:34:49 +0000184 static const size_t MAX_TRAP_OPCODE_SIZE = 8;
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 /// Target byte order.
187 lldb::ByteOrder m_byte_order;
Ed Mastefe5a6422015-07-28 15:45:57 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 /// Process monitor;
190 ProcessMonitor *m_monitor;
Ed Mastefe5a6422015-07-28 15:45:57 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 /// The module we are executing.
193 lldb_private::Module *m_module;
Ed Mastefe5a6422015-07-28 15:45:57 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 /// Message queue notifying this instance of inferior process state changes.
196 std::recursive_mutex m_message_mutex;
197 std::queue<ProcessMessage> m_message_queue;
Ed Mastefe5a6422015-07-28 15:45:57 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 /// Drive any exit events to completion.
200 bool m_exit_now;
Ed Mastefe5a6422015-07-28 15:45:57 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 /// Returns true if the process has exited.
203 bool HasExited();
Ed Mastefe5a6422015-07-28 15:45:57 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 /// Returns true if the process is stopped.
206 bool IsStopped();
Ed Mastefe5a6422015-07-28 15:45:57 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 /// Returns true if at least one running is currently running
209 bool IsAThreadRunning();
Ed Mastefe5a6422015-07-28 15:45:57 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
212 MMapMap m_addr_to_mmap_size;
Ed Mastefe5a6422015-07-28 15:45:57 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 typedef std::set<lldb::tid_t> ThreadStopSet;
215 /// Every thread begins with a stop signal. This keeps track
216 /// of the threads for which we have received the stop signal.
217 ThreadStopSet m_seen_initial_stop;
Ed Mastefe5a6422015-07-28 15:45:57 +0000218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 friend class FreeBSDThread;
Ed Maste7fd845c2013-12-09 15:51:17 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 tid_collection m_suspend_tids;
222 tid_collection m_run_tids;
223 tid_collection m_step_tids;
Ed Maste31f01802017-01-24 14:34:49 +0000224 std::map<lldb::tid_t, lldb::break_id_t> m_threads_stepping_with_breakpoint;
Ed Maste7fd845c2013-12-09 15:51:17 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 int m_resume_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000227};
228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229#endif // liblldb_ProcessFreeBSD_H_