1254721Semaste//===-- Process.h -----------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_Process_h_
11254721Semaste#define liblldb_Process_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste#include <limits.h>
15254721Semaste#include <spawn.h>
16254721Semaste
17254721Semaste// C++ Includes
18254721Semaste#include <list>
19254721Semaste#include <iosfwd>
20254721Semaste#include <vector>
21254721Semaste
22254721Semaste// Other libraries and framework includes
23254721Semaste// Project includes
24254721Semaste#include "lldb/lldb-private.h"
25254721Semaste#include "lldb/Core/ArchSpec.h"
26254721Semaste#include "lldb/Core/Broadcaster.h"
27254721Semaste#include "lldb/Core/Communication.h"
28254721Semaste#include "lldb/Core/Error.h"
29254721Semaste#include "lldb/Core/Event.h"
30254721Semaste#include "lldb/Core/RangeMap.h"
31254721Semaste#include "lldb/Core/StringList.h"
32254721Semaste#include "lldb/Core/ThreadSafeValue.h"
33254721Semaste#include "lldb/Core/PluginInterface.h"
34254721Semaste#include "lldb/Core/UserSettingsController.h"
35254721Semaste#include "lldb/Breakpoint/BreakpointSiteList.h"
36254721Semaste#include "lldb/Expression/ClangPersistentVariables.h"
37254721Semaste#include "lldb/Expression/IRDynamicChecks.h"
38254721Semaste#include "lldb/Host/FileSpec.h"
39254721Semaste#include "lldb/Host/Host.h"
40254721Semaste#include "lldb/Host/ProcessRunLock.h"
41254721Semaste#include "lldb/Interpreter/Args.h"
42254721Semaste#include "lldb/Interpreter/Options.h"
43254721Semaste#include "lldb/Target/ExecutionContextScope.h"
44254721Semaste#include "lldb/Target/Memory.h"
45254721Semaste#include "lldb/Target/ThreadList.h"
46254721Semaste#include "lldb/Target/UnixSignals.h"
47254721Semaste#include "lldb/Utility/PseudoTerminal.h"
48254721Semaste
49254721Semastenamespace lldb_private {
50254721Semaste
51254721Semaste//----------------------------------------------------------------------
52254721Semaste// ProcessProperties
53254721Semaste//----------------------------------------------------------------------
54254721Semasteclass ProcessProperties : public Properties
55254721Semaste{
56254721Semastepublic:
57254721Semaste    ProcessProperties(bool is_global);
58254721Semaste
59254721Semaste    virtual
60254721Semaste    ~ProcessProperties();
61254721Semaste
62254721Semaste    bool
63254721Semaste    GetDisableMemoryCache() const;
64254721Semaste
65254721Semaste    Args
66254721Semaste    GetExtraStartupCommands () const;
67254721Semaste
68254721Semaste    void
69254721Semaste    SetExtraStartupCommands (const Args &args);
70254721Semaste
71254721Semaste    FileSpec
72254721Semaste    GetPythonOSPluginPath () const;
73254721Semaste
74254721Semaste    void
75254721Semaste    SetPythonOSPluginPath (const FileSpec &file);
76254721Semaste
77254721Semaste    bool
78254721Semaste    GetIgnoreBreakpointsInExpressions () const;
79254721Semaste
80254721Semaste    void
81254721Semaste    SetIgnoreBreakpointsInExpressions (bool ignore);
82254721Semaste
83254721Semaste    bool
84254721Semaste    GetUnwindOnErrorInExpressions () const;
85254721Semaste
86254721Semaste    void
87254721Semaste    SetUnwindOnErrorInExpressions (bool ignore);
88254721Semaste
89254721Semaste    bool
90254721Semaste    GetStopOnSharedLibraryEvents () const;
91254721Semaste
92254721Semaste    void
93254721Semaste    SetStopOnSharedLibraryEvents (bool stop);
94254721Semaste
95254721Semaste    bool
96254721Semaste    GetDetachKeepsStopped () const;
97254721Semaste
98254721Semaste    void
99254721Semaste    SetDetachKeepsStopped (bool keep_stopped);
100254721Semaste};
101254721Semaste
102254721Semastetypedef std::shared_ptr<ProcessProperties> ProcessPropertiesSP;
103254721Semaste
104254721Semaste//----------------------------------------------------------------------
105254721Semaste// ProcessInfo
106254721Semaste//
107254721Semaste// A base class for information for a process. This can be used to fill
108254721Semaste// out information for a process prior to launching it, or it can be
109254721Semaste// used for an instance of a process and can be filled in with the
110254721Semaste// existing values for that process.
111254721Semaste//----------------------------------------------------------------------
112254721Semasteclass ProcessInfo
113254721Semaste{
114254721Semastepublic:
115254721Semaste    ProcessInfo () :
116254721Semaste        m_executable (),
117254721Semaste        m_arguments (),
118254721Semaste        m_environment (),
119254721Semaste        m_uid (UINT32_MAX),
120254721Semaste        m_gid (UINT32_MAX),
121254721Semaste        m_arch(),
122254721Semaste        m_pid (LLDB_INVALID_PROCESS_ID)
123254721Semaste    {
124254721Semaste    }
125254721Semaste
126254721Semaste    ProcessInfo (const char *name,
127254721Semaste                 const ArchSpec &arch,
128254721Semaste                 lldb::pid_t pid) :
129254721Semaste        m_executable (name, false),
130254721Semaste        m_arguments (),
131254721Semaste        m_environment(),
132254721Semaste        m_uid (UINT32_MAX),
133254721Semaste        m_gid (UINT32_MAX),
134254721Semaste        m_arch (arch),
135254721Semaste        m_pid (pid)
136254721Semaste    {
137254721Semaste    }
138254721Semaste
139254721Semaste    void
140254721Semaste    Clear ()
141254721Semaste    {
142254721Semaste        m_executable.Clear();
143254721Semaste        m_arguments.Clear();
144254721Semaste        m_environment.Clear();
145254721Semaste        m_uid = UINT32_MAX;
146254721Semaste        m_gid = UINT32_MAX;
147254721Semaste        m_arch.Clear();
148254721Semaste        m_pid = LLDB_INVALID_PROCESS_ID;
149254721Semaste    }
150254721Semaste
151254721Semaste    const char *
152254721Semaste    GetName() const
153254721Semaste    {
154254721Semaste        return m_executable.GetFilename().GetCString();
155254721Semaste    }
156254721Semaste
157254721Semaste    size_t
158254721Semaste    GetNameLength() const
159254721Semaste    {
160254721Semaste        return m_executable.GetFilename().GetLength();
161254721Semaste    }
162254721Semaste
163254721Semaste    FileSpec &
164254721Semaste    GetExecutableFile ()
165254721Semaste    {
166254721Semaste        return m_executable;
167254721Semaste    }
168254721Semaste
169254721Semaste    void
170254721Semaste    SetExecutableFile (const FileSpec &exe_file, bool add_exe_file_as_first_arg)
171254721Semaste    {
172254721Semaste        if (exe_file)
173254721Semaste        {
174254721Semaste            m_executable = exe_file;
175254721Semaste            if (add_exe_file_as_first_arg)
176254721Semaste            {
177254721Semaste                char filename[PATH_MAX];
178254721Semaste                if (exe_file.GetPath(filename, sizeof(filename)))
179254721Semaste                    m_arguments.InsertArgumentAtIndex (0, filename);
180254721Semaste            }
181254721Semaste        }
182254721Semaste        else
183254721Semaste        {
184254721Semaste            m_executable.Clear();
185254721Semaste        }
186254721Semaste    }
187254721Semaste
188254721Semaste    const FileSpec &
189254721Semaste    GetExecutableFile () const
190254721Semaste    {
191254721Semaste        return m_executable;
192254721Semaste    }
193254721Semaste
194254721Semaste    uint32_t
195254721Semaste    GetUserID() const
196254721Semaste    {
197254721Semaste        return m_uid;
198254721Semaste    }
199254721Semaste
200254721Semaste    uint32_t
201254721Semaste    GetGroupID() const
202254721Semaste    {
203254721Semaste        return m_gid;
204254721Semaste    }
205254721Semaste
206254721Semaste    bool
207254721Semaste    UserIDIsValid () const
208254721Semaste    {
209254721Semaste        return m_uid != UINT32_MAX;
210254721Semaste    }
211254721Semaste
212254721Semaste    bool
213254721Semaste    GroupIDIsValid () const
214254721Semaste    {
215254721Semaste        return m_gid != UINT32_MAX;
216254721Semaste    }
217254721Semaste
218254721Semaste    void
219254721Semaste    SetUserID (uint32_t uid)
220254721Semaste    {
221254721Semaste        m_uid = uid;
222254721Semaste    }
223254721Semaste
224254721Semaste    void
225254721Semaste    SetGroupID (uint32_t gid)
226254721Semaste    {
227254721Semaste        m_gid = gid;
228254721Semaste    }
229254721Semaste
230254721Semaste    ArchSpec &
231254721Semaste    GetArchitecture ()
232254721Semaste    {
233254721Semaste        return m_arch;
234254721Semaste    }
235254721Semaste
236254721Semaste    const ArchSpec &
237254721Semaste    GetArchitecture () const
238254721Semaste    {
239254721Semaste        return m_arch;
240254721Semaste    }
241254721Semaste
242254721Semaste    lldb::pid_t
243254721Semaste    GetProcessID () const
244254721Semaste    {
245254721Semaste        return m_pid;
246254721Semaste    }
247254721Semaste
248254721Semaste    void
249254721Semaste    SetProcessID (lldb::pid_t pid)
250254721Semaste    {
251254721Semaste        m_pid = pid;
252254721Semaste    }
253254721Semaste
254254721Semaste    bool
255254721Semaste    ProcessIDIsValid() const
256254721Semaste    {
257254721Semaste        return m_pid != LLDB_INVALID_PROCESS_ID;
258254721Semaste    }
259254721Semaste
260254721Semaste    void
261254721Semaste    Dump (Stream &s, Platform *platform) const;
262254721Semaste
263254721Semaste    Args &
264254721Semaste    GetArguments ()
265254721Semaste    {
266254721Semaste        return m_arguments;
267254721Semaste    }
268254721Semaste
269254721Semaste    const Args &
270254721Semaste    GetArguments () const
271254721Semaste    {
272254721Semaste        return m_arguments;
273254721Semaste    }
274254721Semaste
275254721Semaste    const char *
276254721Semaste    GetArg0 () const
277254721Semaste    {
278254721Semaste        if (m_arg0.empty())
279254721Semaste            return NULL;
280254721Semaste        return m_arg0.c_str();
281254721Semaste    }
282254721Semaste
283254721Semaste    void
284254721Semaste    SetArg0 (const char *arg)
285254721Semaste    {
286254721Semaste        if (arg && arg[0])
287254721Semaste            m_arg0 = arg;
288254721Semaste        else
289254721Semaste            m_arg0.clear();
290254721Semaste    }
291254721Semaste
292254721Semaste    void
293254721Semaste    SetArguments (const Args& args, bool first_arg_is_executable);
294254721Semaste
295254721Semaste    void
296254721Semaste    SetArguments (char const **argv, bool first_arg_is_executable);
297254721Semaste
298254721Semaste    Args &
299254721Semaste    GetEnvironmentEntries ()
300254721Semaste    {
301254721Semaste        return m_environment;
302254721Semaste    }
303254721Semaste
304254721Semaste    const Args &
305254721Semaste    GetEnvironmentEntries () const
306254721Semaste    {
307254721Semaste        return m_environment;
308254721Semaste    }
309254721Semaste
310254721Semasteprotected:
311254721Semaste    FileSpec m_executable;
312254721Semaste    std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
313254721Semaste                        // Not all process plug-ins support specifying an argv[0]
314254721Semaste                        // that differs from the resolved platform executable
315254721Semaste                        // (which is in m_executable)
316254721Semaste    Args m_arguments;   // All program arguments except argv[0]
317254721Semaste    Args m_environment;
318254721Semaste    uint32_t m_uid;
319254721Semaste    uint32_t m_gid;
320254721Semaste    ArchSpec m_arch;
321254721Semaste    lldb::pid_t m_pid;
322254721Semaste};
323254721Semaste
324254721Semaste//----------------------------------------------------------------------
325254721Semaste// ProcessInstanceInfo
326254721Semaste//
327254721Semaste// Describes an existing process and any discoverable information that
328254721Semaste// pertains to that process.
329254721Semaste//----------------------------------------------------------------------
330254721Semasteclass ProcessInstanceInfo : public ProcessInfo
331254721Semaste{
332254721Semastepublic:
333254721Semaste    ProcessInstanceInfo () :
334254721Semaste        ProcessInfo (),
335254721Semaste        m_euid (UINT32_MAX),
336254721Semaste        m_egid (UINT32_MAX),
337254721Semaste        m_parent_pid (LLDB_INVALID_PROCESS_ID)
338254721Semaste    {
339254721Semaste    }
340254721Semaste
341254721Semaste    ProcessInstanceInfo (const char *name,
342254721Semaste                 const ArchSpec &arch,
343254721Semaste                 lldb::pid_t pid) :
344254721Semaste        ProcessInfo (name, arch, pid),
345254721Semaste        m_euid (UINT32_MAX),
346254721Semaste        m_egid (UINT32_MAX),
347254721Semaste        m_parent_pid (LLDB_INVALID_PROCESS_ID)
348254721Semaste    {
349254721Semaste    }
350254721Semaste
351254721Semaste    void
352254721Semaste    Clear ()
353254721Semaste    {
354254721Semaste        ProcessInfo::Clear();
355254721Semaste        m_euid = UINT32_MAX;
356254721Semaste        m_egid = UINT32_MAX;
357254721Semaste        m_parent_pid = LLDB_INVALID_PROCESS_ID;
358254721Semaste    }
359254721Semaste
360254721Semaste    uint32_t
361254721Semaste    GetEffectiveUserID() const
362254721Semaste    {
363254721Semaste        return m_euid;
364254721Semaste    }
365254721Semaste
366254721Semaste    uint32_t
367254721Semaste    GetEffectiveGroupID() const
368254721Semaste    {
369254721Semaste        return m_egid;
370254721Semaste    }
371254721Semaste
372254721Semaste    bool
373254721Semaste    EffectiveUserIDIsValid () const
374254721Semaste    {
375254721Semaste        return m_euid != UINT32_MAX;
376254721Semaste    }
377254721Semaste
378254721Semaste    bool
379254721Semaste    EffectiveGroupIDIsValid () const
380254721Semaste    {
381254721Semaste        return m_egid != UINT32_MAX;
382254721Semaste    }
383254721Semaste
384254721Semaste    void
385254721Semaste    SetEffectiveUserID (uint32_t uid)
386254721Semaste    {
387254721Semaste        m_euid = uid;
388254721Semaste    }
389254721Semaste
390254721Semaste    void
391254721Semaste    SetEffectiveGroupID (uint32_t gid)
392254721Semaste    {
393254721Semaste        m_egid = gid;
394254721Semaste    }
395254721Semaste
396254721Semaste    lldb::pid_t
397254721Semaste    GetParentProcessID () const
398254721Semaste    {
399254721Semaste        return m_parent_pid;
400254721Semaste    }
401254721Semaste
402254721Semaste    void
403254721Semaste    SetParentProcessID (lldb::pid_t pid)
404254721Semaste    {
405254721Semaste        m_parent_pid = pid;
406254721Semaste    }
407254721Semaste
408254721Semaste    bool
409254721Semaste    ParentProcessIDIsValid() const
410254721Semaste    {
411254721Semaste        return m_parent_pid != LLDB_INVALID_PROCESS_ID;
412254721Semaste    }
413254721Semaste
414254721Semaste    void
415254721Semaste    Dump (Stream &s, Platform *platform) const;
416254721Semaste
417254721Semaste    static void
418254721Semaste    DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose);
419254721Semaste
420254721Semaste    void
421254721Semaste    DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const;
422254721Semaste
423254721Semasteprotected:
424254721Semaste    uint32_t m_euid;
425254721Semaste    uint32_t m_egid;
426254721Semaste    lldb::pid_t m_parent_pid;
427254721Semaste};
428254721Semaste
429254721Semaste
430254721Semaste//----------------------------------------------------------------------
431254721Semaste// ProcessLaunchInfo
432254721Semaste//
433254721Semaste// Describes any information that is required to launch a process.
434254721Semaste//----------------------------------------------------------------------
435254721Semaste
436254721Semasteclass ProcessLaunchInfo : public ProcessInfo
437254721Semaste{
438254721Semastepublic:
439254721Semaste
440254721Semaste    class FileAction
441254721Semaste    {
442254721Semaste    public:
443254721Semaste        enum Action
444254721Semaste        {
445254721Semaste            eFileActionNone,
446254721Semaste            eFileActionClose,
447254721Semaste            eFileActionDuplicate,
448254721Semaste            eFileActionOpen
449254721Semaste        };
450254721Semaste
451254721Semaste
452254721Semaste        FileAction () :
453254721Semaste            m_action (eFileActionNone),
454254721Semaste            m_fd (-1),
455254721Semaste            m_arg (-1),
456254721Semaste            m_path ()
457254721Semaste        {
458254721Semaste        }
459254721Semaste
460254721Semaste        void
461254721Semaste        Clear()
462254721Semaste        {
463254721Semaste            m_action = eFileActionNone;
464254721Semaste            m_fd = -1;
465254721Semaste            m_arg = -1;
466254721Semaste            m_path.clear();
467254721Semaste        }
468254721Semaste
469254721Semaste        bool
470254721Semaste        Close (int fd);
471254721Semaste
472254721Semaste        bool
473254721Semaste        Duplicate (int fd, int dup_fd);
474254721Semaste
475254721Semaste        bool
476254721Semaste        Open (int fd, const char *path, bool read, bool write);
477254721Semaste
478254721Semaste        static bool
479254721Semaste        AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions,
480254721Semaste                                 const FileAction *info,
481254721Semaste                                 Log *log,
482254721Semaste                                 Error& error);
483254721Semaste
484254721Semaste        int
485254721Semaste        GetFD () const
486254721Semaste        {
487254721Semaste            return m_fd;
488254721Semaste        }
489254721Semaste
490254721Semaste        Action
491254721Semaste        GetAction () const
492254721Semaste        {
493254721Semaste            return m_action;
494254721Semaste        }
495254721Semaste
496254721Semaste        int
497254721Semaste        GetActionArgument () const
498254721Semaste        {
499254721Semaste            return m_arg;
500254721Semaste        }
501254721Semaste
502254721Semaste        const char *
503254721Semaste        GetPath () const
504254721Semaste        {
505254721Semaste            if (m_path.empty())
506254721Semaste                return NULL;
507254721Semaste            return m_path.c_str();
508254721Semaste        }
509254721Semaste
510254721Semaste    protected:
511254721Semaste        Action m_action;    // The action for this file
512254721Semaste        int m_fd;           // An existing file descriptor
513254721Semaste        int m_arg;          // oflag for eFileActionOpen*, dup_fd for eFileActionDuplicate
514254721Semaste        std::string m_path; // A file path to use for opening after fork or posix_spawn
515254721Semaste    };
516254721Semaste
517254721Semaste    ProcessLaunchInfo () :
518254721Semaste        ProcessInfo(),
519254721Semaste        m_working_dir (),
520254721Semaste        m_plugin_name (),
521254721Semaste        m_shell (),
522254721Semaste        m_flags (0),
523254721Semaste        m_file_actions (),
524254721Semaste        m_pty (),
525254721Semaste        m_resume_count (0),
526254721Semaste        m_monitor_callback (NULL),
527254721Semaste        m_monitor_callback_baton (NULL),
528254721Semaste        m_monitor_signals (false)
529254721Semaste    {
530254721Semaste    }
531254721Semaste
532254721Semaste    ProcessLaunchInfo (const char *stdin_path,
533254721Semaste                       const char *stdout_path,
534254721Semaste                       const char *stderr_path,
535254721Semaste                       const char *working_directory,
536254721Semaste                       uint32_t launch_flags) :
537254721Semaste        ProcessInfo(),
538254721Semaste        m_working_dir (),
539254721Semaste        m_plugin_name (),
540254721Semaste        m_shell (),
541254721Semaste        m_flags (launch_flags),
542254721Semaste        m_file_actions (),
543254721Semaste        m_pty (),
544254721Semaste        m_resume_count (0),
545254721Semaste        m_monitor_callback (NULL),
546254721Semaste        m_monitor_callback_baton (NULL),
547254721Semaste        m_monitor_signals (false)
548254721Semaste    {
549254721Semaste        if (stdin_path)
550254721Semaste        {
551254721Semaste            ProcessLaunchInfo::FileAction file_action;
552254721Semaste            const bool read = true;
553254721Semaste            const bool write = false;
554254721Semaste            if (file_action.Open(STDIN_FILENO, stdin_path, read, write))
555254721Semaste                AppendFileAction (file_action);
556254721Semaste        }
557254721Semaste        if (stdout_path)
558254721Semaste        {
559254721Semaste            ProcessLaunchInfo::FileAction file_action;
560254721Semaste            const bool read = false;
561254721Semaste            const bool write = true;
562254721Semaste            if (file_action.Open(STDOUT_FILENO, stdout_path, read, write))
563254721Semaste                AppendFileAction (file_action);
564254721Semaste        }
565254721Semaste        if (stderr_path)
566254721Semaste        {
567254721Semaste            ProcessLaunchInfo::FileAction file_action;
568254721Semaste            const bool read = false;
569254721Semaste            const bool write = true;
570254721Semaste            if (file_action.Open(STDERR_FILENO, stderr_path, read, write))
571254721Semaste                AppendFileAction (file_action);
572254721Semaste        }
573254721Semaste        if (working_directory)
574254721Semaste            SetWorkingDirectory(working_directory);
575254721Semaste    }
576254721Semaste
577254721Semaste    void
578254721Semaste    AppendFileAction (const FileAction &info)
579254721Semaste    {
580254721Semaste        m_file_actions.push_back(info);
581254721Semaste    }
582254721Semaste
583254721Semaste    bool
584254721Semaste    AppendCloseFileAction (int fd)
585254721Semaste    {
586254721Semaste        FileAction file_action;
587254721Semaste        if (file_action.Close (fd))
588254721Semaste        {
589254721Semaste            AppendFileAction (file_action);
590254721Semaste            return true;
591254721Semaste        }
592254721Semaste        return false;
593254721Semaste    }
594254721Semaste
595254721Semaste    bool
596254721Semaste    AppendDuplicateFileAction (int fd, int dup_fd)
597254721Semaste    {
598254721Semaste        FileAction file_action;
599254721Semaste        if (file_action.Duplicate (fd, dup_fd))
600254721Semaste        {
601254721Semaste            AppendFileAction (file_action);
602254721Semaste            return true;
603254721Semaste        }
604254721Semaste        return false;
605254721Semaste    }
606254721Semaste
607254721Semaste    bool
608254721Semaste    AppendOpenFileAction (int fd, const char *path, bool read, bool write)
609254721Semaste    {
610254721Semaste        FileAction file_action;
611254721Semaste        if (file_action.Open (fd, path, read, write))
612254721Semaste        {
613254721Semaste            AppendFileAction (file_action);
614254721Semaste            return true;
615254721Semaste        }
616254721Semaste        return false;
617254721Semaste    }
618254721Semaste
619254721Semaste    bool
620254721Semaste    AppendSuppressFileAction (int fd, bool read, bool write)
621254721Semaste    {
622254721Semaste        FileAction file_action;
623254721Semaste        if (file_action.Open (fd, "/dev/null", read, write))
624254721Semaste        {
625254721Semaste            AppendFileAction (file_action);
626254721Semaste            return true;
627254721Semaste        }
628254721Semaste        return false;
629254721Semaste    }
630254721Semaste
631254721Semaste    void
632254721Semaste    FinalizeFileActions (Target *target,
633254721Semaste                         bool default_to_use_pty);
634254721Semaste
635254721Semaste    size_t
636254721Semaste    GetNumFileActions () const
637254721Semaste    {
638254721Semaste        return m_file_actions.size();
639254721Semaste    }
640254721Semaste
641254721Semaste    const FileAction *
642254721Semaste    GetFileActionAtIndex (size_t idx) const
643254721Semaste    {
644254721Semaste        if (idx < m_file_actions.size())
645254721Semaste            return &m_file_actions[idx];
646254721Semaste        return NULL;
647254721Semaste    }
648254721Semaste
649254721Semaste    const FileAction *
650254721Semaste    GetFileActionForFD (int fd) const
651254721Semaste    {
652254721Semaste        for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
653254721Semaste        {
654254721Semaste            if (m_file_actions[idx].GetFD () == fd)
655254721Semaste                return &m_file_actions[idx];
656254721Semaste        }
657254721Semaste        return NULL;
658254721Semaste    }
659254721Semaste
660254721Semaste    Flags &
661254721Semaste    GetFlags ()
662254721Semaste    {
663254721Semaste        return m_flags;
664254721Semaste    }
665254721Semaste
666254721Semaste    const Flags &
667254721Semaste    GetFlags () const
668254721Semaste    {
669254721Semaste        return m_flags;
670254721Semaste    }
671254721Semaste
672254721Semaste    const char *
673254721Semaste    GetWorkingDirectory () const
674254721Semaste    {
675254721Semaste        if (m_working_dir.empty())
676254721Semaste            return NULL;
677254721Semaste        return m_working_dir.c_str();
678254721Semaste    }
679254721Semaste
680254721Semaste    void
681254721Semaste    SetWorkingDirectory (const char *working_dir)
682254721Semaste    {
683254721Semaste        if (working_dir && working_dir[0])
684254721Semaste            m_working_dir.assign (working_dir);
685254721Semaste        else
686254721Semaste            m_working_dir.clear();
687254721Semaste    }
688254721Semaste
689254721Semaste    void
690254721Semaste    SwapWorkingDirectory (std::string &working_dir)
691254721Semaste    {
692254721Semaste        m_working_dir.swap (working_dir);
693254721Semaste    }
694254721Semaste
695254721Semaste
696254721Semaste    const char *
697254721Semaste    GetProcessPluginName () const
698254721Semaste    {
699254721Semaste        if (m_plugin_name.empty())
700254721Semaste            return NULL;
701254721Semaste        return m_plugin_name.c_str();
702254721Semaste    }
703254721Semaste
704254721Semaste    void
705254721Semaste    SetProcessPluginName (const char *plugin)
706254721Semaste    {
707254721Semaste        if (plugin && plugin[0])
708254721Semaste            m_plugin_name.assign (plugin);
709254721Semaste        else
710254721Semaste            m_plugin_name.clear();
711254721Semaste    }
712254721Semaste
713254721Semaste    const char *
714254721Semaste    GetShell () const
715254721Semaste    {
716254721Semaste        if (m_shell.empty())
717254721Semaste            return NULL;
718254721Semaste        return m_shell.c_str();
719254721Semaste    }
720254721Semaste
721254721Semaste    void
722254721Semaste    SetShell (const char * path)
723254721Semaste    {
724254721Semaste        if (path && path[0])
725254721Semaste        {
726254721Semaste            m_shell.assign (path);
727254721Semaste            m_flags.Set (lldb::eLaunchFlagLaunchInShell);
728254721Semaste        }
729254721Semaste        else
730254721Semaste        {
731254721Semaste            m_shell.clear();
732254721Semaste            m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
733254721Semaste        }
734254721Semaste    }
735254721Semaste
736254721Semaste    uint32_t
737254721Semaste    GetResumeCount () const
738254721Semaste    {
739254721Semaste        return m_resume_count;
740254721Semaste    }
741254721Semaste
742254721Semaste    void
743254721Semaste    SetResumeCount (uint32_t c)
744254721Semaste    {
745254721Semaste        m_resume_count = c;
746254721Semaste    }
747254721Semaste
748254721Semaste    bool
749254721Semaste    GetLaunchInSeparateProcessGroup ()
750254721Semaste    {
751254721Semaste        return m_flags.Test(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
752254721Semaste    }
753254721Semaste
754254721Semaste    void
755254721Semaste    SetLaunchInSeparateProcessGroup (bool separate)
756254721Semaste    {
757254721Semaste        if (separate)
758254721Semaste            m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
759254721Semaste        else
760254721Semaste            m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
761254721Semaste
762254721Semaste    }
763254721Semaste
764254721Semaste    void
765254721Semaste    Clear ()
766254721Semaste    {
767254721Semaste        ProcessInfo::Clear();
768254721Semaste        m_working_dir.clear();
769254721Semaste        m_plugin_name.clear();
770254721Semaste        m_shell.clear();
771254721Semaste        m_flags.Clear();
772254721Semaste        m_file_actions.clear();
773254721Semaste        m_resume_count = 0;
774254721Semaste    }
775254721Semaste
776254721Semaste    bool
777254721Semaste    ConvertArgumentsForLaunchingInShell (Error &error,
778254721Semaste                                         bool localhost,
779254721Semaste                                         bool will_debug,
780254721Semaste                                         bool first_arg_is_full_shell_command);
781254721Semaste
782254721Semaste    void
783254721Semaste    SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
784254721Semaste                               void *baton,
785254721Semaste                               bool monitor_signals)
786254721Semaste    {
787254721Semaste        m_monitor_callback = callback;
788254721Semaste        m_monitor_callback_baton = baton;
789254721Semaste        m_monitor_signals = monitor_signals;
790254721Semaste    }
791254721Semaste
792254721Semaste    bool
793254721Semaste    MonitorProcess () const
794254721Semaste    {
795254721Semaste        if (m_monitor_callback && ProcessIDIsValid())
796254721Semaste        {
797254721Semaste            Host::StartMonitoringChildProcess (m_monitor_callback,
798254721Semaste                                               m_monitor_callback_baton,
799254721Semaste                                               GetProcessID(),
800254721Semaste                                               m_monitor_signals);
801254721Semaste            return true;
802254721Semaste        }
803254721Semaste        return false;
804254721Semaste    }
805254721Semaste
806254721Semaste    lldb_utility::PseudoTerminal &
807254721Semaste    GetPTY ()
808254721Semaste    {
809254721Semaste        return m_pty;
810254721Semaste    }
811254721Semaste
812254721Semasteprotected:
813254721Semaste    std::string m_working_dir;
814254721Semaste    std::string m_plugin_name;
815254721Semaste    std::string m_shell;
816254721Semaste    Flags m_flags;       // Bitwise OR of bits from lldb::LaunchFlags
817254721Semaste    std::vector<FileAction> m_file_actions; // File actions for any other files
818254721Semaste    lldb_utility::PseudoTerminal m_pty;
819254721Semaste    uint32_t m_resume_count; // How many times do we resume after launching
820254721Semaste    Host::MonitorChildProcessCallback m_monitor_callback;
821254721Semaste    void *m_monitor_callback_baton;
822254721Semaste    bool m_monitor_signals;
823254721Semaste
824254721Semaste};
825254721Semaste
826254721Semaste//----------------------------------------------------------------------
827254721Semaste// ProcessLaunchInfo
828254721Semaste//
829254721Semaste// Describes any information that is required to launch a process.
830254721Semaste//----------------------------------------------------------------------
831254721Semaste
832254721Semasteclass ProcessAttachInfo : public ProcessInstanceInfo
833254721Semaste{
834254721Semastepublic:
835254721Semaste    ProcessAttachInfo() :
836254721Semaste        ProcessInstanceInfo(),
837254721Semaste        m_plugin_name (),
838254721Semaste        m_resume_count (0),
839254721Semaste        m_wait_for_launch (false),
840254721Semaste        m_ignore_existing (true),
841254721Semaste        m_continue_once_attached (false)
842254721Semaste    {
843254721Semaste    }
844254721Semaste
845254721Semaste    ProcessAttachInfo (const ProcessLaunchInfo &launch_info) :
846254721Semaste        ProcessInstanceInfo(),
847254721Semaste        m_plugin_name (),
848254721Semaste        m_resume_count (0),
849254721Semaste        m_wait_for_launch (false),
850254721Semaste        m_ignore_existing (true),
851254721Semaste        m_continue_once_attached (false)
852254721Semaste    {
853254721Semaste        ProcessInfo::operator= (launch_info);
854254721Semaste        SetProcessPluginName (launch_info.GetProcessPluginName());
855254721Semaste        SetResumeCount (launch_info.GetResumeCount());
856254721Semaste    }
857254721Semaste
858254721Semaste    bool
859254721Semaste    GetWaitForLaunch () const
860254721Semaste    {
861254721Semaste        return m_wait_for_launch;
862254721Semaste    }
863254721Semaste
864254721Semaste    void
865254721Semaste    SetWaitForLaunch (bool b)
866254721Semaste    {
867254721Semaste        m_wait_for_launch = b;
868254721Semaste    }
869254721Semaste
870254721Semaste    bool
871254721Semaste    GetIgnoreExisting () const
872254721Semaste    {
873254721Semaste        return m_ignore_existing;
874254721Semaste    }
875254721Semaste
876254721Semaste    void
877254721Semaste    SetIgnoreExisting (bool b)
878254721Semaste    {
879254721Semaste        m_ignore_existing = b;
880254721Semaste    }
881254721Semaste
882254721Semaste    bool
883254721Semaste    GetContinueOnceAttached () const
884254721Semaste    {
885254721Semaste        return m_continue_once_attached;
886254721Semaste    }
887254721Semaste
888254721Semaste    void
889254721Semaste    SetContinueOnceAttached (bool b)
890254721Semaste    {
891254721Semaste        m_continue_once_attached = b;
892254721Semaste    }
893254721Semaste
894254721Semaste    uint32_t
895254721Semaste    GetResumeCount () const
896254721Semaste    {
897254721Semaste        return m_resume_count;
898254721Semaste    }
899254721Semaste
900254721Semaste    void
901254721Semaste    SetResumeCount (uint32_t c)
902254721Semaste    {
903254721Semaste        m_resume_count = c;
904254721Semaste    }
905254721Semaste
906254721Semaste    const char *
907254721Semaste    GetProcessPluginName () const
908254721Semaste    {
909254721Semaste        if (m_plugin_name.empty())
910254721Semaste            return NULL;
911254721Semaste        return m_plugin_name.c_str();
912254721Semaste    }
913254721Semaste
914254721Semaste    void
915254721Semaste    SetProcessPluginName (const char *plugin)
916254721Semaste    {
917254721Semaste        if (plugin && plugin[0])
918254721Semaste            m_plugin_name.assign (plugin);
919254721Semaste        else
920254721Semaste            m_plugin_name.clear();
921254721Semaste    }
922254721Semaste
923254721Semaste    void
924254721Semaste    Clear ()
925254721Semaste    {
926254721Semaste        ProcessInstanceInfo::Clear();
927254721Semaste        m_plugin_name.clear();
928254721Semaste        m_resume_count = 0;
929254721Semaste        m_wait_for_launch = false;
930254721Semaste        m_ignore_existing = true;
931254721Semaste        m_continue_once_attached = false;
932254721Semaste    }
933254721Semaste
934254721Semaste    bool
935254721Semaste    ProcessInfoSpecified () const
936254721Semaste    {
937254721Semaste        if (GetExecutableFile())
938254721Semaste            return true;
939254721Semaste        if (GetProcessID() != LLDB_INVALID_PROCESS_ID)
940254721Semaste            return true;
941254721Semaste        if (GetParentProcessID() != LLDB_INVALID_PROCESS_ID)
942254721Semaste            return true;
943254721Semaste        return false;
944254721Semaste    }
945254721Semasteprotected:
946254721Semaste    std::string m_plugin_name;
947254721Semaste    uint32_t m_resume_count; // How many times do we resume after launching
948254721Semaste    bool m_wait_for_launch;
949254721Semaste    bool m_ignore_existing;
950254721Semaste    bool m_continue_once_attached; // Supports the use-case scenario of immediately continuing the process once attached.
951254721Semaste};
952254721Semaste
953254721Semasteclass ProcessLaunchCommandOptions : public Options
954254721Semaste{
955254721Semastepublic:
956254721Semaste
957254721Semaste    ProcessLaunchCommandOptions (CommandInterpreter &interpreter) :
958254721Semaste        Options(interpreter)
959254721Semaste    {
960254721Semaste        // Keep default values of all options in one place: OptionParsingStarting ()
961254721Semaste        OptionParsingStarting ();
962254721Semaste    }
963254721Semaste
964254721Semaste    ~ProcessLaunchCommandOptions ()
965254721Semaste    {
966254721Semaste    }
967254721Semaste
968254721Semaste    Error
969254721Semaste    SetOptionValue (uint32_t option_idx, const char *option_arg);
970254721Semaste
971254721Semaste    void
972254721Semaste    OptionParsingStarting ()
973254721Semaste    {
974254721Semaste        launch_info.Clear();
975254721Semaste    }
976254721Semaste
977254721Semaste    const OptionDefinition*
978254721Semaste    GetDefinitions ()
979254721Semaste    {
980254721Semaste        return g_option_table;
981254721Semaste    }
982254721Semaste
983254721Semaste    // Options table: Required for subclasses of Options.
984254721Semaste
985254721Semaste    static OptionDefinition g_option_table[];
986254721Semaste
987254721Semaste    // Instance variables to hold the values for command options.
988254721Semaste
989254721Semaste    ProcessLaunchInfo launch_info;
990254721Semaste};
991254721Semaste
992254721Semaste//----------------------------------------------------------------------
993254721Semaste// ProcessInstanceInfoMatch
994254721Semaste//
995254721Semaste// A class to help matching one ProcessInstanceInfo to another.
996254721Semaste//----------------------------------------------------------------------
997254721Semaste
998254721Semasteclass ProcessInstanceInfoMatch
999254721Semaste{
1000254721Semastepublic:
1001254721Semaste    ProcessInstanceInfoMatch () :
1002254721Semaste        m_match_info (),
1003254721Semaste        m_name_match_type (eNameMatchIgnore),
1004254721Semaste        m_match_all_users (false)
1005254721Semaste    {
1006254721Semaste    }
1007254721Semaste
1008254721Semaste    ProcessInstanceInfoMatch (const char *process_name,
1009254721Semaste                              NameMatchType process_name_match_type) :
1010254721Semaste        m_match_info (),
1011254721Semaste        m_name_match_type (process_name_match_type),
1012254721Semaste        m_match_all_users (false)
1013254721Semaste    {
1014254721Semaste        m_match_info.GetExecutableFile().SetFile(process_name, false);
1015254721Semaste    }
1016254721Semaste
1017254721Semaste    ProcessInstanceInfo &
1018254721Semaste    GetProcessInfo ()
1019254721Semaste    {
1020254721Semaste        return m_match_info;
1021254721Semaste    }
1022254721Semaste
1023254721Semaste    const ProcessInstanceInfo &
1024254721Semaste    GetProcessInfo () const
1025254721Semaste    {
1026254721Semaste        return m_match_info;
1027254721Semaste    }
1028254721Semaste
1029254721Semaste    bool
1030254721Semaste    GetMatchAllUsers () const
1031254721Semaste    {
1032254721Semaste        return m_match_all_users;
1033254721Semaste    }
1034254721Semaste
1035254721Semaste    void
1036254721Semaste    SetMatchAllUsers (bool b)
1037254721Semaste    {
1038254721Semaste        m_match_all_users = b;
1039254721Semaste    }
1040254721Semaste
1041254721Semaste    NameMatchType
1042254721Semaste    GetNameMatchType () const
1043254721Semaste    {
1044254721Semaste        return m_name_match_type;
1045254721Semaste    }
1046254721Semaste
1047254721Semaste    void
1048254721Semaste    SetNameMatchType (NameMatchType name_match_type)
1049254721Semaste    {
1050254721Semaste        m_name_match_type = name_match_type;
1051254721Semaste    }
1052254721Semaste
1053254721Semaste    bool
1054254721Semaste    NameMatches (const char *process_name) const;
1055254721Semaste
1056254721Semaste    bool
1057254721Semaste    Matches (const ProcessInstanceInfo &proc_info) const;
1058254721Semaste
1059254721Semaste    bool
1060254721Semaste    MatchAllProcesses () const;
1061254721Semaste    void
1062254721Semaste    Clear ();
1063254721Semaste
1064254721Semasteprotected:
1065254721Semaste    ProcessInstanceInfo m_match_info;
1066254721Semaste    NameMatchType m_name_match_type;
1067254721Semaste    bool m_match_all_users;
1068254721Semaste};
1069254721Semaste
1070254721Semasteclass ProcessInstanceInfoList
1071254721Semaste{
1072254721Semastepublic:
1073254721Semaste    ProcessInstanceInfoList () :
1074254721Semaste        m_infos()
1075254721Semaste    {
1076254721Semaste    }
1077254721Semaste
1078254721Semaste    void
1079254721Semaste    Clear()
1080254721Semaste    {
1081254721Semaste        m_infos.clear();
1082254721Semaste    }
1083254721Semaste
1084254721Semaste    size_t
1085254721Semaste    GetSize()
1086254721Semaste    {
1087254721Semaste        return m_infos.size();
1088254721Semaste    }
1089254721Semaste
1090254721Semaste    void
1091254721Semaste    Append (const ProcessInstanceInfo &info)
1092254721Semaste    {
1093254721Semaste        m_infos.push_back (info);
1094254721Semaste    }
1095254721Semaste
1096254721Semaste    const char *
1097254721Semaste    GetProcessNameAtIndex (size_t idx)
1098254721Semaste    {
1099254721Semaste        if (idx < m_infos.size())
1100254721Semaste            return m_infos[idx].GetName();
1101254721Semaste        return NULL;
1102254721Semaste    }
1103254721Semaste
1104254721Semaste    size_t
1105254721Semaste    GetProcessNameLengthAtIndex (size_t idx)
1106254721Semaste    {
1107254721Semaste        if (idx < m_infos.size())
1108254721Semaste            return m_infos[idx].GetNameLength();
1109254721Semaste        return 0;
1110254721Semaste    }
1111254721Semaste
1112254721Semaste    lldb::pid_t
1113254721Semaste    GetProcessIDAtIndex (size_t idx)
1114254721Semaste    {
1115254721Semaste        if (idx < m_infos.size())
1116254721Semaste            return m_infos[idx].GetProcessID();
1117254721Semaste        return 0;
1118254721Semaste    }
1119254721Semaste
1120254721Semaste    bool
1121254721Semaste    GetInfoAtIndex (size_t idx, ProcessInstanceInfo &info)
1122254721Semaste    {
1123254721Semaste        if (idx < m_infos.size())
1124254721Semaste        {
1125254721Semaste            info = m_infos[idx];
1126254721Semaste            return true;
1127254721Semaste        }
1128254721Semaste        return false;
1129254721Semaste    }
1130254721Semaste
1131254721Semaste    // You must ensure "idx" is valid before calling this function
1132254721Semaste    const ProcessInstanceInfo &
1133254721Semaste    GetProcessInfoAtIndex (size_t idx) const
1134254721Semaste    {
1135254721Semaste        assert (idx < m_infos.size());
1136254721Semaste        return m_infos[idx];
1137254721Semaste    }
1138254721Semaste
1139254721Semasteprotected:
1140254721Semaste    typedef std::vector<ProcessInstanceInfo> collection;
1141254721Semaste    collection m_infos;
1142254721Semaste};
1143254721Semaste
1144254721Semaste
1145254721Semaste// This class tracks the Modification state of the process.  Things that can currently modify
1146254721Semaste// the program are running the program (which will up the StopID) and writing memory (which
1147254721Semaste// will up the MemoryID.)
1148254721Semaste// FIXME: Should we also include modification of register states?
1149254721Semaste
1150254721Semasteclass ProcessModID
1151254721Semaste{
1152254721Semastefriend bool operator== (const ProcessModID &lhs, const ProcessModID &rhs);
1153254721Semastepublic:
1154254721Semaste    ProcessModID () :
1155254721Semaste        m_stop_id (0),
1156254721Semaste        m_last_natural_stop_id(0),
1157254721Semaste        m_resume_id (0),
1158254721Semaste        m_memory_id (0),
1159254721Semaste        m_last_user_expression_resume (0),
1160254721Semaste        m_running_user_expression (false)
1161254721Semaste    {}
1162254721Semaste
1163254721Semaste    ProcessModID (const ProcessModID &rhs) :
1164254721Semaste        m_stop_id (rhs.m_stop_id),
1165254721Semaste        m_memory_id (rhs.m_memory_id)
1166254721Semaste    {}
1167254721Semaste
1168254721Semaste    const ProcessModID & operator= (const ProcessModID &rhs)
1169254721Semaste    {
1170254721Semaste        if (this != &rhs)
1171254721Semaste        {
1172254721Semaste            m_stop_id = rhs.m_stop_id;
1173254721Semaste            m_memory_id = rhs.m_memory_id;
1174254721Semaste        }
1175254721Semaste        return *this;
1176254721Semaste    }
1177254721Semaste
1178254721Semaste    ~ProcessModID () {}
1179254721Semaste
1180254721Semaste    void BumpStopID () {
1181254721Semaste        m_stop_id++;
1182254721Semaste        if (!IsLastResumeForUserExpression())
1183254721Semaste            m_last_natural_stop_id++;
1184254721Semaste    }
1185254721Semaste
1186254721Semaste    void BumpMemoryID () { m_memory_id++; }
1187254721Semaste
1188254721Semaste    void BumpResumeID () {
1189254721Semaste        m_resume_id++;
1190254721Semaste        if (m_running_user_expression > 0)
1191254721Semaste            m_last_user_expression_resume = m_resume_id;
1192254721Semaste    }
1193254721Semaste
1194254721Semaste    uint32_t GetStopID() const { return m_stop_id; }
1195254721Semaste    uint32_t GetLastNaturalStopID() const { return m_last_natural_stop_id; }
1196254721Semaste    uint32_t GetMemoryID () const { return m_memory_id; }
1197254721Semaste    uint32_t GetResumeID () const { return m_resume_id; }
1198254721Semaste    uint32_t GetLastUserExpressionResumeID () const { return m_last_user_expression_resume; }
1199254721Semaste
1200254721Semaste    bool MemoryIDEqual (const ProcessModID &compare) const
1201254721Semaste    {
1202254721Semaste        return m_memory_id == compare.m_memory_id;
1203254721Semaste    }
1204254721Semaste
1205254721Semaste    bool StopIDEqual (const ProcessModID &compare) const
1206254721Semaste    {
1207254721Semaste        return m_stop_id == compare.m_stop_id;
1208254721Semaste    }
1209254721Semaste
1210254721Semaste    void SetInvalid ()
1211254721Semaste    {
1212254721Semaste        m_stop_id = UINT32_MAX;
1213254721Semaste    }
1214254721Semaste
1215254721Semaste    bool IsValid () const
1216254721Semaste    {
1217254721Semaste        return m_stop_id != UINT32_MAX;
1218254721Semaste    }
1219254721Semaste
1220254721Semaste    bool
1221254721Semaste    IsLastResumeForUserExpression () const
1222254721Semaste    {
1223254721Semaste        return m_resume_id == m_last_user_expression_resume;
1224254721Semaste    }
1225254721Semaste
1226254721Semaste    void
1227254721Semaste    SetRunningUserExpression (bool on)
1228254721Semaste    {
1229254721Semaste        // REMOVEME printf ("Setting running user expression %s at resume id %d - value: %d.\n", on ? "on" : "off", m_resume_id, m_running_user_expression);
1230254721Semaste        if (on)
1231254721Semaste            m_running_user_expression++;
1232254721Semaste        else
1233254721Semaste            m_running_user_expression--;
1234254721Semaste    }
1235254721Semaste
1236254721Semasteprivate:
1237254721Semaste    uint32_t m_stop_id;
1238254721Semaste    uint32_t m_last_natural_stop_id;
1239254721Semaste    uint32_t m_resume_id;
1240254721Semaste    uint32_t m_memory_id;
1241254721Semaste    uint32_t m_last_user_expression_resume;
1242254721Semaste    uint32_t m_running_user_expression;
1243254721Semaste};
1244254721Semasteinline bool operator== (const ProcessModID &lhs, const ProcessModID &rhs)
1245254721Semaste{
1246254721Semaste    if (lhs.StopIDEqual (rhs)
1247254721Semaste        && lhs.MemoryIDEqual (rhs))
1248254721Semaste        return true;
1249254721Semaste    else
1250254721Semaste        return false;
1251254721Semaste}
1252254721Semaste
1253254721Semasteinline bool operator!= (const ProcessModID &lhs, const ProcessModID &rhs)
1254254721Semaste{
1255254721Semaste    if (!lhs.StopIDEqual (rhs)
1256254721Semaste        || !lhs.MemoryIDEqual (rhs))
1257254721Semaste        return true;
1258254721Semaste    else
1259254721Semaste        return false;
1260254721Semaste}
1261254721Semaste
1262254721Semasteclass MemoryRegionInfo
1263254721Semaste{
1264254721Semastepublic:
1265254721Semaste    typedef Range<lldb::addr_t, lldb::addr_t> RangeType;
1266254721Semaste
1267254721Semaste    enum OptionalBool {
1268254721Semaste        eDontKnow  = -1,
1269254721Semaste        eNo         = 0,
1270254721Semaste        eYes        = 1
1271254721Semaste    };
1272254721Semaste
1273254721Semaste    MemoryRegionInfo () :
1274254721Semaste        m_range (),
1275254721Semaste        m_read (eDontKnow),
1276254721Semaste        m_write (eDontKnow),
1277254721Semaste        m_execute (eDontKnow)
1278254721Semaste    {
1279254721Semaste    }
1280254721Semaste
1281254721Semaste    ~MemoryRegionInfo ()
1282254721Semaste    {
1283254721Semaste    }
1284254721Semaste
1285254721Semaste    RangeType &
1286254721Semaste    GetRange()
1287254721Semaste    {
1288254721Semaste        return m_range;
1289254721Semaste    }
1290254721Semaste
1291254721Semaste    void
1292254721Semaste    Clear()
1293254721Semaste    {
1294254721Semaste        m_range.Clear();
1295254721Semaste        m_read = m_write = m_execute = eDontKnow;
1296254721Semaste    }
1297254721Semaste
1298254721Semaste    const RangeType &
1299254721Semaste    GetRange() const
1300254721Semaste    {
1301254721Semaste        return m_range;
1302254721Semaste    }
1303254721Semaste
1304254721Semaste    OptionalBool
1305254721Semaste    GetReadable () const
1306254721Semaste    {
1307254721Semaste        return m_read;
1308254721Semaste    }
1309254721Semaste
1310254721Semaste    OptionalBool
1311254721Semaste    GetWritable () const
1312254721Semaste    {
1313254721Semaste        return m_write;
1314254721Semaste    }
1315254721Semaste
1316254721Semaste    OptionalBool
1317254721Semaste    GetExecutable () const
1318254721Semaste    {
1319254721Semaste        return m_execute;
1320254721Semaste    }
1321254721Semaste
1322254721Semaste    void
1323254721Semaste    SetReadable (OptionalBool val)
1324254721Semaste    {
1325254721Semaste        m_read = val;
1326254721Semaste    }
1327254721Semaste
1328254721Semaste    void
1329254721Semaste    SetWritable (OptionalBool val)
1330254721Semaste    {
1331254721Semaste        m_write = val;
1332254721Semaste    }
1333254721Semaste
1334254721Semaste    void
1335254721Semaste    SetExecutable (OptionalBool val)
1336254721Semaste    {
1337254721Semaste        m_execute = val;
1338254721Semaste    }
1339254721Semaste
1340254721Semasteprotected:
1341254721Semaste    RangeType m_range;
1342254721Semaste    OptionalBool m_read;
1343254721Semaste    OptionalBool m_write;
1344254721Semaste    OptionalBool m_execute;
1345254721Semaste};
1346254721Semaste
1347254721Semaste//----------------------------------------------------------------------
1348254721Semaste/// @class Process Process.h "lldb/Target/Process.h"
1349254721Semaste/// @brief A plug-in interface definition class for debugging a process.
1350254721Semaste//----------------------------------------------------------------------
1351254721Semasteclass Process :
1352254721Semaste    public std::enable_shared_from_this<Process>,
1353254721Semaste    public ProcessProperties,
1354254721Semaste    public UserID,
1355254721Semaste    public Broadcaster,
1356254721Semaste    public ExecutionContextScope,
1357254721Semaste    public PluginInterface
1358254721Semaste{
1359254721Semastefriend class ThreadList;
1360254721Semastefriend class ClangFunction; // For WaitForStateChangeEventsPrivate
1361254721Semastefriend class CommandObjectProcessLaunch;
1362254721Semastefriend class ProcessEventData;
1363254721Semastefriend class CommandObjectBreakpointCommand;
1364254721Semastefriend class StopInfo;
1365254721Semaste
1366254721Semastepublic:
1367254721Semaste
1368254721Semaste    //------------------------------------------------------------------
1369254721Semaste    /// Broadcaster event bits definitions.
1370254721Semaste    //------------------------------------------------------------------
1371254721Semaste    enum
1372254721Semaste    {
1373254721Semaste        eBroadcastBitStateChanged   = (1 << 0),
1374254721Semaste        eBroadcastBitInterrupt      = (1 << 1),
1375254721Semaste        eBroadcastBitSTDOUT         = (1 << 2),
1376254721Semaste        eBroadcastBitSTDERR         = (1 << 3),
1377254721Semaste        eBroadcastBitProfileData    = (1 << 4)
1378254721Semaste    };
1379254721Semaste
1380254721Semaste    enum
1381254721Semaste    {
1382254721Semaste        eBroadcastInternalStateControlStop = (1<<0),
1383254721Semaste        eBroadcastInternalStateControlPause = (1<<1),
1384254721Semaste        eBroadcastInternalStateControlResume = (1<<2)
1385254721Semaste    };
1386254721Semaste
1387254721Semaste    typedef Range<lldb::addr_t, lldb::addr_t> LoadRange;
1388254721Semaste    // We use a read/write lock to allow on or more clients to
1389254721Semaste    // access the process state while the process is stopped (reader).
1390254721Semaste    // We lock the write lock to control access to the process
1391254721Semaste    // while it is running (readers, or clients that want the process
1392254721Semaste    // stopped can block waiting for the process to stop, or just
1393254721Semaste    // try to lock it to see if they can immediately access the stopped
1394254721Semaste    // process. If the try read lock fails, then the process is running.
1395254721Semaste    typedef ProcessRunLock::ProcessRunLocker StopLocker;
1396254721Semaste
1397254721Semaste    // These two functions fill out the Broadcaster interface:
1398254721Semaste
1399254721Semaste    static ConstString &GetStaticBroadcasterClass ();
1400254721Semaste
1401254721Semaste    virtual ConstString &GetBroadcasterClass() const
1402254721Semaste    {
1403254721Semaste        return GetStaticBroadcasterClass();
1404254721Semaste    }
1405254721Semaste
1406254721Semaste
1407254721Semaste    //------------------------------------------------------------------
1408254721Semaste    /// A notification structure that can be used by clients to listen
1409254721Semaste    /// for changes in a process's lifetime.
1410254721Semaste    ///
1411254721Semaste    /// @see RegisterNotificationCallbacks (const Notifications&)
1412254721Semaste    /// @see UnregisterNotificationCallbacks (const Notifications&)
1413254721Semaste    //------------------------------------------------------------------
1414254721Semaste#ifndef SWIG
1415254721Semaste    typedef struct
1416254721Semaste    {
1417254721Semaste        void *baton;
1418254721Semaste        void (*initialize)(void *baton, Process *process);
1419254721Semaste        void (*process_state_changed) (void *baton, Process *process, lldb::StateType state);
1420254721Semaste    } Notifications;
1421254721Semaste
1422254721Semaste    class ProcessEventData :
1423254721Semaste        public EventData
1424254721Semaste    {
1425254721Semaste        friend class Process;
1426254721Semaste
1427254721Semaste        public:
1428254721Semaste            ProcessEventData ();
1429254721Semaste            ProcessEventData (const lldb::ProcessSP &process, lldb::StateType state);
1430254721Semaste
1431254721Semaste            virtual ~ProcessEventData();
1432254721Semaste
1433254721Semaste            static const ConstString &
1434254721Semaste            GetFlavorString ();
1435254721Semaste
1436254721Semaste            virtual const ConstString &
1437254721Semaste            GetFlavor () const;
1438254721Semaste
1439254721Semaste            const lldb::ProcessSP &
1440254721Semaste            GetProcessSP() const
1441254721Semaste            {
1442254721Semaste                return m_process_sp;
1443254721Semaste            }
1444254721Semaste            lldb::StateType
1445254721Semaste            GetState() const
1446254721Semaste            {
1447254721Semaste                return m_state;
1448254721Semaste            }
1449254721Semaste            bool
1450254721Semaste            GetRestarted () const
1451254721Semaste            {
1452254721Semaste                return m_restarted;
1453254721Semaste            }
1454254721Semaste
1455254721Semaste            size_t
1456254721Semaste            GetNumRestartedReasons ()
1457254721Semaste            {
1458254721Semaste                return m_restarted_reasons.size();
1459254721Semaste            }
1460254721Semaste
1461254721Semaste            const char *
1462254721Semaste            GetRestartedReasonAtIndex(size_t idx)
1463254721Semaste            {
1464254721Semaste                if (idx > m_restarted_reasons.size())
1465254721Semaste                    return NULL;
1466254721Semaste                else
1467254721Semaste                    return m_restarted_reasons[idx].c_str();
1468254721Semaste            }
1469254721Semaste
1470254721Semaste            bool
1471254721Semaste            GetInterrupted () const
1472254721Semaste            {
1473254721Semaste                return m_interrupted;
1474254721Semaste            }
1475254721Semaste
1476254721Semaste            virtual void
1477254721Semaste            Dump (Stream *s) const;
1478254721Semaste
1479254721Semaste            virtual void
1480254721Semaste            DoOnRemoval (Event *event_ptr);
1481254721Semaste
1482254721Semaste            static const Process::ProcessEventData *
1483254721Semaste            GetEventDataFromEvent (const Event *event_ptr);
1484254721Semaste
1485254721Semaste            static lldb::ProcessSP
1486254721Semaste            GetProcessFromEvent (const Event *event_ptr);
1487254721Semaste
1488254721Semaste            static lldb::StateType
1489254721Semaste            GetStateFromEvent (const Event *event_ptr);
1490254721Semaste
1491254721Semaste            static bool
1492254721Semaste            GetRestartedFromEvent (const Event *event_ptr);
1493254721Semaste
1494254721Semaste            static size_t
1495254721Semaste            GetNumRestartedReasons(const Event *event_ptr);
1496254721Semaste
1497254721Semaste            static const char *
1498254721Semaste            GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx);
1499254721Semaste
1500254721Semaste            static void
1501254721Semaste            AddRestartedReason (Event *event_ptr, const char *reason);
1502254721Semaste
1503254721Semaste            static void
1504254721Semaste            SetRestartedInEvent (Event *event_ptr, bool new_value);
1505254721Semaste
1506254721Semaste            static bool
1507254721Semaste            GetInterruptedFromEvent (const Event *event_ptr);
1508254721Semaste
1509254721Semaste            static void
1510254721Semaste            SetInterruptedInEvent (Event *event_ptr, bool new_value);
1511254721Semaste
1512254721Semaste            static bool
1513254721Semaste            SetUpdateStateOnRemoval (Event *event_ptr);
1514254721Semaste
1515254721Semaste       private:
1516254721Semaste
1517254721Semaste            void
1518254721Semaste            SetUpdateStateOnRemoval()
1519254721Semaste            {
1520254721Semaste                m_update_state++;
1521254721Semaste            }
1522254721Semaste            void
1523254721Semaste            SetRestarted (bool new_value)
1524254721Semaste            {
1525254721Semaste                m_restarted = new_value;
1526254721Semaste            }
1527254721Semaste            void
1528254721Semaste            SetInterrupted (bool new_value)
1529254721Semaste            {
1530254721Semaste                m_interrupted = new_value;
1531254721Semaste            }
1532254721Semaste            void
1533254721Semaste            AddRestartedReason (const char *reason)
1534254721Semaste            {
1535254721Semaste                m_restarted_reasons.push_back(reason);
1536254721Semaste            }
1537254721Semaste
1538254721Semaste            lldb::ProcessSP m_process_sp;
1539254721Semaste            lldb::StateType m_state;
1540254721Semaste            std::vector<std::string> m_restarted_reasons;
1541254721Semaste            bool m_restarted;  // For "eStateStopped" events, this is true if the target was automatically restarted.
1542254721Semaste            int m_update_state;
1543254721Semaste            bool m_interrupted;
1544254721Semaste            DISALLOW_COPY_AND_ASSIGN (ProcessEventData);
1545254721Semaste
1546254721Semaste    };
1547254721Semaste
1548254721Semaste#endif
1549254721Semaste
1550254721Semaste    static void
1551254721Semaste    SettingsInitialize ();
1552254721Semaste
1553254721Semaste    static void
1554254721Semaste    SettingsTerminate ();
1555254721Semaste
1556254721Semaste    static const ProcessPropertiesSP &
1557254721Semaste    GetGlobalProperties();
1558254721Semaste
1559254721Semaste    //------------------------------------------------------------------
1560254721Semaste    /// Construct with a shared pointer to a target, and the Process listener.
1561254721Semaste    //------------------------------------------------------------------
1562254721Semaste    Process(Target &target, Listener &listener);
1563254721Semaste
1564254721Semaste    //------------------------------------------------------------------
1565254721Semaste    /// Destructor.
1566254721Semaste    ///
1567254721Semaste    /// The destructor is virtual since this class is designed to be
1568254721Semaste    /// inherited from by the plug-in instance.
1569254721Semaste    //------------------------------------------------------------------
1570254721Semaste    virtual
1571254721Semaste    ~Process();
1572254721Semaste
1573254721Semaste    //------------------------------------------------------------------
1574254721Semaste    /// Find a Process plug-in that can debug \a module using the
1575254721Semaste    /// currently selected architecture.
1576254721Semaste    ///
1577254721Semaste    /// Scans all loaded plug-in interfaces that implement versions of
1578254721Semaste    /// the Process plug-in interface and returns the first instance
1579254721Semaste    /// that can debug the file.
1580254721Semaste    ///
1581254721Semaste    /// @param[in] module_sp
1582254721Semaste    ///     The module shared pointer that this process will debug.
1583254721Semaste    ///
1584254721Semaste    /// @param[in] plugin_name
1585254721Semaste    ///     If NULL, select the best plug-in for the binary. If non-NULL
1586254721Semaste    ///     then look for a plugin whose PluginInfo's name matches
1587254721Semaste    ///     this string.
1588254721Semaste    ///
1589254721Semaste    /// @see Process::CanDebug ()
1590254721Semaste    //------------------------------------------------------------------
1591254721Semaste    static lldb::ProcessSP
1592254721Semaste    FindPlugin (Target &target,
1593254721Semaste                const char *plugin_name,
1594254721Semaste                Listener &listener,
1595254721Semaste                const FileSpec *crash_file_path);
1596254721Semaste
1597254721Semaste
1598254721Semaste
1599254721Semaste    //------------------------------------------------------------------
1600254721Semaste    /// Static function that can be used with the \b host function
1601254721Semaste    /// Host::StartMonitoringChildProcess ().
1602254721Semaste    ///
1603254721Semaste    /// This function can be used by lldb_private::Process subclasses
1604254721Semaste    /// when they want to watch for a local process and have its exit
1605254721Semaste    /// status automatically set when the host child process exits.
1606254721Semaste    /// Subclasses should call Host::StartMonitoringChildProcess ()
1607254721Semaste    /// with:
1608254721Semaste    ///     callback = Process::SetHostProcessExitStatus
1609254721Semaste    ///     callback_baton = NULL
1610254721Semaste    ///     pid = Process::GetID()
1611254721Semaste    ///     monitor_signals = false
1612254721Semaste    //------------------------------------------------------------------
1613254721Semaste    static bool
1614254721Semaste    SetProcessExitStatus (void *callback_baton,   // The callback baton which should be set to NULL
1615254721Semaste                          lldb::pid_t pid,        // The process ID we want to monitor
1616254721Semaste                          bool exited,
1617254721Semaste                          int signo,              // Zero for no signal
1618254721Semaste                          int status);            // Exit value of process if signal is zero
1619254721Semaste
1620254721Semaste    lldb::ByteOrder
1621254721Semaste    GetByteOrder () const;
1622254721Semaste
1623254721Semaste    uint32_t
1624254721Semaste    GetAddressByteSize () const;
1625254721Semaste
1626254721Semaste    uint32_t
1627254721Semaste    GetUniqueID() const
1628254721Semaste    {
1629254721Semaste        return m_process_unique_id;
1630254721Semaste    }
1631254721Semaste    //------------------------------------------------------------------
1632254721Semaste    /// Check if a plug-in instance can debug the file in \a module.
1633254721Semaste    ///
1634254721Semaste    /// Each plug-in is given a chance to say whether it can debug
1635254721Semaste    /// the file in \a module. If the Process plug-in instance can
1636254721Semaste    /// debug a file on the current system, it should return \b true.
1637254721Semaste    ///
1638254721Semaste    /// @return
1639254721Semaste    ///     Returns \b true if this Process plug-in instance can
1640254721Semaste    ///     debug the executable, \b false otherwise.
1641254721Semaste    //------------------------------------------------------------------
1642254721Semaste    virtual bool
1643254721Semaste    CanDebug (Target &target,
1644254721Semaste              bool plugin_specified_by_name) = 0;
1645254721Semaste
1646254721Semaste
1647254721Semaste    //------------------------------------------------------------------
1648254721Semaste    /// This object is about to be destroyed, do any necessary cleanup.
1649254721Semaste    ///
1650254721Semaste    /// Subclasses that override this method should always call this
1651254721Semaste    /// superclass method.
1652254721Semaste    //------------------------------------------------------------------
1653254721Semaste    virtual void
1654254721Semaste    Finalize();
1655254721Semaste
1656254721Semaste
1657254721Semaste    //------------------------------------------------------------------
1658254721Semaste    /// Return whether this object is valid (i.e. has not been finalized.)
1659254721Semaste    ///
1660254721Semaste    /// @return
1661254721Semaste    ///     Returns \b true if this Process has not been finalized
1662254721Semaste    ///     and \b false otherwise.
1663254721Semaste    //------------------------------------------------------------------
1664254721Semaste    bool
1665254721Semaste    IsValid() const
1666254721Semaste    {
1667254721Semaste        return !m_finalize_called;
1668254721Semaste    }
1669254721Semaste
1670254721Semaste    //------------------------------------------------------------------
1671254721Semaste    /// Return a multi-word command object that can be used to expose
1672254721Semaste    /// plug-in specific commands.
1673254721Semaste    ///
1674254721Semaste    /// This object will be used to resolve plug-in commands and can be
1675254721Semaste    /// triggered by a call to:
1676254721Semaste    ///
1677254721Semaste    ///     (lldb) process commmand <args>
1678254721Semaste    ///
1679254721Semaste    /// @return
1680254721Semaste    ///     A CommandObject which can be one of the concrete subclasses
1681254721Semaste    ///     of CommandObject like CommandObjectRaw, CommandObjectParsed,
1682254721Semaste    ///     or CommandObjectMultiword.
1683254721Semaste    //------------------------------------------------------------------
1684254721Semaste    virtual CommandObject *
1685254721Semaste    GetPluginCommandObject()
1686254721Semaste    {
1687254721Semaste        return NULL;
1688254721Semaste    }
1689254721Semaste
1690254721Semaste    //------------------------------------------------------------------
1691254721Semaste    /// Launch a new process.
1692254721Semaste    ///
1693254721Semaste    /// Launch a new process by spawning a new process using the
1694254721Semaste    /// target object's executable module's file as the file to launch.
1695254721Semaste    /// Arguments are given in \a argv, and the environment variables
1696254721Semaste    /// are in \a envp. Standard input and output files can be
1697254721Semaste    /// optionally re-directed to \a stdin_path, \a stdout_path, and
1698254721Semaste    /// \a stderr_path.
1699254721Semaste    ///
1700254721Semaste    /// This function is not meant to be overridden by Process
1701254721Semaste    /// subclasses. It will first call Process::WillLaunch (Module *)
1702254721Semaste    /// and if that returns \b true, Process::DoLaunch (Module*,
1703254721Semaste    /// char const *[],char const *[],const char *,const char *,
1704254721Semaste    /// const char *) will be called to actually do the launching. If
1705254721Semaste    /// DoLaunch returns \b true, then Process::DidLaunch() will be
1706254721Semaste    /// called.
1707254721Semaste    ///
1708254721Semaste    /// @param[in] argv
1709254721Semaste    ///     The argument array.
1710254721Semaste    ///
1711254721Semaste    /// @param[in] envp
1712254721Semaste    ///     The environment array.
1713254721Semaste    ///
1714254721Semaste    /// @param[in] launch_flags
1715254721Semaste    ///     Flags to modify the launch (@see lldb::LaunchFlags)
1716254721Semaste    ///
1717254721Semaste    /// @param[in] stdin_path
1718254721Semaste    ///     The path to use when re-directing the STDIN of the new
1719254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
1720254721Semaste    ///     terminal will be used.
1721254721Semaste    ///
1722254721Semaste    /// @param[in] stdout_path
1723254721Semaste    ///     The path to use when re-directing the STDOUT of the new
1724254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
1725254721Semaste    ///     terminal will be used.
1726254721Semaste    ///
1727254721Semaste    /// @param[in] stderr_path
1728254721Semaste    ///     The path to use when re-directing the STDERR of the new
1729254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
1730254721Semaste    ///     terminal will be used.
1731254721Semaste    ///
1732254721Semaste    /// @param[in] working_directory
1733254721Semaste    ///     The working directory to have the child process run in
1734254721Semaste    ///
1735254721Semaste    /// @return
1736254721Semaste    ///     An error object. Call GetID() to get the process ID if
1737254721Semaste    ///     the error object is success.
1738254721Semaste    //------------------------------------------------------------------
1739254721Semaste    virtual Error
1740254721Semaste    Launch (const ProcessLaunchInfo &launch_info);
1741254721Semaste
1742254721Semaste    virtual Error
1743254721Semaste    LoadCore ();
1744254721Semaste
1745254721Semaste    virtual Error
1746254721Semaste    DoLoadCore ()
1747254721Semaste    {
1748254721Semaste        Error error;
1749254721Semaste        error.SetErrorStringWithFormat("error: %s does not support loading core files.", GetPluginName().GetCString());
1750254721Semaste        return error;
1751254721Semaste    }
1752254721Semaste
1753254721Semaste    //------------------------------------------------------------------
1754254721Semaste    /// Get the dynamic loader plug-in for this process.
1755254721Semaste    ///
1756254721Semaste    /// The default action is to let the DynamicLoader plug-ins check
1757254721Semaste    /// the main executable and the DynamicLoader will select itself
1758254721Semaste    /// automatically. Subclasses can override this if inspecting the
1759254721Semaste    /// executable is not desired, or if Process subclasses can only
1760254721Semaste    /// use a specific DynamicLoader plug-in.
1761254721Semaste    //------------------------------------------------------------------
1762254721Semaste    virtual DynamicLoader *
1763254721Semaste    GetDynamicLoader ();
1764254721Semaste
1765254721Semaste    //------------------------------------------------------------------
1766254721Semaste    /// Attach to an existing process using the process attach info.
1767254721Semaste    ///
1768254721Semaste    /// This function is not meant to be overridden by Process
1769254721Semaste    /// subclasses. It will first call WillAttach (lldb::pid_t)
1770254721Semaste    /// or WillAttach (const char *), and if that returns \b
1771254721Semaste    /// true, DoAttach (lldb::pid_t) or DoAttach (const char *) will
1772254721Semaste    /// be called to actually do the attach. If DoAttach returns \b
1773254721Semaste    /// true, then Process::DidAttach() will be called.
1774254721Semaste    ///
1775254721Semaste    /// @param[in] pid
1776254721Semaste    ///     The process ID that we should attempt to attach to.
1777254721Semaste    ///
1778254721Semaste    /// @return
1779254721Semaste    ///     Returns \a pid if attaching was successful, or
1780254721Semaste    ///     LLDB_INVALID_PROCESS_ID if attaching fails.
1781254721Semaste    //------------------------------------------------------------------
1782254721Semaste    virtual Error
1783254721Semaste    Attach (ProcessAttachInfo &attach_info);
1784254721Semaste
1785254721Semaste    //------------------------------------------------------------------
1786254721Semaste    /// Attach to a remote system via a URL
1787254721Semaste    ///
1788254721Semaste    /// @param[in] strm
1789254721Semaste    ///     A stream where output intended for the user
1790254721Semaste    ///     (if the driver has a way to display that) generated during
1791254721Semaste    ///     the connection.  This may be NULL if no output is needed.A
1792254721Semaste    ///
1793254721Semaste    /// @param[in] remote_url
1794254721Semaste    ///     The URL format that we are connecting to.
1795254721Semaste    ///
1796254721Semaste    /// @return
1797254721Semaste    ///     Returns an error object.
1798254721Semaste    //------------------------------------------------------------------
1799254721Semaste    virtual Error
1800254721Semaste    ConnectRemote (Stream *strm, const char *remote_url);
1801254721Semaste
1802254721Semaste    bool
1803254721Semaste    GetShouldDetach () const
1804254721Semaste    {
1805254721Semaste        return m_should_detach;
1806254721Semaste    }
1807254721Semaste
1808254721Semaste    void
1809254721Semaste    SetShouldDetach (bool b)
1810254721Semaste    {
1811254721Semaste        m_should_detach = b;
1812254721Semaste    }
1813254721Semaste
1814254721Semaste    //------------------------------------------------------------------
1815254721Semaste    /// Get the image information address for the current process.
1816254721Semaste    ///
1817254721Semaste    /// Some runtimes have system functions that can help dynamic
1818254721Semaste    /// loaders locate the dynamic loader information needed to observe
1819254721Semaste    /// shared libraries being loaded or unloaded. This function is
1820254721Semaste    /// in the Process interface (as opposed to the DynamicLoader
1821254721Semaste    /// interface) to ensure that remote debugging can take advantage of
1822254721Semaste    /// this functionality.
1823254721Semaste    ///
1824254721Semaste    /// @return
1825254721Semaste    ///     The address of the dynamic loader information, or
1826254721Semaste    ///     LLDB_INVALID_ADDRESS if this is not supported by this
1827254721Semaste    ///     interface.
1828254721Semaste    //------------------------------------------------------------------
1829254721Semaste    virtual lldb::addr_t
1830254721Semaste    GetImageInfoAddress ();
1831254721Semaste
1832254721Semaste    //------------------------------------------------------------------
1833254721Semaste    /// Load a shared library into this process.
1834254721Semaste    ///
1835254721Semaste    /// Try and load a shared library into the current process. This
1836254721Semaste    /// call might fail in the dynamic loader plug-in says it isn't safe
1837254721Semaste    /// to try and load shared libraries at the moment.
1838254721Semaste    ///
1839254721Semaste    /// @param[in] image_spec
1840254721Semaste    ///     The image file spec that points to the shared library that
1841254721Semaste    ///     you want to load.
1842254721Semaste    ///
1843254721Semaste    /// @param[out] error
1844254721Semaste    ///     An error object that gets filled in with any errors that
1845254721Semaste    ///     might occur when trying to load the shared library.
1846254721Semaste    ///
1847254721Semaste    /// @return
1848254721Semaste    ///     A token that represents the shared library that can be
1849254721Semaste    ///     later used to unload the shared library. A value of
1850254721Semaste    ///     LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
1851254721Semaste    ///     library can't be opened.
1852254721Semaste    //------------------------------------------------------------------
1853254721Semaste    virtual uint32_t
1854254721Semaste    LoadImage (const FileSpec &image_spec, Error &error);
1855254721Semaste
1856254721Semaste    virtual Error
1857254721Semaste    UnloadImage (uint32_t image_token);
1858254721Semaste
1859254721Semaste    //------------------------------------------------------------------
1860254721Semaste    /// Register for process and thread notifications.
1861254721Semaste    ///
1862254721Semaste    /// Clients can register nofication callbacks by filling out a
1863254721Semaste    /// Process::Notifications structure and calling this function.
1864254721Semaste    ///
1865254721Semaste    /// @param[in] callbacks
1866254721Semaste    ///     A structure that contains the notification baton and
1867254721Semaste    ///     callback functions.
1868254721Semaste    ///
1869254721Semaste    /// @see Process::Notifications
1870254721Semaste    //------------------------------------------------------------------
1871254721Semaste#ifndef SWIG
1872254721Semaste    void
1873254721Semaste    RegisterNotificationCallbacks (const Process::Notifications& callbacks);
1874254721Semaste#endif
1875254721Semaste    //------------------------------------------------------------------
1876254721Semaste    /// Unregister for process and thread notifications.
1877254721Semaste    ///
1878254721Semaste    /// Clients can unregister nofication callbacks by passing a copy of
1879254721Semaste    /// the original baton and callbacks in \a callbacks.
1880254721Semaste    ///
1881254721Semaste    /// @param[in] callbacks
1882254721Semaste    ///     A structure that contains the notification baton and
1883254721Semaste    ///     callback functions.
1884254721Semaste    ///
1885254721Semaste    /// @return
1886254721Semaste    ///     Returns \b true if the notification callbacks were
1887254721Semaste    ///     successfully removed from the process, \b false otherwise.
1888254721Semaste    ///
1889254721Semaste    /// @see Process::Notifications
1890254721Semaste    //------------------------------------------------------------------
1891254721Semaste#ifndef SWIG
1892254721Semaste    bool
1893254721Semaste    UnregisterNotificationCallbacks (const Process::Notifications& callbacks);
1894254721Semaste#endif
1895254721Semaste    //==================================================================
1896254721Semaste    // Built in Process Control functions
1897254721Semaste    //==================================================================
1898254721Semaste    //------------------------------------------------------------------
1899254721Semaste    /// Resumes all of a process's threads as configured using the
1900254721Semaste    /// Thread run control functions.
1901254721Semaste    ///
1902254721Semaste    /// Threads for a process should be updated with one of the run
1903254721Semaste    /// control actions (resume, step, or suspend) that they should take
1904254721Semaste    /// when the process is resumed. If no run control action is given
1905254721Semaste    /// to a thread it will be resumed by default.
1906254721Semaste    ///
1907254721Semaste    /// This function is not meant to be overridden by Process
1908254721Semaste    /// subclasses. This function will take care of disabling any
1909254721Semaste    /// breakpoints that threads may be stopped at, single stepping, and
1910254721Semaste    /// re-enabling breakpoints, and enabling the basic flow control
1911254721Semaste    /// that the plug-in instances need not worry about.
1912254721Semaste    ///
1913254721Semaste    /// N.B. This function also sets the Write side of the Run Lock,
1914254721Semaste    /// which is unset when the corresponding stop event is pulled off
1915254721Semaste    /// the Public Event Queue.  If you need to resume the process without
1916254721Semaste    /// setting the Run Lock, use PrivateResume (though you should only do
1917254721Semaste    /// that from inside the Process class.
1918254721Semaste    ///
1919254721Semaste    /// @return
1920254721Semaste    ///     Returns an error object.
1921254721Semaste    ///
1922254721Semaste    /// @see Thread:Resume()
1923254721Semaste    /// @see Thread:Step()
1924254721Semaste    /// @see Thread:Suspend()
1925254721Semaste    //------------------------------------------------------------------
1926254721Semaste    Error
1927254721Semaste    Resume();
1928254721Semaste
1929254721Semaste    //------------------------------------------------------------------
1930254721Semaste    /// Halts a running process.
1931254721Semaste    ///
1932254721Semaste    /// This function is not meant to be overridden by Process
1933254721Semaste    /// subclasses.
1934254721Semaste    /// If the process is successfully halted, a eStateStopped
1935254721Semaste    /// process event with GetInterrupted will be broadcast.  If false, we will
1936254721Semaste    /// halt the process with no events generated by the halt.
1937254721Semaste    ///
1938254721Semaste    /// @param[in] clear_thread_plans
1939254721Semaste    ///     If true, when the process stops, clear all thread plans.
1940254721Semaste    ///
1941254721Semaste    /// @return
1942254721Semaste    ///     Returns an error object.  If the error is empty, the process is halted.
1943254721Semaste    ///     otherwise the halt has failed.
1944254721Semaste    //------------------------------------------------------------------
1945254721Semaste    Error
1946254721Semaste    Halt (bool clear_thread_plans = false);
1947254721Semaste
1948254721Semaste    //------------------------------------------------------------------
1949254721Semaste    /// Detaches from a running or stopped process.
1950254721Semaste    ///
1951254721Semaste    /// This function is not meant to be overridden by Process
1952254721Semaste    /// subclasses.
1953254721Semaste    ///
1954254721Semaste    /// @param[in] keep_stopped
1955254721Semaste    ///     If true, don't resume the process on detach.
1956254721Semaste    ///
1957254721Semaste    /// @return
1958254721Semaste    ///     Returns an error object.
1959254721Semaste    //------------------------------------------------------------------
1960254721Semaste    Error
1961254721Semaste    Detach (bool keep_stopped);
1962254721Semaste
1963254721Semaste    //------------------------------------------------------------------
1964254721Semaste    /// Kills the process and shuts down all threads that were spawned
1965254721Semaste    /// to track and monitor the process.
1966254721Semaste    ///
1967254721Semaste    /// This function is not meant to be overridden by Process
1968254721Semaste    /// subclasses.
1969254721Semaste    ///
1970254721Semaste    /// @return
1971254721Semaste    ///     Returns an error object.
1972254721Semaste    //------------------------------------------------------------------
1973254721Semaste    Error
1974254721Semaste    Destroy();
1975254721Semaste
1976254721Semaste    //------------------------------------------------------------------
1977254721Semaste    /// Sends a process a UNIX signal \a signal.
1978254721Semaste    ///
1979254721Semaste    /// This function is not meant to be overridden by Process
1980254721Semaste    /// subclasses.
1981254721Semaste    ///
1982254721Semaste    /// @return
1983254721Semaste    ///     Returns an error object.
1984254721Semaste    //------------------------------------------------------------------
1985254721Semaste    Error
1986254721Semaste    Signal (int signal);
1987254721Semaste
1988254721Semaste    virtual UnixSignals &
1989254721Semaste    GetUnixSignals ()
1990254721Semaste    {
1991254721Semaste        return m_unix_signals;
1992254721Semaste    }
1993254721Semaste
1994254721Semaste    //==================================================================
1995254721Semaste    // Plug-in Process Control Overrides
1996254721Semaste    //==================================================================
1997254721Semaste
1998254721Semaste    //------------------------------------------------------------------
1999254721Semaste    /// Called before attaching to a process.
2000254721Semaste    ///
2001254721Semaste    /// Allow Process plug-ins to execute some code before attaching a
2002254721Semaste    /// process.
2003254721Semaste    ///
2004254721Semaste    /// @return
2005254721Semaste    ///     Returns an error object.
2006254721Semaste    //------------------------------------------------------------------
2007254721Semaste    virtual Error
2008254721Semaste    WillAttachToProcessWithID (lldb::pid_t pid)
2009254721Semaste    {
2010254721Semaste        return Error();
2011254721Semaste    }
2012254721Semaste
2013254721Semaste    //------------------------------------------------------------------
2014254721Semaste    /// Called before attaching to a process.
2015254721Semaste    ///
2016254721Semaste    /// Allow Process plug-ins to execute some code before attaching a
2017254721Semaste    /// process.
2018254721Semaste    ///
2019254721Semaste    /// @return
2020254721Semaste    ///     Returns an error object.
2021254721Semaste    //------------------------------------------------------------------
2022254721Semaste    virtual Error
2023254721Semaste    WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
2024254721Semaste    {
2025254721Semaste        return Error();
2026254721Semaste    }
2027254721Semaste
2028254721Semaste    //------------------------------------------------------------------
2029254721Semaste    /// Attach to a remote system via a URL
2030254721Semaste    ///
2031254721Semaste    /// @param[in] strm
2032254721Semaste    ///     A stream where output intended for the user
2033254721Semaste    ///     (if the driver has a way to display that) generated during
2034254721Semaste    ///     the connection.  This may be NULL if no output is needed.A
2035254721Semaste    ///
2036254721Semaste    /// @param[in] remote_url
2037254721Semaste    ///     The URL format that we are connecting to.
2038254721Semaste    ///
2039254721Semaste    /// @return
2040254721Semaste    ///     Returns an error object.
2041254721Semaste    //------------------------------------------------------------------
2042254721Semaste    virtual Error
2043254721Semaste    DoConnectRemote (Stream *strm, const char *remote_url)
2044254721Semaste    {
2045254721Semaste        Error error;
2046254721Semaste        error.SetErrorString ("remote connections are not supported");
2047254721Semaste        return error;
2048254721Semaste    }
2049254721Semaste
2050254721Semaste    //------------------------------------------------------------------
2051254721Semaste    /// Attach to an existing process using a process ID.
2052254721Semaste    ///
2053254721Semaste    /// @param[in] pid
2054254721Semaste    ///     The process ID that we should attempt to attach to.
2055254721Semaste    ///
2056254721Semaste    /// @return
2057254721Semaste    ///     Returns \a pid if attaching was successful, or
2058254721Semaste    ///     LLDB_INVALID_PROCESS_ID if attaching fails.
2059254721Semaste    //------------------------------------------------------------------
2060254721Semaste    virtual Error
2061254721Semaste    DoAttachToProcessWithID (lldb::pid_t pid)
2062254721Semaste    {
2063254721Semaste        Error error;
2064254721Semaste        error.SetErrorStringWithFormat("error: %s does not support attaching to a process by pid", GetPluginName().GetCString());
2065254721Semaste        return error;
2066254721Semaste    }
2067254721Semaste
2068254721Semaste    //------------------------------------------------------------------
2069254721Semaste    /// Attach to an existing process using a process ID.
2070254721Semaste    ///
2071254721Semaste    /// @param[in] pid
2072254721Semaste    ///     The process ID that we should attempt to attach to.
2073254721Semaste    ///
2074254721Semaste    /// @param[in] attach_info
2075254721Semaste    ///     Information on how to do the attach. For example, GetUserID()
2076254721Semaste    ///     will return the uid to attach as.
2077254721Semaste    ///
2078254721Semaste    /// @return
2079254721Semaste    ///     Returns \a pid if attaching was successful, or
2080254721Semaste    ///     LLDB_INVALID_PROCESS_ID if attaching fails.
2081254721Semaste    /// hanming : need flag
2082254721Semaste    //------------------------------------------------------------------
2083254721Semaste    virtual Error
2084254721Semaste    DoAttachToProcessWithID (lldb::pid_t pid,  const ProcessAttachInfo &attach_info)
2085254721Semaste    {
2086254721Semaste        Error error;
2087254721Semaste        error.SetErrorStringWithFormat("error: %s does not support attaching to a process by pid", GetPluginName().GetCString());
2088254721Semaste        return error;
2089254721Semaste    }
2090254721Semaste
2091254721Semaste    //------------------------------------------------------------------
2092254721Semaste    /// Attach to an existing process using a partial process name.
2093254721Semaste    ///
2094254721Semaste    /// @param[in] process_name
2095254721Semaste    ///     The name of the process to attach to.
2096254721Semaste    ///
2097254721Semaste    /// @param[in] wait_for_launch
2098254721Semaste    ///     If \b true, wait for the process to be launched and attach
2099254721Semaste    ///     as soon as possible after it does launch. If \b false, then
2100254721Semaste    ///     search for a matching process the currently exists.
2101254721Semaste    ///
2102254721Semaste    /// @param[in] attach_info
2103254721Semaste    ///     Information on how to do the attach. For example, GetUserID()
2104254721Semaste    ///     will return the uid to attach as.
2105254721Semaste    ///
2106254721Semaste    /// @return
2107254721Semaste    ///     Returns \a pid if attaching was successful, or
2108254721Semaste    ///     LLDB_INVALID_PROCESS_ID if attaching fails.
2109254721Semaste    //------------------------------------------------------------------
2110254721Semaste    virtual Error
2111254721Semaste    DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
2112254721Semaste    {
2113254721Semaste        Error error;
2114254721Semaste        error.SetErrorString("attach by name is not supported");
2115254721Semaste        return error;
2116254721Semaste    }
2117254721Semaste
2118254721Semaste    //------------------------------------------------------------------
2119254721Semaste    /// Called after attaching a process.
2120254721Semaste    ///
2121254721Semaste    /// Allow Process plug-ins to execute some code after attaching to
2122254721Semaste    /// a process.
2123254721Semaste    //------------------------------------------------------------------
2124254721Semaste    virtual void
2125254721Semaste    DidAttach () {}
2126254721Semaste
2127254721Semaste
2128254721Semaste    //------------------------------------------------------------------
2129254721Semaste    /// Called after a process re-execs itself.
2130254721Semaste    ///
2131254721Semaste    /// Allow Process plug-ins to execute some code after a process has
2132254721Semaste    /// exec'ed itself. Subclasses typically should override DoDidExec()
2133254721Semaste    /// as the lldb_private::Process class needs to remove its dynamic
2134254721Semaste    /// loader, runtime, ABI and other plug-ins, as well as unload all
2135254721Semaste    /// shared libraries.
2136254721Semaste    //------------------------------------------------------------------
2137254721Semaste    virtual void
2138254721Semaste    DidExec ();
2139254721Semaste
2140254721Semaste    //------------------------------------------------------------------
2141254721Semaste    /// Subclasses of Process should implement this function if they
2142254721Semaste    /// need to do anything after a process exec's itself.
2143254721Semaste    //------------------------------------------------------------------
2144254721Semaste    virtual void
2145254721Semaste    DoDidExec ()
2146254721Semaste    {
2147254721Semaste    }
2148254721Semaste
2149254721Semaste    //------------------------------------------------------------------
2150254721Semaste    /// Called before launching to a process.
2151254721Semaste    ///
2152254721Semaste    /// Allow Process plug-ins to execute some code before launching a
2153254721Semaste    /// process.
2154254721Semaste    ///
2155254721Semaste    /// @return
2156254721Semaste    ///     Returns an error object.
2157254721Semaste    //------------------------------------------------------------------
2158254721Semaste    virtual Error
2159254721Semaste    WillLaunch (Module* module)
2160254721Semaste    {
2161254721Semaste        return Error();
2162254721Semaste    }
2163254721Semaste
2164254721Semaste    //------------------------------------------------------------------
2165254721Semaste    /// Launch a new process.
2166254721Semaste    ///
2167254721Semaste    /// Launch a new process by spawning a new process using \a module's
2168254721Semaste    /// file as the file to launch. Arguments are given in \a argv,
2169254721Semaste    /// and the environment variables are in \a envp. Standard input
2170254721Semaste    /// and output files can be optionally re-directed to \a stdin_path,
2171254721Semaste    /// \a stdout_path, and \a stderr_path.
2172254721Semaste    ///
2173254721Semaste    /// @param[in] module
2174254721Semaste    ///     The module from which to extract the file specification and
2175254721Semaste    ///     launch.
2176254721Semaste    ///
2177254721Semaste    /// @param[in] argv
2178254721Semaste    ///     The argument array.
2179254721Semaste    ///
2180254721Semaste    /// @param[in] envp
2181254721Semaste    ///     The environment array.
2182254721Semaste    ///
2183254721Semaste    /// @param[in] launch_flags
2184254721Semaste    ///     Flags to modify the launch (@see lldb::LaunchFlags)
2185254721Semaste    ///
2186254721Semaste    /// @param[in] stdin_path
2187254721Semaste    ///     The path to use when re-directing the STDIN of the new
2188254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
2189254721Semaste    ///     terminal will be used.
2190254721Semaste    ///
2191254721Semaste    /// @param[in] stdout_path
2192254721Semaste    ///     The path to use when re-directing the STDOUT of the new
2193254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
2194254721Semaste    ///     terminal will be used.
2195254721Semaste    ///
2196254721Semaste    /// @param[in] stderr_path
2197254721Semaste    ///     The path to use when re-directing the STDERR of the new
2198254721Semaste    ///     process. If all stdXX_path arguments are NULL, a pseudo
2199254721Semaste    ///     terminal will be used.
2200254721Semaste    ///
2201254721Semaste    /// @param[in] working_directory
2202254721Semaste    ///     The working directory to have the child process run in
2203254721Semaste    ///
2204254721Semaste    /// @return
2205254721Semaste    ///     A new valid process ID, or LLDB_INVALID_PROCESS_ID if
2206254721Semaste    ///     launching fails.
2207254721Semaste    //------------------------------------------------------------------
2208254721Semaste    virtual Error
2209254721Semaste    DoLaunch (Module *exe_module,
2210254721Semaste              const ProcessLaunchInfo &launch_info)
2211254721Semaste    {
2212254721Semaste        Error error;
2213254721Semaste        error.SetErrorStringWithFormat("error: %s does not support launching processes", GetPluginName().GetCString());
2214254721Semaste        return error;
2215254721Semaste    }
2216254721Semaste
2217254721Semaste
2218254721Semaste    //------------------------------------------------------------------
2219254721Semaste    /// Called after launching a process.
2220254721Semaste    ///
2221254721Semaste    /// Allow Process plug-ins to execute some code after launching
2222254721Semaste    /// a process.
2223254721Semaste    //------------------------------------------------------------------
2224254721Semaste    virtual void
2225254721Semaste    DidLaunch () {}
2226254721Semaste
2227254721Semaste
2228254721Semaste
2229254721Semaste    //------------------------------------------------------------------
2230254721Semaste    /// Called before resuming to a process.
2231254721Semaste    ///
2232254721Semaste    /// Allow Process plug-ins to execute some code before resuming a
2233254721Semaste    /// process.
2234254721Semaste    ///
2235254721Semaste    /// @return
2236254721Semaste    ///     Returns an error object.
2237254721Semaste    //------------------------------------------------------------------
2238254721Semaste    virtual Error
2239254721Semaste    WillResume () { return Error(); }
2240254721Semaste
2241254721Semaste    //------------------------------------------------------------------
2242254721Semaste    /// Resumes all of a process's threads as configured using the
2243254721Semaste    /// Thread run control functions.
2244254721Semaste    ///
2245254721Semaste    /// Threads for a process should be updated with one of the run
2246254721Semaste    /// control actions (resume, step, or suspend) that they should take
2247254721Semaste    /// when the process is resumed. If no run control action is given
2248254721Semaste    /// to a thread it will be resumed by default.
2249254721Semaste    ///
2250254721Semaste    /// @return
2251254721Semaste    ///     Returns \b true if the process successfully resumes using
2252254721Semaste    ///     the thread run control actions, \b false otherwise.
2253254721Semaste    ///
2254254721Semaste    /// @see Thread:Resume()
2255254721Semaste    /// @see Thread:Step()
2256254721Semaste    /// @see Thread:Suspend()
2257254721Semaste    //------------------------------------------------------------------
2258254721Semaste    virtual Error
2259254721Semaste    DoResume ()
2260254721Semaste    {
2261254721Semaste        Error error;
2262254721Semaste        error.SetErrorStringWithFormat("error: %s does not support resuming processes", GetPluginName().GetCString());
2263254721Semaste        return error;
2264254721Semaste    }
2265254721Semaste
2266254721Semaste
2267254721Semaste    //------------------------------------------------------------------
2268254721Semaste    /// Called after resuming a process.
2269254721Semaste    ///
2270254721Semaste    /// Allow Process plug-ins to execute some code after resuming
2271254721Semaste    /// a process.
2272254721Semaste    //------------------------------------------------------------------
2273254721Semaste    virtual void
2274254721Semaste    DidResume () {}
2275254721Semaste
2276254721Semaste
2277254721Semaste    //------------------------------------------------------------------
2278254721Semaste    /// Called before halting to a process.
2279254721Semaste    ///
2280254721Semaste    /// Allow Process plug-ins to execute some code before halting a
2281254721Semaste    /// process.
2282254721Semaste    ///
2283254721Semaste    /// @return
2284254721Semaste    ///     Returns an error object.
2285254721Semaste    //------------------------------------------------------------------
2286254721Semaste    virtual Error
2287254721Semaste    WillHalt () { return Error(); }
2288254721Semaste
2289254721Semaste    //------------------------------------------------------------------
2290254721Semaste    /// Halts a running process.
2291254721Semaste    ///
2292254721Semaste    /// DoHalt must produce one and only one stop StateChanged event if it actually
2293254721Semaste    /// stops the process.  If the stop happens through some natural event (for
2294254721Semaste    /// instance a SIGSTOP), then forwarding that event will do.  Otherwise, you must
2295254721Semaste    /// generate the event manually.  Note also, the private event thread is stopped when
2296254721Semaste    /// DoHalt is run to prevent the events generated while halting to trigger
2297254721Semaste    /// other state changes before the halt is complete.
2298254721Semaste    ///
2299254721Semaste    /// @param[out] caused_stop
2300254721Semaste    ///     If true, then this Halt caused the stop, otherwise, the
2301254721Semaste    ///     process was already stopped.
2302254721Semaste    ///
2303254721Semaste    /// @return
2304254721Semaste    ///     Returns \b true if the process successfully halts, \b false
2305254721Semaste    ///     otherwise.
2306254721Semaste    //------------------------------------------------------------------
2307254721Semaste    virtual Error
2308254721Semaste    DoHalt (bool &caused_stop)
2309254721Semaste    {
2310254721Semaste        Error error;
2311254721Semaste        error.SetErrorStringWithFormat("error: %s does not support halting processes", GetPluginName().GetCString());
2312254721Semaste        return error;
2313254721Semaste    }
2314254721Semaste
2315254721Semaste
2316254721Semaste    //------------------------------------------------------------------
2317254721Semaste    /// Called after halting a process.
2318254721Semaste    ///
2319254721Semaste    /// Allow Process plug-ins to execute some code after halting
2320254721Semaste    /// a process.
2321254721Semaste    //------------------------------------------------------------------
2322254721Semaste    virtual void
2323254721Semaste    DidHalt () {}
2324254721Semaste
2325254721Semaste    //------------------------------------------------------------------
2326254721Semaste    /// Called before detaching from a process.
2327254721Semaste    ///
2328254721Semaste    /// Allow Process plug-ins to execute some code before detaching
2329254721Semaste    /// from a process.
2330254721Semaste    ///
2331254721Semaste    /// @return
2332254721Semaste    ///     Returns an error object.
2333254721Semaste    //------------------------------------------------------------------
2334254721Semaste    virtual Error
2335254721Semaste    WillDetach ()
2336254721Semaste    {
2337254721Semaste        return Error();
2338254721Semaste    }
2339254721Semaste
2340254721Semaste    //------------------------------------------------------------------
2341254721Semaste    /// Detaches from a running or stopped process.
2342254721Semaste    ///
2343254721Semaste    /// @return
2344254721Semaste    ///     Returns \b true if the process successfully detaches, \b
2345254721Semaste    ///     false otherwise.
2346254721Semaste    //------------------------------------------------------------------
2347254721Semaste    virtual Error
2348254721Semaste    DoDetach (bool keep_stopped)
2349254721Semaste    {
2350254721Semaste        Error error;
2351254721Semaste        error.SetErrorStringWithFormat("error: %s does not support detaching from processes", GetPluginName().GetCString());
2352254721Semaste        return error;
2353254721Semaste    }
2354254721Semaste
2355254721Semaste
2356254721Semaste    //------------------------------------------------------------------
2357254721Semaste    /// Called after detaching from a process.
2358254721Semaste    ///
2359254721Semaste    /// Allow Process plug-ins to execute some code after detaching
2360254721Semaste    /// from a process.
2361254721Semaste    //------------------------------------------------------------------
2362254721Semaste    virtual void
2363254721Semaste    DidDetach () {}
2364254721Semaste
2365254721Semaste    virtual bool
2366254721Semaste    DetachRequiresHalt() { return false; }
2367254721Semaste
2368254721Semaste    //------------------------------------------------------------------
2369254721Semaste    /// Called before sending a signal to a process.
2370254721Semaste    ///
2371254721Semaste    /// Allow Process plug-ins to execute some code before sending a
2372254721Semaste    /// signal to a process.
2373254721Semaste    ///
2374254721Semaste    /// @return
2375254721Semaste    ///     Returns no error if it is safe to proceed with a call to
2376254721Semaste    ///     Process::DoSignal(int), otherwise an error describing what
2377254721Semaste    ///     prevents the signal from being sent.
2378254721Semaste    //------------------------------------------------------------------
2379254721Semaste    virtual Error
2380254721Semaste    WillSignal () { return Error(); }
2381254721Semaste
2382254721Semaste    //------------------------------------------------------------------
2383254721Semaste    /// Sends a process a UNIX signal \a signal.
2384254721Semaste    ///
2385254721Semaste    /// @return
2386254721Semaste    ///     Returns an error object.
2387254721Semaste    //------------------------------------------------------------------
2388254721Semaste    virtual Error
2389254721Semaste    DoSignal (int signal)
2390254721Semaste    {
2391254721Semaste        Error error;
2392254721Semaste        error.SetErrorStringWithFormat("error: %s does not support senging signals to processes", GetPluginName().GetCString());
2393254721Semaste        return error;
2394254721Semaste    }
2395254721Semaste
2396254721Semaste    virtual Error
2397254721Semaste    WillDestroy () { return Error(); }
2398254721Semaste
2399254721Semaste    virtual Error
2400254721Semaste    DoDestroy () = 0;
2401254721Semaste
2402254721Semaste    virtual void
2403254721Semaste    DidDestroy () { }
2404254721Semaste
2405254721Semaste    virtual bool
2406254721Semaste    DestroyRequiresHalt() { return true; }
2407254721Semaste
2408254721Semaste
2409254721Semaste    //------------------------------------------------------------------
2410254721Semaste    /// Called after sending a signal to a process.
2411254721Semaste    ///
2412254721Semaste    /// Allow Process plug-ins to execute some code after sending a
2413254721Semaste    /// signal to a process.
2414254721Semaste    //------------------------------------------------------------------
2415254721Semaste    virtual void
2416254721Semaste    DidSignal () {}
2417254721Semaste
2418254721Semaste    //------------------------------------------------------------------
2419254721Semaste    /// Currently called as part of ShouldStop.
2420254721Semaste    /// FIXME: Should really happen when the target stops before the
2421254721Semaste    /// event is taken from the queue...
2422254721Semaste    ///
2423254721Semaste    /// This callback is called as the event
2424254721Semaste    /// is about to be queued up to allow Process plug-ins to execute
2425254721Semaste    /// some code prior to clients being notified that a process was
2426254721Semaste    /// stopped. Common operations include updating the thread list,
2427254721Semaste    /// invalidating any thread state (registers, stack, etc) prior to
2428254721Semaste    /// letting the notification go out.
2429254721Semaste    ///
2430254721Semaste    //------------------------------------------------------------------
2431254721Semaste    virtual void
2432254721Semaste    RefreshStateAfterStop () = 0;
2433254721Semaste
2434254721Semaste    //------------------------------------------------------------------
2435254721Semaste    /// Get the target object pointer for this module.
2436254721Semaste    ///
2437254721Semaste    /// @return
2438254721Semaste    ///     A Target object pointer to the target that owns this
2439254721Semaste    ///     module.
2440254721Semaste    //------------------------------------------------------------------
2441254721Semaste    Target &
2442254721Semaste    GetTarget ()
2443254721Semaste    {
2444254721Semaste        return m_target;
2445254721Semaste    }
2446254721Semaste
2447254721Semaste    //------------------------------------------------------------------
2448254721Semaste    /// Get the const target object pointer for this module.
2449254721Semaste    ///
2450254721Semaste    /// @return
2451254721Semaste    ///     A const Target object pointer to the target that owns this
2452254721Semaste    ///     module.
2453254721Semaste    //------------------------------------------------------------------
2454254721Semaste    const Target &
2455254721Semaste    GetTarget () const
2456254721Semaste    {
2457254721Semaste        return m_target;
2458254721Semaste    }
2459254721Semaste
2460254721Semaste    //------------------------------------------------------------------
2461254721Semaste    /// Flush all data in the process.
2462254721Semaste    ///
2463254721Semaste    /// Flush the memory caches, all threads, and any other cached data
2464254721Semaste    /// in the process.
2465254721Semaste    ///
2466254721Semaste    /// This function can be called after a world changing event like
2467254721Semaste    /// adding a new symbol file, or after the process makes a large
2468254721Semaste    /// context switch (from boot ROM to booted into an OS).
2469254721Semaste    //------------------------------------------------------------------
2470254721Semaste    void
2471254721Semaste    Flush ();
2472254721Semaste
2473254721Semaste    //------------------------------------------------------------------
2474254721Semaste    /// Get accessor for the current process state.
2475254721Semaste    ///
2476254721Semaste    /// @return
2477254721Semaste    ///     The current state of the process.
2478254721Semaste    ///
2479254721Semaste    /// @see lldb::StateType
2480254721Semaste    //------------------------------------------------------------------
2481254721Semaste    lldb::StateType
2482254721Semaste    GetState ();
2483254721Semaste
2484254721Semaste    ExecutionResults
2485254721Semaste    RunThreadPlan (ExecutionContext &exe_ctx,
2486254721Semaste                    lldb::ThreadPlanSP &thread_plan_sp,
2487254721Semaste                    bool stop_others,
2488254721Semaste                    bool run_others,
2489254721Semaste                    bool unwind_on_error,
2490254721Semaste                    bool ignore_breakpoints,
2491254721Semaste                    uint32_t timeout_usec,
2492254721Semaste                    Stream &errors);
2493254721Semaste
2494254721Semaste    static const char *
2495254721Semaste    ExecutionResultAsCString (ExecutionResults result);
2496254721Semaste
2497254721Semaste    void
2498254721Semaste    GetStatus (Stream &ostrm);
2499254721Semaste
2500254721Semaste    size_t
2501254721Semaste    GetThreadStatus (Stream &ostrm,
2502254721Semaste                     bool only_threads_with_stop_reason,
2503254721Semaste                     uint32_t start_frame,
2504254721Semaste                     uint32_t num_frames,
2505254721Semaste                     uint32_t num_frames_with_source);
2506254721Semaste
2507254721Semaste    void
2508254721Semaste    SendAsyncInterrupt ();
2509254721Semaste
2510254721Semasteprotected:
2511254721Semaste
2512254721Semaste    void
2513254721Semaste    SetState (lldb::EventSP &event_sp);
2514254721Semaste
2515254721Semaste    lldb::StateType
2516254721Semaste    GetPrivateState ();
2517254721Semaste
2518254721Semaste    //------------------------------------------------------------------
2519254721Semaste    /// The "private" side of resuming a process.  This doesn't alter the
2520254721Semaste    /// state of m_run_lock, but just causes the process to resume.
2521254721Semaste    ///
2522254721Semaste    /// @return
2523254721Semaste    ///     An Error object describing the success or failure of the resume.
2524254721Semaste    //------------------------------------------------------------------
2525254721Semaste    Error
2526254721Semaste    PrivateResume ();
2527254721Semaste
2528254721Semaste    //------------------------------------------------------------------
2529254721Semaste    // Called internally
2530254721Semaste    //------------------------------------------------------------------
2531254721Semaste    void
2532254721Semaste    CompleteAttach ();
2533254721Semaste
2534254721Semastepublic:
2535254721Semaste    //------------------------------------------------------------------
2536254721Semaste    /// Get the exit status for a process.
2537254721Semaste    ///
2538254721Semaste    /// @return
2539254721Semaste    ///     The process's return code, or -1 if the current process
2540254721Semaste    ///     state is not eStateExited.
2541254721Semaste    //------------------------------------------------------------------
2542254721Semaste    int
2543254721Semaste    GetExitStatus ();
2544254721Semaste
2545254721Semaste    //------------------------------------------------------------------
2546254721Semaste    /// Get a textual description of what the process exited.
2547254721Semaste    ///
2548254721Semaste    /// @return
2549254721Semaste    ///     The textual description of why the process exited, or NULL
2550254721Semaste    ///     if there is no description available.
2551254721Semaste    //------------------------------------------------------------------
2552254721Semaste    const char *
2553254721Semaste    GetExitDescription ();
2554254721Semaste
2555254721Semaste
2556254721Semaste    virtual void
2557254721Semaste    DidExit ()
2558254721Semaste    {
2559254721Semaste    }
2560254721Semaste
2561254721Semaste    //------------------------------------------------------------------
2562254721Semaste    /// Get the Modification ID of the process.
2563254721Semaste    ///
2564254721Semaste    /// @return
2565254721Semaste    ///     The modification ID of the process.
2566254721Semaste    //------------------------------------------------------------------
2567254721Semaste    ProcessModID
2568254721Semaste    GetModID () const
2569254721Semaste    {
2570254721Semaste        return m_mod_id;
2571254721Semaste    }
2572254721Semaste
2573254721Semaste    const ProcessModID &
2574254721Semaste    GetModIDRef () const
2575254721Semaste    {
2576254721Semaste        return m_mod_id;
2577254721Semaste    }
2578254721Semaste
2579254721Semaste    uint32_t
2580254721Semaste    GetStopID () const
2581254721Semaste    {
2582254721Semaste        return m_mod_id.GetStopID();
2583254721Semaste    }
2584254721Semaste
2585254721Semaste    uint32_t
2586254721Semaste    GetResumeID () const
2587254721Semaste    {
2588254721Semaste        return m_mod_id.GetResumeID();
2589254721Semaste    }
2590254721Semaste
2591254721Semaste    uint32_t
2592254721Semaste    GetLastUserExpressionResumeID () const
2593254721Semaste    {
2594254721Semaste        return m_mod_id.GetLastUserExpressionResumeID();
2595254721Semaste    }
2596254721Semaste
2597254721Semaste    uint32_t
2598254721Semaste    GetLastNaturalStopID()
2599254721Semaste    {
2600254721Semaste        return m_mod_id.GetLastNaturalStopID();
2601254721Semaste    }
2602254721Semaste
2603254721Semaste    //------------------------------------------------------------------
2604254721Semaste    /// Set accessor for the process exit status (return code).
2605254721Semaste    ///
2606254721Semaste    /// Sometimes a child exits and the exit can be detected by global
2607254721Semaste    /// functions (signal handler for SIGCHLD for example). This
2608254721Semaste    /// accessor allows the exit status to be set from an external
2609254721Semaste    /// source.
2610254721Semaste    ///
2611254721Semaste    /// Setting this will cause a eStateExited event to be posted to
2612254721Semaste    /// the process event queue.
2613254721Semaste    ///
2614254721Semaste    /// @param[in] exit_status
2615254721Semaste    ///     The value for the process's return code.
2616254721Semaste    ///
2617254721Semaste    /// @see lldb::StateType
2618254721Semaste    //------------------------------------------------------------------
2619254721Semaste    virtual bool
2620254721Semaste    SetExitStatus (int exit_status, const char *cstr);
2621254721Semaste
2622254721Semaste    //------------------------------------------------------------------
2623254721Semaste    /// Check if a process is still alive.
2624254721Semaste    ///
2625254721Semaste    /// @return
2626254721Semaste    ///     Returns \b true if the process is still valid, \b false
2627254721Semaste    ///     otherwise.
2628254721Semaste    //------------------------------------------------------------------
2629254721Semaste    virtual bool
2630254721Semaste    IsAlive () = 0;
2631254721Semaste
2632254721Semaste    //------------------------------------------------------------------
2633254721Semaste    /// Before lldb detaches from a process, it warns the user that they are about to lose their debug session.
2634254721Semaste    /// In some cases, this warning doesn't need to be emitted -- for instance, with core file debugging where
2635254721Semaste    /// the user can reconstruct the "state" by simply re-running the debugger on the core file.
2636254721Semaste    ///
2637254721Semaste    /// @return
2638254721Semaste    //      true if the user should be warned about detaching from this process.
2639254721Semaste    //------------------------------------------------------------------
2640254721Semaste    virtual bool
2641254721Semaste    WarnBeforeDetach () const
2642254721Semaste    {
2643254721Semaste        return true;
2644254721Semaste    }
2645254721Semaste
2646254721Semaste    //------------------------------------------------------------------
2647254721Semaste    /// Actually do the reading of memory from a process.
2648254721Semaste    ///
2649254721Semaste    /// Subclasses must override this function and can return fewer
2650254721Semaste    /// bytes than requested when memory requests are too large. This
2651254721Semaste    /// class will break up the memory requests and keep advancing the
2652254721Semaste    /// arguments along as needed.
2653254721Semaste    ///
2654254721Semaste    /// @param[in] vm_addr
2655254721Semaste    ///     A virtual load address that indicates where to start reading
2656254721Semaste    ///     memory from.
2657254721Semaste    ///
2658254721Semaste    /// @param[in] size
2659254721Semaste    ///     The number of bytes to read.
2660254721Semaste    ///
2661254721Semaste    /// @param[out] buf
2662254721Semaste    ///     A byte buffer that is at least \a size bytes long that
2663254721Semaste    ///     will receive the memory bytes.
2664254721Semaste    ///
2665254721Semaste    /// @return
2666254721Semaste    ///     The number of bytes that were actually read into \a buf.
2667254721Semaste    //------------------------------------------------------------------
2668254721Semaste    virtual size_t
2669254721Semaste    DoReadMemory (lldb::addr_t vm_addr,
2670254721Semaste                  void *buf,
2671254721Semaste                  size_t size,
2672254721Semaste                  Error &error) = 0;
2673254721Semaste
2674254721Semaste    //------------------------------------------------------------------
2675254721Semaste    /// Read of memory from a process.
2676254721Semaste    ///
2677254721Semaste    /// This function will read memory from the current process's
2678254721Semaste    /// address space and remove any traps that may have been inserted
2679254721Semaste    /// into the memory.
2680254721Semaste    ///
2681254721Semaste    /// This function is not meant to be overridden by Process
2682254721Semaste    /// subclasses, the subclasses should implement
2683254721Semaste    /// Process::DoReadMemory (lldb::addr_t, size_t, void *).
2684254721Semaste    ///
2685254721Semaste    /// @param[in] vm_addr
2686254721Semaste    ///     A virtual load address that indicates where to start reading
2687254721Semaste    ///     memory from.
2688254721Semaste    ///
2689254721Semaste    /// @param[out] buf
2690254721Semaste    ///     A byte buffer that is at least \a size bytes long that
2691254721Semaste    ///     will receive the memory bytes.
2692254721Semaste    ///
2693254721Semaste    /// @param[in] size
2694254721Semaste    ///     The number of bytes to read.
2695254721Semaste    ///
2696254721Semaste    /// @return
2697254721Semaste    ///     The number of bytes that were actually read into \a buf. If
2698254721Semaste    ///     the returned number is greater than zero, yet less than \a
2699254721Semaste    ///     size, then this function will get called again with \a
2700254721Semaste    ///     vm_addr, \a buf, and \a size updated appropriately. Zero is
2701254721Semaste    ///     returned to indicate an error.
2702254721Semaste    //------------------------------------------------------------------
2703254721Semaste    virtual size_t
2704254721Semaste    ReadMemory (lldb::addr_t vm_addr,
2705254721Semaste                void *buf,
2706254721Semaste                size_t size,
2707254721Semaste                Error &error);
2708254721Semaste
2709254721Semaste    //------------------------------------------------------------------
2710254721Semaste    /// Read a NULL terminated string from memory
2711254721Semaste    ///
2712254721Semaste    /// This function will read a cache page at a time until a NULL
2713254721Semaste    /// string terminator is found. It will stop reading if an aligned
2714254721Semaste    /// sequence of NULL termination \a type_width bytes is not found
2715254721Semaste    /// before reading \a cstr_max_len bytes.  The results are always
2716254721Semaste    /// guaranteed to be NULL terminated, and that no more than
2717254721Semaste    /// (max_bytes - type_width) bytes will be read.
2718254721Semaste    ///
2719254721Semaste    /// @param[in] vm_addr
2720254721Semaste    ///     The virtual load address to start the memory read.
2721254721Semaste    ///
2722254721Semaste    /// @param[in] str
2723254721Semaste    ///     A character buffer containing at least max_bytes.
2724254721Semaste    ///
2725254721Semaste    /// @param[in] max_bytes
2726254721Semaste    ///     The maximum number of bytes to read.
2727254721Semaste    ///
2728254721Semaste    /// @param[in] error
2729254721Semaste    ///     The error status of the read operation.
2730254721Semaste    ///
2731254721Semaste    /// @param[in] type_width
2732254721Semaste    ///     The size of the null terminator (1 to 4 bytes per
2733254721Semaste    ///     character).  Defaults to 1.
2734254721Semaste    ///
2735254721Semaste    /// @return
2736254721Semaste    ///     The error status or the number of bytes prior to the null terminator.
2737254721Semaste    //------------------------------------------------------------------
2738254721Semaste    size_t
2739254721Semaste    ReadStringFromMemory (lldb::addr_t vm_addr,
2740254721Semaste                           char *str,
2741254721Semaste                           size_t max_bytes,
2742254721Semaste                           Error &error,
2743254721Semaste                           size_t type_width = 1);
2744254721Semaste
2745254721Semaste    //------------------------------------------------------------------
2746254721Semaste    /// Read a NULL terminated C string from memory
2747254721Semaste    ///
2748254721Semaste    /// This function will read a cache page at a time until the NULL
2749254721Semaste    /// C string terminator is found. It will stop reading if the NULL
2750254721Semaste    /// termination byte isn't found before reading \a cstr_max_len
2751254721Semaste    /// bytes, and the results are always guaranteed to be NULL
2752254721Semaste    /// terminated (at most cstr_max_len - 1 bytes will be read).
2753254721Semaste    //------------------------------------------------------------------
2754254721Semaste    size_t
2755254721Semaste    ReadCStringFromMemory (lldb::addr_t vm_addr,
2756254721Semaste                           char *cstr,
2757254721Semaste                           size_t cstr_max_len,
2758254721Semaste                           Error &error);
2759254721Semaste
2760254721Semaste    size_t
2761254721Semaste    ReadCStringFromMemory (lldb::addr_t vm_addr,
2762254721Semaste                           std::string &out_str,
2763254721Semaste                           Error &error);
2764254721Semaste
2765254721Semaste    size_t
2766254721Semaste    ReadMemoryFromInferior (lldb::addr_t vm_addr,
2767254721Semaste                            void *buf,
2768254721Semaste                            size_t size,
2769254721Semaste                            Error &error);
2770254721Semaste
2771254721Semaste    //------------------------------------------------------------------
2772254721Semaste    /// Reads an unsigned integer of the specified byte size from
2773254721Semaste    /// process memory.
2774254721Semaste    ///
2775254721Semaste    /// @param[in] load_addr
2776254721Semaste    ///     A load address of the integer to read.
2777254721Semaste    ///
2778254721Semaste    /// @param[in] byte_size
2779254721Semaste    ///     The size in byte of the integer to read.
2780254721Semaste    ///
2781254721Semaste    /// @param[in] fail_value
2782254721Semaste    ///     The value to return if we fail to read an integer.
2783254721Semaste    ///
2784254721Semaste    /// @param[out] error
2785254721Semaste    ///     An error that indicates the success or failure of this
2786254721Semaste    ///     operation. If error indicates success (error.Success()),
2787254721Semaste    ///     then the value returned can be trusted, otherwise zero
2788254721Semaste    ///     will be returned.
2789254721Semaste    ///
2790254721Semaste    /// @return
2791254721Semaste    ///     The unsigned integer that was read from the process memory
2792254721Semaste    ///     space. If the integer was smaller than a uint64_t, any
2793254721Semaste    ///     unused upper bytes will be zero filled. If the process
2794254721Semaste    ///     byte order differs from the host byte order, the integer
2795254721Semaste    ///     value will be appropriately byte swapped into host byte
2796254721Semaste    ///     order.
2797254721Semaste    //------------------------------------------------------------------
2798254721Semaste    uint64_t
2799254721Semaste    ReadUnsignedIntegerFromMemory (lldb::addr_t load_addr,
2800254721Semaste                                   size_t byte_size,
2801254721Semaste                                   uint64_t fail_value,
2802254721Semaste                                   Error &error);
2803254721Semaste
2804254721Semaste    lldb::addr_t
2805254721Semaste    ReadPointerFromMemory (lldb::addr_t vm_addr,
2806254721Semaste                           Error &error);
2807254721Semaste
2808254721Semaste    bool
2809254721Semaste    WritePointerToMemory (lldb::addr_t vm_addr,
2810254721Semaste                          lldb::addr_t ptr_value,
2811254721Semaste                          Error &error);
2812254721Semaste
2813254721Semaste    //------------------------------------------------------------------
2814254721Semaste    /// Actually do the writing of memory to a process.
2815254721Semaste    ///
2816254721Semaste    /// @param[in] vm_addr
2817254721Semaste    ///     A virtual load address that indicates where to start writing
2818254721Semaste    ///     memory to.
2819254721Semaste    ///
2820254721Semaste    /// @param[in] buf
2821254721Semaste    ///     A byte buffer that is at least \a size bytes long that
2822254721Semaste    ///     contains the data to write.
2823254721Semaste    ///
2824254721Semaste    /// @param[in] size
2825254721Semaste    ///     The number of bytes to write.
2826254721Semaste    ///
2827254721Semaste    /// @param[out] error
2828254721Semaste    ///     An error value in case the memory write fails.
2829254721Semaste    ///
2830254721Semaste    /// @return
2831254721Semaste    ///     The number of bytes that were actually written.
2832254721Semaste    //------------------------------------------------------------------
2833254721Semaste    virtual size_t
2834254721Semaste    DoWriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
2835254721Semaste    {
2836254721Semaste        error.SetErrorStringWithFormat("error: %s does not support writing to processes", GetPluginName().GetCString());
2837254721Semaste        return 0;
2838254721Semaste    }
2839254721Semaste
2840254721Semaste
2841254721Semaste    //------------------------------------------------------------------
2842254721Semaste    /// Write all or part of a scalar value to memory.
2843254721Semaste    ///
2844254721Semaste    /// The value contained in \a scalar will be swapped to match the
2845254721Semaste    /// byte order of the process that is being debugged. If \a size is
2846254721Semaste    /// less than the size of scalar, the least significate \a size bytes
2847254721Semaste    /// from scalar will be written. If \a size is larger than the byte
2848254721Semaste    /// size of scalar, then the extra space will be padded with zeros
2849254721Semaste    /// and the scalar value will be placed in the least significant
2850254721Semaste    /// bytes in memory.
2851254721Semaste    ///
2852254721Semaste    /// @param[in] vm_addr
2853254721Semaste    ///     A virtual load address that indicates where to start writing
2854254721Semaste    ///     memory to.
2855254721Semaste    ///
2856254721Semaste    /// @param[in] scalar
2857254721Semaste    ///     The scalar to write to the debugged process.
2858254721Semaste    ///
2859254721Semaste    /// @param[in] size
2860254721Semaste    ///     This value can be smaller or larger than the scalar value
2861254721Semaste    ///     itself. If \a size is smaller than the size of \a scalar,
2862254721Semaste    ///     the least significant bytes in \a scalar will be used. If
2863254721Semaste    ///     \a size is larger than the byte size of \a scalar, then
2864254721Semaste    ///     the extra space will be padded with zeros. If \a size is
2865254721Semaste    ///     set to UINT32_MAX, then the size of \a scalar will be used.
2866254721Semaste    ///
2867254721Semaste    /// @param[out] error
2868254721Semaste    ///     An error value in case the memory write fails.
2869254721Semaste    ///
2870254721Semaste    /// @return
2871254721Semaste    ///     The number of bytes that were actually written.
2872254721Semaste    //------------------------------------------------------------------
2873254721Semaste    size_t
2874254721Semaste    WriteScalarToMemory (lldb::addr_t vm_addr,
2875254721Semaste                         const Scalar &scalar,
2876254721Semaste                         size_t size,
2877254721Semaste                         Error &error);
2878254721Semaste
2879254721Semaste    size_t
2880254721Semaste    ReadScalarIntegerFromMemory (lldb::addr_t addr,
2881254721Semaste                                 uint32_t byte_size,
2882254721Semaste                                 bool is_signed,
2883254721Semaste                                 Scalar &scalar,
2884254721Semaste                                 Error &error);
2885254721Semaste
2886254721Semaste    //------------------------------------------------------------------
2887254721Semaste    /// Write memory to a process.
2888254721Semaste    ///
2889254721Semaste    /// This function will write memory to the current process's
2890254721Semaste    /// address space and maintain any traps that might be present due
2891254721Semaste    /// to software breakpoints.
2892254721Semaste    ///
2893254721Semaste    /// This function is not meant to be overridden by Process
2894254721Semaste    /// subclasses, the subclasses should implement
2895254721Semaste    /// Process::DoWriteMemory (lldb::addr_t, size_t, void *).
2896254721Semaste    ///
2897254721Semaste    /// @param[in] vm_addr
2898254721Semaste    ///     A virtual load address that indicates where to start writing
2899254721Semaste    ///     memory to.
2900254721Semaste    ///
2901254721Semaste    /// @param[in] buf
2902254721Semaste    ///     A byte buffer that is at least \a size bytes long that
2903254721Semaste    ///     contains the data to write.
2904254721Semaste    ///
2905254721Semaste    /// @param[in] size
2906254721Semaste    ///     The number of bytes to write.
2907254721Semaste    ///
2908254721Semaste    /// @return
2909254721Semaste    ///     The number of bytes that were actually written.
2910254721Semaste    //------------------------------------------------------------------
2911254721Semaste    size_t
2912254721Semaste    WriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error);
2913254721Semaste
2914254721Semaste
2915254721Semaste    //------------------------------------------------------------------
2916254721Semaste    /// Actually allocate memory in the process.
2917254721Semaste    ///
2918254721Semaste    /// This function will allocate memory in the process's address
2919254721Semaste    /// space.  This can't rely on the generic function calling mechanism,
2920254721Semaste    /// since that requires this function.
2921254721Semaste    ///
2922254721Semaste    /// @param[in] size
2923254721Semaste    ///     The size of the allocation requested.
2924254721Semaste    ///
2925254721Semaste    /// @return
2926254721Semaste    ///     The address of the allocated buffer in the process, or
2927254721Semaste    ///     LLDB_INVALID_ADDRESS if the allocation failed.
2928254721Semaste    //------------------------------------------------------------------
2929254721Semaste
2930254721Semaste    virtual lldb::addr_t
2931254721Semaste    DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
2932254721Semaste    {
2933254721Semaste        error.SetErrorStringWithFormat("error: %s does not support allocating in the debug process", GetPluginName().GetCString());
2934254721Semaste        return LLDB_INVALID_ADDRESS;
2935254721Semaste    }
2936254721Semaste
2937254721Semaste
2938254721Semaste    //------------------------------------------------------------------
2939254721Semaste    /// The public interface to allocating memory in the process.
2940254721Semaste    ///
2941254721Semaste    /// This function will allocate memory in the process's address
2942254721Semaste    /// space.  This can't rely on the generic function calling mechanism,
2943254721Semaste    /// since that requires this function.
2944254721Semaste    ///
2945254721Semaste    /// @param[in] size
2946254721Semaste    ///     The size of the allocation requested.
2947254721Semaste    ///
2948254721Semaste    /// @param[in] permissions
2949254721Semaste    ///     Or together any of the lldb::Permissions bits.  The permissions on
2950254721Semaste    ///     a given memory allocation can't be changed after allocation.  Note
2951254721Semaste    ///     that a block that isn't set writable can still be written on from lldb,
2952254721Semaste    ///     just not by the process itself.
2953254721Semaste    ///
2954254721Semaste    /// @param[in/out] error
2955254721Semaste    ///     An error object to fill in if things go wrong.
2956254721Semaste    /// @return
2957254721Semaste    ///     The address of the allocated buffer in the process, or
2958254721Semaste    ///     LLDB_INVALID_ADDRESS if the allocation failed.
2959254721Semaste    //------------------------------------------------------------------
2960254721Semaste
2961254721Semaste    lldb::addr_t
2962254721Semaste    AllocateMemory (size_t size, uint32_t permissions, Error &error);
2963254721Semaste
2964254721Semaste
2965254721Semaste    //------------------------------------------------------------------
2966254721Semaste    /// Resolve dynamically loaded indirect functions.
2967254721Semaste    ///
2968254721Semaste    /// @param[in] address
2969254721Semaste    ///     The load address of the indirect function to resolve.
2970254721Semaste    ///
2971254721Semaste    /// @param[out] error
2972254721Semaste    ///     An error value in case the resolve fails.
2973254721Semaste    ///
2974254721Semaste    /// @return
2975254721Semaste    ///     The address of the resolved function.
2976254721Semaste    ///     LLDB_INVALID_ADDRESS if the resolution failed.
2977254721Semaste    //------------------------------------------------------------------
2978254721Semaste
2979254721Semaste    virtual lldb::addr_t
2980254721Semaste    ResolveIndirectFunction(const Address *address, Error &error)
2981254721Semaste    {
2982254721Semaste        error.SetErrorStringWithFormat("error: %s does not support indirect functions in the debug process", GetPluginName().GetCString());
2983254721Semaste        return LLDB_INVALID_ADDRESS;
2984254721Semaste    }
2985254721Semaste
2986254721Semaste    virtual Error
2987254721Semaste    GetMemoryRegionInfo (lldb::addr_t load_addr,
2988254721Semaste                        MemoryRegionInfo &range_info)
2989254721Semaste    {
2990254721Semaste        Error error;
2991254721Semaste        error.SetErrorString ("Process::GetMemoryRegionInfo() not supported");
2992254721Semaste        return error;
2993254721Semaste    }
2994254721Semaste
2995254721Semaste    virtual Error
2996254721Semaste    GetWatchpointSupportInfo (uint32_t &num)
2997254721Semaste    {
2998254721Semaste        Error error;
2999254721Semaste        num = 0;
3000254721Semaste        error.SetErrorString ("Process::GetWatchpointSupportInfo() not supported");
3001254721Semaste        return error;
3002254721Semaste    }
3003254721Semaste
3004254721Semaste    virtual Error
3005254721Semaste    GetWatchpointSupportInfo (uint32_t &num, bool& after)
3006254721Semaste    {
3007254721Semaste        Error error;
3008254721Semaste        num = 0;
3009254721Semaste        after = true;
3010254721Semaste        error.SetErrorString ("Process::GetWatchpointSupportInfo() not supported");
3011254721Semaste        return error;
3012254721Semaste    }
3013254721Semaste
3014254721Semaste    lldb::ModuleSP
3015254721Semaste    ReadModuleFromMemory (const FileSpec& file_spec,
3016254721Semaste                          lldb::addr_t header_addr);
3017254721Semaste
3018254721Semaste    //------------------------------------------------------------------
3019254721Semaste    /// Attempt to get the attributes for a region of memory in the process.
3020254721Semaste    ///
3021254721Semaste    /// It may be possible for the remote debug server to inspect attributes
3022254721Semaste    /// for a region of memory in the process, such as whether there is a
3023254721Semaste    /// valid page of memory at a given address or whether that page is
3024254721Semaste    /// readable/writable/executable by the process.
3025254721Semaste    ///
3026254721Semaste    /// @param[in] load_addr
3027254721Semaste    ///     The address of interest in the process.
3028254721Semaste    ///
3029254721Semaste    /// @param[out] permissions
3030254721Semaste    ///     If this call returns successfully, this bitmask will have
3031254721Semaste    ///     its Permissions bits set to indicate whether the region is
3032254721Semaste    ///     readable/writable/executable.  If this call fails, the
3033254721Semaste    ///     bitmask values are undefined.
3034254721Semaste    ///
3035254721Semaste    /// @return
3036254721Semaste    ///     Returns true if it was able to determine the attributes of the
3037254721Semaste    ///     memory region.  False if not.
3038254721Semaste    //------------------------------------------------------------------
3039254721Semaste
3040254721Semaste    virtual bool
3041254721Semaste    GetLoadAddressPermissions (lldb::addr_t load_addr, uint32_t &permissions)
3042254721Semaste    {
3043254721Semaste        MemoryRegionInfo range_info;
3044254721Semaste        permissions = 0;
3045254721Semaste        Error error (GetMemoryRegionInfo (load_addr, range_info));
3046254721Semaste        if (!error.Success())
3047254721Semaste            return false;
3048254721Semaste        if (range_info.GetReadable() == MemoryRegionInfo::eDontKnow
3049254721Semaste            || range_info.GetWritable() == MemoryRegionInfo::eDontKnow
3050254721Semaste            || range_info.GetExecutable() == MemoryRegionInfo::eDontKnow)
3051254721Semaste        {
3052254721Semaste            return false;
3053254721Semaste        }
3054254721Semaste
3055254721Semaste        if (range_info.GetReadable() == MemoryRegionInfo::eYes)
3056254721Semaste            permissions |= lldb::ePermissionsReadable;
3057254721Semaste
3058254721Semaste        if (range_info.GetWritable() == MemoryRegionInfo::eYes)
3059254721Semaste            permissions |= lldb::ePermissionsWritable;
3060254721Semaste
3061254721Semaste        if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
3062254721Semaste            permissions |= lldb::ePermissionsExecutable;
3063254721Semaste
3064254721Semaste        return true;
3065254721Semaste    }
3066254721Semaste
3067254721Semaste    //------------------------------------------------------------------
3068254721Semaste    /// Determines whether executing JIT-compiled code in this process
3069254721Semaste    /// is possible.
3070254721Semaste    ///
3071254721Semaste    /// @return
3072254721Semaste    ///     True if execution of JIT code is possible; false otherwise.
3073254721Semaste    //------------------------------------------------------------------
3074254721Semaste    bool CanJIT ();
3075254721Semaste
3076254721Semaste    //------------------------------------------------------------------
3077254721Semaste    /// Sets whether executing JIT-compiled code in this process
3078254721Semaste    /// is possible.
3079254721Semaste    ///
3080254721Semaste    /// @param[in] can_jit
3081254721Semaste    ///     True if execution of JIT code is possible; false otherwise.
3082254721Semaste    //------------------------------------------------------------------
3083254721Semaste    void SetCanJIT (bool can_jit);
3084254721Semaste
3085254721Semaste    //------------------------------------------------------------------
3086254721Semaste    /// Actually deallocate memory in the process.
3087254721Semaste    ///
3088254721Semaste    /// This function will deallocate memory in the process's address
3089254721Semaste    /// space that was allocated with AllocateMemory.
3090254721Semaste    ///
3091254721Semaste    /// @param[in] ptr
3092254721Semaste    ///     A return value from AllocateMemory, pointing to the memory you
3093254721Semaste    ///     want to deallocate.
3094254721Semaste    ///
3095254721Semaste    /// @return
3096254721Semaste    ///     \btrue if the memory was deallocated, \bfalse otherwise.
3097254721Semaste    //------------------------------------------------------------------
3098254721Semaste
3099254721Semaste    virtual Error
3100254721Semaste    DoDeallocateMemory (lldb::addr_t ptr)
3101254721Semaste    {
3102254721Semaste        Error error;
3103254721Semaste        error.SetErrorStringWithFormat("error: %s does not support deallocating in the debug process", GetPluginName().GetCString());
3104254721Semaste        return error;
3105254721Semaste    }
3106254721Semaste
3107254721Semaste
3108254721Semaste    //------------------------------------------------------------------
3109254721Semaste    /// The public interface to deallocating memory in the process.
3110254721Semaste    ///
3111254721Semaste    /// This function will deallocate memory in the process's address
3112254721Semaste    /// space that was allocated with AllocateMemory.
3113254721Semaste    ///
3114254721Semaste    /// @param[in] ptr
3115254721Semaste    ///     A return value from AllocateMemory, pointing to the memory you
3116254721Semaste    ///     want to deallocate.
3117254721Semaste    ///
3118254721Semaste    /// @return
3119254721Semaste    ///     \btrue if the memory was deallocated, \bfalse otherwise.
3120254721Semaste    //------------------------------------------------------------------
3121254721Semaste
3122254721Semaste    Error
3123254721Semaste    DeallocateMemory (lldb::addr_t ptr);
3124254721Semaste
3125254721Semaste    //------------------------------------------------------------------
3126254721Semaste    /// Get any available STDOUT.
3127254721Semaste    ///
3128254721Semaste    /// If the process was launched without supplying valid file paths
3129254721Semaste    /// for stdin, stdout, and stderr, then the Process class might
3130254721Semaste    /// try to cache the STDOUT for the process if it is able. Events
3131254721Semaste    /// will be queued indicating that there is STDOUT available that
3132254721Semaste    /// can be retrieved using this function.
3133254721Semaste    ///
3134254721Semaste    /// @param[out] buf
3135254721Semaste    ///     A buffer that will receive any STDOUT bytes that are
3136254721Semaste    ///     currently available.
3137254721Semaste    ///
3138254721Semaste    /// @param[out] buf_size
3139254721Semaste    ///     The size in bytes for the buffer \a buf.
3140254721Semaste    ///
3141254721Semaste    /// @return
3142254721Semaste    ///     The number of bytes written into \a buf. If this value is
3143254721Semaste    ///     equal to \a buf_size, another call to this function should
3144254721Semaste    ///     be made to retrieve more STDOUT data.
3145254721Semaste    //------------------------------------------------------------------
3146254721Semaste    virtual size_t
3147254721Semaste    GetSTDOUT (char *buf, size_t buf_size, Error &error);
3148254721Semaste
3149254721Semaste    //------------------------------------------------------------------
3150254721Semaste    /// Get any available STDERR.
3151254721Semaste    ///
3152254721Semaste    /// If the process was launched without supplying valid file paths
3153254721Semaste    /// for stdin, stdout, and stderr, then the Process class might
3154254721Semaste    /// try to cache the STDERR for the process if it is able. Events
3155254721Semaste    /// will be queued indicating that there is STDERR available that
3156254721Semaste    /// can be retrieved using this function.
3157254721Semaste    ///
3158254721Semaste    /// @param[out] buf
3159254721Semaste    ///     A buffer that will receive any STDERR bytes that are
3160254721Semaste    ///     currently available.
3161254721Semaste    ///
3162254721Semaste    /// @param[out] buf_size
3163254721Semaste    ///     The size in bytes for the buffer \a buf.
3164254721Semaste    ///
3165254721Semaste    /// @return
3166254721Semaste    ///     The number of bytes written into \a buf. If this value is
3167254721Semaste    ///     equal to \a buf_size, another call to this function should
3168254721Semaste    ///     be made to retrieve more STDERR data.
3169254721Semaste    //------------------------------------------------------------------
3170254721Semaste    virtual size_t
3171254721Semaste    GetSTDERR (char *buf, size_t buf_size, Error &error);
3172254721Semaste
3173254721Semaste    virtual size_t
3174254721Semaste    PutSTDIN (const char *buf, size_t buf_size, Error &error)
3175254721Semaste    {
3176254721Semaste        error.SetErrorString("stdin unsupported");
3177254721Semaste        return 0;
3178254721Semaste    }
3179254721Semaste
3180254721Semaste    //------------------------------------------------------------------
3181254721Semaste    /// Get any available profile data.
3182254721Semaste    ///
3183254721Semaste    /// @param[out] buf
3184254721Semaste    ///     A buffer that will receive any profile data bytes that are
3185254721Semaste    ///     currently available.
3186254721Semaste    ///
3187254721Semaste    /// @param[out] buf_size
3188254721Semaste    ///     The size in bytes for the buffer \a buf.
3189254721Semaste    ///
3190254721Semaste    /// @return
3191254721Semaste    ///     The number of bytes written into \a buf. If this value is
3192254721Semaste    ///     equal to \a buf_size, another call to this function should
3193254721Semaste    ///     be made to retrieve more profile data.
3194254721Semaste    //------------------------------------------------------------------
3195254721Semaste    virtual size_t
3196254721Semaste    GetAsyncProfileData (char *buf, size_t buf_size, Error &error);
3197254721Semaste
3198254721Semaste    //----------------------------------------------------------------------
3199254721Semaste    // Process Breakpoints
3200254721Semaste    //----------------------------------------------------------------------
3201254721Semaste    size_t
3202254721Semaste    GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site);
3203254721Semaste
3204254721Semaste    virtual Error
3205254721Semaste    EnableBreakpointSite (BreakpointSite *bp_site)
3206254721Semaste    {
3207254721Semaste        Error error;
3208254721Semaste        error.SetErrorStringWithFormat("error: %s does not support enabling breakpoints", GetPluginName().GetCString());
3209254721Semaste        return error;
3210254721Semaste    }
3211254721Semaste
3212254721Semaste
3213254721Semaste    virtual Error
3214254721Semaste    DisableBreakpointSite (BreakpointSite *bp_site)
3215254721Semaste    {
3216254721Semaste        Error error;
3217254721Semaste        error.SetErrorStringWithFormat("error: %s does not support disabling breakpoints", GetPluginName().GetCString());
3218254721Semaste        return error;
3219254721Semaste    }
3220254721Semaste
3221254721Semaste
3222254721Semaste    // This is implemented completely using the lldb::Process API. Subclasses
3223254721Semaste    // don't need to implement this function unless the standard flow of
3224254721Semaste    // read existing opcode, write breakpoint opcode, verify breakpoint opcode
3225254721Semaste    // doesn't work for a specific process plug-in.
3226254721Semaste    virtual Error
3227254721Semaste    EnableSoftwareBreakpoint (BreakpointSite *bp_site);
3228254721Semaste
3229254721Semaste    // This is implemented completely using the lldb::Process API. Subclasses
3230254721Semaste    // don't need to implement this function unless the standard flow of
3231254721Semaste    // restoring original opcode in memory and verifying the restored opcode
3232254721Semaste    // doesn't work for a specific process plug-in.
3233254721Semaste    virtual Error
3234254721Semaste    DisableSoftwareBreakpoint (BreakpointSite *bp_site);
3235254721Semaste
3236254721Semaste    BreakpointSiteList &
3237254721Semaste    GetBreakpointSiteList();
3238254721Semaste
3239254721Semaste    const BreakpointSiteList &
3240254721Semaste    GetBreakpointSiteList() const;
3241254721Semaste
3242254721Semaste    void
3243254721Semaste    DisableAllBreakpointSites ();
3244254721Semaste
3245254721Semaste    Error
3246254721Semaste    ClearBreakpointSiteByID (lldb::user_id_t break_id);
3247254721Semaste
3248254721Semaste    lldb::break_id_t
3249254721Semaste    CreateBreakpointSite (const lldb::BreakpointLocationSP &owner,
3250254721Semaste                          bool use_hardware);
3251254721Semaste
3252254721Semaste    Error
3253254721Semaste    DisableBreakpointSiteByID (lldb::user_id_t break_id);
3254254721Semaste
3255254721Semaste    Error
3256254721Semaste    EnableBreakpointSiteByID (lldb::user_id_t break_id);
3257254721Semaste
3258254721Semaste
3259254721Semaste    // BreakpointLocations use RemoveOwnerFromBreakpointSite to remove
3260254721Semaste    // themselves from the owner's list of this breakpoint sites.
3261254721Semaste    void
3262254721Semaste    RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id,
3263254721Semaste                                   lldb::user_id_t owner_loc_id,
3264254721Semaste                                   lldb::BreakpointSiteSP &bp_site_sp);
3265254721Semaste
3266254721Semaste    //----------------------------------------------------------------------
3267254721Semaste    // Process Watchpoints (optional)
3268254721Semaste    //----------------------------------------------------------------------
3269254721Semaste    virtual Error
3270254721Semaste    EnableWatchpoint (Watchpoint *wp, bool notify = true);
3271254721Semaste
3272254721Semaste    virtual Error
3273254721Semaste    DisableWatchpoint (Watchpoint *wp, bool notify = true);
3274254721Semaste
3275254721Semaste    //------------------------------------------------------------------
3276254721Semaste    // Thread Queries
3277254721Semaste    //------------------------------------------------------------------
3278254721Semaste    virtual bool
3279254721Semaste    UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) = 0;
3280254721Semaste
3281254721Semaste    void
3282254721Semaste    UpdateThreadListIfNeeded ();
3283254721Semaste
3284254721Semaste    ThreadList &
3285254721Semaste    GetThreadList ()
3286254721Semaste    {
3287254721Semaste        return m_thread_list;
3288254721Semaste    }
3289254721Semaste
3290254721Semaste    uint32_t
3291254721Semaste    GetNextThreadIndexID (uint64_t thread_id);
3292254721Semaste
3293254721Semaste    lldb::ThreadSP
3294254721Semaste    CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context);
3295254721Semaste
3296254721Semaste    // Returns true if an index id has been assigned to a thread.
3297254721Semaste    bool
3298254721Semaste    HasAssignedIndexIDToThread(uint64_t sb_thread_id);
3299254721Semaste
3300254721Semaste    // Given a thread_id, it will assign a more reasonable index id for display to the user.
3301254721Semaste    // If the thread_id has previously been assigned, the same index id will be used.
3302254721Semaste    uint32_t
3303254721Semaste    AssignIndexIDToThread(uint64_t thread_id);
3304254721Semaste
3305254721Semaste    //------------------------------------------------------------------
3306254721Semaste    // Event Handling
3307254721Semaste    //------------------------------------------------------------------
3308254721Semaste    lldb::StateType
3309254721Semaste    GetNextEvent (lldb::EventSP &event_sp);
3310254721Semaste
3311254721Semaste    lldb::StateType
3312254721Semaste    WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr = NULL);
3313254721Semaste
3314254721Semaste    lldb::StateType
3315254721Semaste    WaitForStateChangedEvents (const TimeValue *timeout, lldb::EventSP &event_sp);
3316254721Semaste
3317254721Semaste    Event *
3318254721Semaste    PeekAtStateChangedEvents ();
3319254721Semaste
3320254721Semaste
3321254721Semaste    class
3322254721Semaste    ProcessEventHijacker
3323254721Semaste    {
3324254721Semaste    public:
3325254721Semaste        ProcessEventHijacker (Process &process, Listener *listener) :
3326254721Semaste            m_process (process)
3327254721Semaste        {
3328254721Semaste            m_process.HijackProcessEvents (listener);
3329254721Semaste        }
3330254721Semaste        ~ProcessEventHijacker ()
3331254721Semaste        {
3332254721Semaste            m_process.RestoreProcessEvents();
3333254721Semaste        }
3334254721Semaste
3335254721Semaste    private:
3336254721Semaste        Process &m_process;
3337254721Semaste    };
3338254721Semaste    friend class ProcessEventHijacker;
3339254721Semaste    //------------------------------------------------------------------
3340254721Semaste    /// If you need to ensure that you and only you will hear about some public
3341254721Semaste    /// event, then make a new listener, set to listen to process events, and
3342254721Semaste    /// then call this with that listener.  Then you will have to wait on that
3343254721Semaste    /// listener explicitly for events (rather than using the GetNextEvent & WaitFor*
3344254721Semaste    /// calls above.  Be sure to call RestoreProcessEvents when you are done.
3345254721Semaste    ///
3346254721Semaste    /// @param[in] listener
3347254721Semaste    ///     This is the new listener to whom all process events will be delivered.
3348254721Semaste    ///
3349254721Semaste    /// @return
3350254721Semaste    ///     Returns \b true if the new listener could be installed,
3351254721Semaste    ///     \b false otherwise.
3352254721Semaste    //------------------------------------------------------------------
3353254721Semaste    bool
3354254721Semaste    HijackProcessEvents (Listener *listener);
3355254721Semaste
3356254721Semaste    //------------------------------------------------------------------
3357254721Semaste    /// Restores the process event broadcasting to its normal state.
3358254721Semaste    ///
3359254721Semaste    //------------------------------------------------------------------
3360254721Semaste    void
3361254721Semaste    RestoreProcessEvents ();
3362254721Semaste
3363254721Semasteprivate:
3364254721Semaste    //------------------------------------------------------------------
3365254721Semaste    /// This is the part of the event handling that for a process event.
3366254721Semaste    /// It decides what to do with the event and returns true if the
3367254721Semaste    /// event needs to be propagated to the user, and false otherwise.
3368254721Semaste    /// If the event is not propagated, this call will most likely set
3369254721Semaste    /// the target to executing again.
3370254721Semaste    /// There is only one place where this call should be called, HandlePrivateEvent.
3371254721Semaste    /// Don't call it from anywhere else...
3372254721Semaste    ///
3373254721Semaste    /// @param[in] event_ptr
3374254721Semaste    ///     This is the event we are handling.
3375254721Semaste    ///
3376254721Semaste    /// @return
3377254721Semaste    ///     Returns \b true if the event should be reported to the
3378254721Semaste    ///     user, \b false otherwise.
3379254721Semaste    //------------------------------------------------------------------
3380254721Semaste    bool
3381254721Semaste    ShouldBroadcastEvent (Event *event_ptr);
3382254721Semaste
3383254721Semastepublic:
3384254721Semaste    const lldb::ABISP &
3385254721Semaste    GetABI ();
3386254721Semaste
3387254721Semaste    OperatingSystem *
3388254721Semaste    GetOperatingSystem ()
3389254721Semaste    {
3390254721Semaste        return m_os_ap.get();
3391254721Semaste    }
3392254721Semaste
3393254721Semaste
3394254721Semaste    virtual LanguageRuntime *
3395254721Semaste    GetLanguageRuntime (lldb::LanguageType language, bool retry_if_null = true);
3396254721Semaste
3397254721Semaste    virtual CPPLanguageRuntime *
3398254721Semaste    GetCPPLanguageRuntime (bool retry_if_null = true);
3399254721Semaste
3400254721Semaste    virtual ObjCLanguageRuntime *
3401254721Semaste    GetObjCLanguageRuntime (bool retry_if_null = true);
3402254721Semaste
3403254721Semaste    bool
3404254721Semaste    IsPossibleDynamicValue (ValueObject& in_value);
3405254721Semaste
3406254721Semaste    bool
3407254721Semaste    IsRunning () const;
3408254721Semaste
3409254721Semaste    DynamicCheckerFunctions *GetDynamicCheckers()
3410254721Semaste    {
3411254721Semaste        return m_dynamic_checkers_ap.get();
3412254721Semaste    }
3413254721Semaste
3414254721Semaste    void SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers)
3415254721Semaste    {
3416254721Semaste        m_dynamic_checkers_ap.reset(dynamic_checkers);
3417254721Semaste    }
3418254721Semaste
3419254721Semaste    //------------------------------------------------------------------
3420254721Semaste    /// Call this to set the lldb in the mode where it breaks on new thread
3421254721Semaste    /// creations, and then auto-restarts.  This is useful when you are trying
3422254721Semaste    /// to run only one thread, but either that thread or the kernel is creating
3423254721Semaste    /// new threads in the process.  If you stop when the thread is created, you
3424254721Semaste    /// can immediately suspend it, and keep executing only the one thread you intend.
3425254721Semaste    ///
3426254721Semaste    /// @return
3427254721Semaste    ///     Returns \b true if we were able to start up the notification
3428254721Semaste    ///     \b false otherwise.
3429254721Semaste    //------------------------------------------------------------------
3430254721Semaste    virtual bool
3431254721Semaste    StartNoticingNewThreads()
3432254721Semaste    {
3433254721Semaste        return true;
3434254721Semaste    }
3435254721Semaste
3436254721Semaste    //------------------------------------------------------------------
3437254721Semaste    /// Call this to turn off the stop & notice new threads mode.
3438254721Semaste    ///
3439254721Semaste    /// @return
3440254721Semaste    ///     Returns \b true if we were able to start up the notification
3441254721Semaste    ///     \b false otherwise.
3442254721Semaste    //------------------------------------------------------------------
3443254721Semaste    virtual bool
3444254721Semaste    StopNoticingNewThreads()
3445254721Semaste    {
3446254721Semaste        return true;
3447254721Semaste    }
3448254721Semaste
3449254721Semaste    void
3450254721Semaste    SetRunningUserExpression (bool on);
3451254721Semaste
3452254721Semaste    //------------------------------------------------------------------
3453254721Semaste    // lldb::ExecutionContextScope pure virtual functions
3454254721Semaste    //------------------------------------------------------------------
3455254721Semaste    virtual lldb::TargetSP
3456254721Semaste    CalculateTarget ();
3457254721Semaste
3458254721Semaste    virtual lldb::ProcessSP
3459254721Semaste    CalculateProcess ()
3460254721Semaste    {
3461254721Semaste        return shared_from_this();
3462254721Semaste    }
3463254721Semaste
3464254721Semaste    virtual lldb::ThreadSP
3465254721Semaste    CalculateThread ()
3466254721Semaste    {
3467254721Semaste        return lldb::ThreadSP();
3468254721Semaste    }
3469254721Semaste
3470254721Semaste    virtual lldb::StackFrameSP
3471254721Semaste    CalculateStackFrame ()
3472254721Semaste    {
3473254721Semaste        return lldb::StackFrameSP();
3474254721Semaste    }
3475254721Semaste
3476254721Semaste    virtual void
3477254721Semaste    CalculateExecutionContext (ExecutionContext &exe_ctx);
3478254721Semaste
3479254721Semaste    void
3480254721Semaste    SetSTDIOFileDescriptor (int file_descriptor);
3481254721Semaste
3482254721Semaste    //------------------------------------------------------------------
3483254721Semaste    // Add a permanent region of memory that should never be read or
3484254721Semaste    // written to. This can be used to ensure that memory reads or writes
3485254721Semaste    // to certain areas of memory never end up being sent to the
3486254721Semaste    // DoReadMemory or DoWriteMemory functions which can improve
3487254721Semaste    // performance.
3488254721Semaste    //------------------------------------------------------------------
3489254721Semaste    void
3490254721Semaste    AddInvalidMemoryRegion (const LoadRange &region);
3491254721Semaste
3492254721Semaste    //------------------------------------------------------------------
3493254721Semaste    // Remove a permanent region of memory that should never be read or
3494254721Semaste    // written to that was previously added with AddInvalidMemoryRegion.
3495254721Semaste    //------------------------------------------------------------------
3496254721Semaste    bool
3497254721Semaste    RemoveInvalidMemoryRange (const LoadRange &region);
3498254721Semaste
3499254721Semaste    //------------------------------------------------------------------
3500254721Semaste    // If the setup code of a thread plan needs to do work that might involve
3501254721Semaste    // calling a function in the target, it should not do that work directly
3502254721Semaste    // in one of the thread plan functions (DidPush/WillResume) because
3503254721Semaste    // such work needs to be handled carefully.  Instead, put that work in
3504254721Semaste    // a PreResumeAction callback, and register it with the process.  It will
3505254721Semaste    // get done before the actual "DoResume" gets called.
3506254721Semaste    //------------------------------------------------------------------
3507254721Semaste
3508254721Semaste    typedef bool (PreResumeActionCallback)(void *);
3509254721Semaste
3510254721Semaste    void
3511254721Semaste    AddPreResumeAction (PreResumeActionCallback callback, void *baton);
3512254721Semaste
3513254721Semaste    bool
3514254721Semaste    RunPreResumeActions ();
3515254721Semaste
3516254721Semaste    void
3517254721Semaste    ClearPreResumeActions ();
3518254721Semaste
3519254721Semaste    ProcessRunLock &
3520254721Semaste    GetRunLock ()
3521254721Semaste    {
3522254721Semaste        if (Host::GetCurrentThread() == m_private_state_thread)
3523254721Semaste            return m_private_run_lock;
3524254721Semaste        else
3525254721Semaste            return m_public_run_lock;
3526254721Semaste    }
3527254721Semaste
3528254721Semasteprotected:
3529254721Semaste    //------------------------------------------------------------------
3530254721Semaste    // NextEventAction provides a way to register an action on the next
3531254721Semaste    // event that is delivered to this process.  There is currently only
3532254721Semaste    // one next event action allowed in the process at one time.  If a
3533254721Semaste    // new "NextEventAction" is added while one is already present, the
3534254721Semaste    // old action will be discarded (with HandleBeingUnshipped called
3535254721Semaste    // after it is discarded.)
3536254721Semaste    //
3537254721Semaste    // If you want to resume the process as a result of a resume action,
3538254721Semaste    // call RequestResume, don't call Resume directly.
3539254721Semaste    //------------------------------------------------------------------
3540254721Semaste    class NextEventAction
3541254721Semaste    {
3542254721Semaste    public:
3543254721Semaste        typedef enum EventActionResult
3544254721Semaste        {
3545254721Semaste            eEventActionSuccess,
3546254721Semaste            eEventActionRetry,
3547254721Semaste            eEventActionExit
3548254721Semaste        } EventActionResult;
3549254721Semaste
3550254721Semaste        NextEventAction (Process *process) :
3551254721Semaste            m_process(process)
3552254721Semaste        {
3553254721Semaste        }
3554254721Semaste
3555254721Semaste        virtual
3556254721Semaste        ~NextEventAction()
3557254721Semaste        {
3558254721Semaste        }
3559254721Semaste
3560254721Semaste        virtual EventActionResult PerformAction (lldb::EventSP &event_sp) = 0;
3561254721Semaste        virtual void HandleBeingUnshipped () {}
3562254721Semaste        virtual EventActionResult HandleBeingInterrupted () = 0;
3563254721Semaste        virtual const char *GetExitString() = 0;
3564254721Semaste        void RequestResume()
3565254721Semaste        {
3566254721Semaste            m_process->m_resume_requested = true;
3567254721Semaste        }
3568254721Semaste    protected:
3569254721Semaste        Process *m_process;
3570254721Semaste    };
3571254721Semaste
3572254721Semaste    void SetNextEventAction (Process::NextEventAction *next_event_action)
3573254721Semaste    {
3574254721Semaste        if (m_next_event_action_ap.get())
3575254721Semaste            m_next_event_action_ap->HandleBeingUnshipped();
3576254721Semaste
3577254721Semaste        m_next_event_action_ap.reset(next_event_action);
3578254721Semaste    }
3579254721Semaste
3580254721Semaste    // This is the completer for Attaching:
3581254721Semaste    class AttachCompletionHandler : public NextEventAction
3582254721Semaste    {
3583254721Semaste    public:
3584254721Semaste        AttachCompletionHandler (Process *process, uint32_t exec_count) :
3585254721Semaste            NextEventAction (process),
3586254721Semaste            m_exec_count (exec_count)
3587254721Semaste        {
3588254721Semaste        }
3589254721Semaste
3590254721Semaste        virtual
3591254721Semaste        ~AttachCompletionHandler()
3592254721Semaste        {
3593254721Semaste        }
3594254721Semaste
3595254721Semaste        virtual EventActionResult PerformAction (lldb::EventSP &event_sp);
3596254721Semaste        virtual EventActionResult HandleBeingInterrupted ();
3597254721Semaste        virtual const char *GetExitString();
3598254721Semaste    private:
3599254721Semaste        uint32_t m_exec_count;
3600254721Semaste        std::string m_exit_string;
3601254721Semaste    };
3602254721Semaste
3603254721Semaste    bool
3604254721Semaste    HijackPrivateProcessEvents (Listener *listener);
3605254721Semaste
3606254721Semaste    void
3607254721Semaste    RestorePrivateProcessEvents ();
3608254721Semaste
3609254721Semaste    bool
3610254721Semaste    PrivateStateThreadIsValid () const
3611254721Semaste    {
3612254721Semaste        return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
3613254721Semaste    }
3614254721Semaste
3615254721Semaste    //------------------------------------------------------------------
3616254721Semaste    // Type definitions
3617254721Semaste    //------------------------------------------------------------------
3618254721Semaste    typedef std::map<lldb::LanguageType, lldb::LanguageRuntimeSP> LanguageRuntimeCollection;
3619254721Semaste
3620254721Semaste    struct PreResumeCallbackAndBaton
3621254721Semaste    {
3622254721Semaste        bool (*callback) (void *);
3623254721Semaste        void *baton;
3624254721Semaste        PreResumeCallbackAndBaton (PreResumeActionCallback in_callback, void *in_baton) :
3625254721Semaste            callback (in_callback),
3626254721Semaste            baton (in_baton)
3627254721Semaste        {
3628254721Semaste        }
3629254721Semaste    };
3630254721Semaste
3631254721Semaste    //------------------------------------------------------------------
3632254721Semaste    // Member variables
3633254721Semaste    //------------------------------------------------------------------
3634254721Semaste    Target &                    m_target;               ///< The target that owns this process.
3635254721Semaste    ThreadSafeValue<lldb::StateType>  m_public_state;
3636254721Semaste    ThreadSafeValue<lldb::StateType>  m_private_state; // The actual state of our process
3637254721Semaste    Broadcaster                 m_private_state_broadcaster;  // This broadcaster feeds state changed events into the private state thread's listener.
3638254721Semaste    Broadcaster                 m_private_state_control_broadcaster; // This is the control broadcaster, used to pause, resume & stop the private state thread.
3639254721Semaste    Listener                    m_private_state_listener;     // This is the listener for the private state thread.
3640254721Semaste    Predicate<bool>             m_private_state_control_wait; /// This Predicate is used to signal that a control operation is complete.
3641254721Semaste    lldb::thread_t              m_private_state_thread;  // Thread ID for the thread that watches interal state events
3642254721Semaste    ProcessModID                m_mod_id;               ///< Tracks the state of the process over stops and other alterations.
3643254721Semaste    uint32_t                    m_process_unique_id;    ///< Each lldb_private::Process class that is created gets a unique integer ID that increments with each new instance
3644254721Semaste    uint32_t                    m_thread_index_id;      ///< Each thread is created with a 1 based index that won't get re-used.
3645254721Semaste    std::map<uint64_t, uint32_t> m_thread_id_to_index_id_map;
3646254721Semaste    int                         m_exit_status;          ///< The exit status of the process, or -1 if not set.
3647254721Semaste    std::string                 m_exit_string;          ///< A textual description of why a process exited.
3648254721Semaste    Mutex                       m_thread_mutex;
3649254721Semaste    ThreadList                  m_thread_list_real;     ///< The threads for this process as are known to the protocol we are debugging with
3650254721Semaste    ThreadList                  m_thread_list;          ///< The threads for this process as the user will see them. This is usually the same as
3651254721Semaste                                                        ///< m_thread_list_real, but might be different if there is an OS plug-in creating memory threads
3652254721Semaste    std::vector<Notifications>  m_notifications;        ///< The list of notifications that this process can deliver.
3653254721Semaste    std::vector<lldb::addr_t>   m_image_tokens;
3654254721Semaste    Listener                    &m_listener;
3655254721Semaste    BreakpointSiteList          m_breakpoint_site_list; ///< This is the list of breakpoint locations we intend to insert in the target.
3656254721Semaste    std::unique_ptr<DynamicLoader> m_dyld_ap;
3657254721Semaste    std::unique_ptr<DynamicCheckerFunctions> m_dynamic_checkers_ap; ///< The functions used by the expression parser to validate data that expressions use.
3658254721Semaste    std::unique_ptr<OperatingSystem> m_os_ap;
3659254721Semaste    UnixSignals                 m_unix_signals;         /// This is the current signal set for this process.
3660254721Semaste    lldb::ABISP                 m_abi_sp;
3661254721Semaste    lldb::InputReaderSP         m_process_input_reader;
3662254721Semaste    Communication               m_stdio_communication;
3663254721Semaste    Mutex                       m_stdio_communication_mutex;
3664254721Semaste    std::string                 m_stdout_data;
3665254721Semaste    std::string                 m_stderr_data;
3666254721Semaste    Mutex                       m_profile_data_comm_mutex;
3667254721Semaste    std::vector<std::string>    m_profile_data;
3668254721Semaste    MemoryCache                 m_memory_cache;
3669254721Semaste    AllocatedMemoryCache        m_allocated_memory_cache;
3670254721Semaste    bool                        m_should_detach;   /// Should we detach if the process object goes away with an explicit call to Kill or Detach?
3671254721Semaste    LanguageRuntimeCollection   m_language_runtimes;
3672254721Semaste    std::unique_ptr<NextEventAction> m_next_event_action_ap;
3673254721Semaste    std::vector<PreResumeCallbackAndBaton> m_pre_resume_actions;
3674254721Semaste    ProcessRunLock              m_public_run_lock;
3675254721Semaste    ProcessRunLock              m_private_run_lock;
3676254721Semaste    Predicate<bool>             m_currently_handling_event; // This predicate is set in HandlePrivateEvent while all its business is being done.
3677254721Semaste    bool                        m_currently_handling_do_on_removals;
3678254721Semaste    bool                        m_resume_requested;         // If m_currently_handling_event or m_currently_handling_do_on_removals are true, Resume will only request a resume, using this flag to check.
3679254721Semaste    bool                        m_finalize_called;
3680254721Semaste    bool                        m_clear_thread_plans_on_stop;
3681254721Semaste    lldb::StateType             m_last_broadcast_state;   /// This helps with the Public event coalescing in ShouldBroadcastEvent.
3682254721Semaste    bool m_destroy_in_process;
3683254721Semaste
3684254721Semaste    enum {
3685254721Semaste        eCanJITDontKnow= 0,
3686254721Semaste        eCanJITYes,
3687254721Semaste        eCanJITNo
3688254721Semaste    } m_can_jit;
3689254721Semaste
3690254721Semaste    size_t
3691254721Semaste    RemoveBreakpointOpcodesFromBuffer (lldb::addr_t addr, size_t size, uint8_t *buf) const;
3692254721Semaste
3693254721Semaste    void
3694254721Semaste    SynchronouslyNotifyStateChanged (lldb::StateType state);
3695254721Semaste
3696254721Semaste    void
3697254721Semaste    SetPublicState (lldb::StateType new_state, bool restarted);
3698254721Semaste
3699254721Semaste    void
3700254721Semaste    SetPrivateState (lldb::StateType state);
3701254721Semaste
3702254721Semaste    bool
3703254721Semaste    StartPrivateStateThread (bool force = false);
3704254721Semaste
3705254721Semaste    void
3706254721Semaste    StopPrivateStateThread ();
3707254721Semaste
3708254721Semaste    void
3709254721Semaste    PausePrivateStateThread ();
3710254721Semaste
3711254721Semaste    void
3712254721Semaste    ResumePrivateStateThread ();
3713254721Semaste
3714254721Semaste    static void *
3715254721Semaste    PrivateStateThread (void *arg);
3716254721Semaste
3717254721Semaste    void *
3718254721Semaste    RunPrivateStateThread ();
3719254721Semaste
3720254721Semaste    void
3721254721Semaste    HandlePrivateEvent (lldb::EventSP &event_sp);
3722254721Semaste
3723254721Semaste    lldb::StateType
3724254721Semaste    WaitForProcessStopPrivate (const TimeValue *timeout, lldb::EventSP &event_sp);
3725254721Semaste
3726254721Semaste    // This waits for both the state change broadcaster, and the control broadcaster.
3727254721Semaste    // If control_only, it only waits for the control broadcaster.
3728254721Semaste
3729254721Semaste    bool
3730254721Semaste    WaitForEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp, bool control_only);
3731254721Semaste
3732254721Semaste    lldb::StateType
3733254721Semaste    WaitForStateChangedEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp);
3734254721Semaste
3735254721Semaste    lldb::StateType
3736254721Semaste    WaitForState (const TimeValue *timeout,
3737254721Semaste                  const lldb::StateType *match_states,
3738254721Semaste                  const uint32_t num_match_states);
3739254721Semaste
3740254721Semaste    size_t
3741254721Semaste    WriteMemoryPrivate (lldb::addr_t addr, const void *buf, size_t size, Error &error);
3742254721Semaste
3743254721Semaste    void
3744254721Semaste    AppendSTDOUT (const char *s, size_t len);
3745254721Semaste
3746254721Semaste    void
3747254721Semaste    AppendSTDERR (const char *s, size_t len);
3748254721Semaste
3749254721Semaste    void
3750254721Semaste    BroadcastAsyncProfileData(const std::string &one_profile_data);
3751254721Semaste
3752254721Semaste    static void
3753254721Semaste    STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
3754254721Semaste
3755254721Semaste    void
3756254721Semaste    PushProcessInputReader ();
3757254721Semaste
3758254721Semaste    void
3759254721Semaste    PopProcessInputReader ();
3760254721Semaste
3761254721Semaste    void
3762254721Semaste    ResetProcessInputReader ();
3763254721Semaste
3764254721Semaste    static size_t
3765254721Semaste    ProcessInputReaderCallback (void *baton,
3766254721Semaste                                InputReader &reader,
3767254721Semaste                                lldb::InputReaderAction notification,
3768254721Semaste                                const char *bytes,
3769254721Semaste                                size_t bytes_len);
3770254721Semaste
3771254721Semaste    Error
3772254721Semaste    HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp);
3773254721Semaste
3774254721Semasteprivate:
3775254721Semaste    //------------------------------------------------------------------
3776254721Semaste    // For Process only
3777254721Semaste    //------------------------------------------------------------------
3778254721Semaste    void ControlPrivateStateThread (uint32_t signal);
3779254721Semaste
3780254721Semaste    DISALLOW_COPY_AND_ASSIGN (Process);
3781254721Semaste
3782254721Semaste};
3783254721Semaste
3784254721Semaste} // namespace lldb_private
3785254721Semaste
3786254721Semaste#endif  // liblldb_Process_h_
3787