blob: 293f7b854b4e1af2b1216a9f035ec97897da7b78 [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 // Static functions.
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 static lldb::ProcessSP
28 CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
29 const lldb_private::FileSpec *crash_file_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 static void Initialize();
Johnny Chen9ed5b492012-01-05 21:48:15 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 static void Terminate();
Johnny Chen9ed5b492012-01-05 21:48:15 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 static lldb_private::ConstString GetPluginNameStatic();
Johnny Chen9ed5b492012-01-05 21:48:15 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 static const char *GetPluginDescriptionStatic();
Johnny Chen9ed5b492012-01-05 21:48:15 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 // Constructors and destructors
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 ProcessFreeBSD(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
41 lldb::UnixSignalsSP &unix_signals_sp);
Johnny Chen9ed5b492012-01-05 21:48:15 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 ~ProcessFreeBSD();
Ed Maste7fd845c2013-12-09 15:51:17 +000044
Zachary Turner97206d52017-05-12 04:51:55 +000045 virtual lldb_private::Status WillResume() override;
Ed Maste7fd845c2013-12-09 15:51:17 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 // PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 virtual lldb_private::ConstString GetPluginName() override;
Johnny Chen9ed5b492012-01-05 21:48:15 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 virtual uint32_t GetPluginVersion() override;
Johnny Chen9ed5b492012-01-05 21:48:15 +000051
Ed Mastefe5a6422015-07-28 15:45:57 +000052public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 // Process protocol.
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 void Finalize() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 bool CanDebug(lldb::TargetSP target_sp,
57 bool plugin_specified_by_name) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000058
Zachary Turner97206d52017-05-12 04:51:55 +000059 lldb_private::Status WillLaunch(lldb_private::Module *module) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000060
Zachary Turner97206d52017-05-12 04:51:55 +000061 lldb_private::Status DoAttachToProcessWithID(
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 lldb::pid_t pid,
63 const lldb_private::ProcessAttachInfo &attach_info) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000064
Zachary Turner97206d52017-05-12 04:51:55 +000065 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 DoLaunch(lldb_private::Module *exe_module,
67 lldb_private::ProcessLaunchInfo &launch_info) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 void DidLaunch() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000070
Zachary Turner97206d52017-05-12 04:51:55 +000071 lldb_private::Status DoResume() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000072
Zachary Turner97206d52017-05-12 04:51:55 +000073 lldb_private::Status DoHalt(bool &caused_stop) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000074
Zachary Turner97206d52017-05-12 04:51:55 +000075 lldb_private::Status DoDetach(bool keep_stopped) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000076
Zachary Turner97206d52017-05-12 04:51:55 +000077 lldb_private::Status DoSignal(int signal) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000078
Zachary Turner97206d52017-05-12 04:51:55 +000079 lldb_private::Status DoDestroy() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 void DoDidExec() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 void RefreshStateAfterStop() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 bool IsAlive() override;
Ed Mastefe5a6422015-07-28 15:45:57 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000088 lldb_private::Status &error) override;
Ed Mastefe5a6422015-07-28 15:45:57 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000091 lldb_private::Status &error) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +000092
93 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
Zachary Turner97206d52017-05-12 04:51:55 +000094 lldb_private::Status &error) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +000095
Zachary Turner97206d52017-05-12 04:51:55 +000096 lldb_private::Status DoDeallocateMemory(lldb::addr_t ptr) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +000097
98 virtual size_t
99 GetSoftwareBreakpointTrapOpcode(lldb_private::BreakpointSite *bp_site);
100
Zachary Turner97206d52017-05-12 04:51:55 +0000101 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
103
Zachary Turner97206d52017-05-12 04:51:55 +0000104 lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
106
Zachary Turner97206d52017-05-12 04:51:55 +0000107 lldb_private::Status EnableWatchpoint(lldb_private::Watchpoint *wp,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 bool notify = true) override;
109
Zachary Turner97206d52017-05-12 04:51:55 +0000110 lldb_private::Status DisableWatchpoint(lldb_private::Watchpoint *wp,
111 bool notify = true) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112
Zachary Turner97206d52017-05-12 04:51:55 +0000113 lldb_private::Status GetWatchpointSupportInfo(uint32_t &num) override;
114
115 lldb_private::Status GetWatchpointSupportInfo(uint32_t &num,
116 bool &after) override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117
118 virtual uint32_t UpdateThreadListIfNeeded();
119
120 bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
121 lldb_private::ThreadList &new_thread_list) override;
122
123 virtual lldb::ByteOrder GetByteOrder() const;
124
125 lldb::addr_t GetImageInfoAddress() override;
126
127 size_t PutSTDIN(const char *buf, size_t len,
Zachary Turner97206d52017-05-12 04:51:55 +0000128 lldb_private::Status &error) override;
Ed Mastefe5a6422015-07-28 15:45:57 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 const lldb::DataBufferSP GetAuxvData() override;
Ed Mastefe5a6422015-07-28 15:45:57 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 // ProcessFreeBSD internal API.
Ed Mastefe5a6422015-07-28 15:45:57 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 /// Registers the given message with this process.
135 virtual void SendMessage(const ProcessMessage &message);
Ed Mastefe5a6422015-07-28 15:45:57 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 ProcessMonitor &GetMonitor() {
138 assert(m_monitor);
139 return *m_monitor;
140 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 lldb_private::FileSpec
143 GetFileSpec(const lldb_private::FileAction *file_action,
144 const lldb_private::FileSpec &default_file_spec,
145 const lldb_private::FileSpec &dbg_pts_file_spec);
Ed Mastefe5a6422015-07-28 15:45:57 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 /// Adds the thread to the list of threads for which we have received the
148 /// initial stopping signal.
149 /// The \p stop_tid parameter indicates the thread which the stop happened
150 /// for.
151 bool AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 bool WaitingForInitialStop(lldb::tid_t stop_tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 virtual FreeBSDThread *CreateNewFreeBSDThread(lldb_private::Process &process,
156 lldb::tid_t tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000157
Ed Maste31f01802017-01-24 14:34:49 +0000158 static bool SingleStepBreakpointHit(
159 void *baton, lldb_private::StoppointCallbackContext *context,
160 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
161
Zachary Turner97206d52017-05-12 04:51:55 +0000162 lldb_private::Status SetupSoftwareSingleStepping(lldb::tid_t tid);
Ed Maste31f01802017-01-24 14:34:49 +0000163
Zachary Turner97206d52017-05-12 04:51:55 +0000164 lldb_private::Status SetSoftwareSingleStepBreakpoint(lldb::tid_t tid,
165 lldb::addr_t addr);
Ed Maste31f01802017-01-24 14:34:49 +0000166
167 bool IsSoftwareStepBreakpoint(lldb::tid_t tid);
168
169 bool SupportHardwareSingleStepping() const;
170
171 typedef std::vector<lldb::tid_t> tid_collection;
172 tid_collection &GetStepTids() { return m_step_tids; }
173
Ed Maste7fd845c2013-12-09 15:51:17 +0000174protected:
Ed Maste31f01802017-01-24 14:34:49 +0000175 static const size_t MAX_TRAP_OPCODE_SIZE = 8;
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 /// Target byte order.
178 lldb::ByteOrder m_byte_order;
Ed Mastefe5a6422015-07-28 15:45:57 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 /// Process monitor;
181 ProcessMonitor *m_monitor;
Ed Mastefe5a6422015-07-28 15:45:57 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 /// The module we are executing.
184 lldb_private::Module *m_module;
Ed Mastefe5a6422015-07-28 15:45:57 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 /// Message queue notifying this instance of inferior process state changes.
187 std::recursive_mutex m_message_mutex;
188 std::queue<ProcessMessage> m_message_queue;
Ed Mastefe5a6422015-07-28 15:45:57 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 /// Drive any exit events to completion.
191 bool m_exit_now;
Ed Mastefe5a6422015-07-28 15:45:57 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 /// Returns true if the process has exited.
194 bool HasExited();
Ed Mastefe5a6422015-07-28 15:45:57 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 /// Returns true if the process is stopped.
197 bool IsStopped();
Ed Mastefe5a6422015-07-28 15:45:57 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 /// Returns true if at least one running is currently running
200 bool IsAThreadRunning();
Ed Mastefe5a6422015-07-28 15:45:57 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
203 MMapMap m_addr_to_mmap_size;
Ed Mastefe5a6422015-07-28 15:45:57 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 typedef std::set<lldb::tid_t> ThreadStopSet;
206 /// Every thread begins with a stop signal. This keeps track
207 /// of the threads for which we have received the stop signal.
208 ThreadStopSet m_seen_initial_stop;
Ed Mastefe5a6422015-07-28 15:45:57 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 friend class FreeBSDThread;
Ed Maste7fd845c2013-12-09 15:51:17 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 tid_collection m_suspend_tids;
213 tid_collection m_run_tids;
214 tid_collection m_step_tids;
Ed Maste31f01802017-01-24 14:34:49 +0000215 std::map<lldb::tid_t, lldb::break_id_t> m_threads_stepping_with_breakpoint;
Ed Maste7fd845c2013-12-09 15:51:17 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 int m_resume_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218};
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220#endif // liblldb_ProcessFreeBSD_H_