1//===-- RNBContext.h --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Created by Greg Clayton on 12/12/07.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H
14#define LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H
15
16#include "DNBError.h"
17#include "PThreadEvent.h"
18#include "RNBDefs.h"
19#include <string>
20#include <vector>
21
22class RNBContext {
23public:
24 using IgnoredExceptions = std::vector<exception_mask_t>;
25 enum {
26 event_proc_state_changed = 0x001,
27 event_proc_thread_running = 0x002, // Sticky
28 event_proc_thread_exiting = 0x004,
29 event_proc_stdio_available = 0x008,
30 event_proc_profile_data = 0x010,
31 event_read_packet_available = 0x020,
32 event_read_thread_running = 0x040, // Sticky
33 event_read_thread_exiting = 0x080,
34
35 normal_event_bits = event_proc_state_changed | event_proc_thread_exiting |
36 event_proc_stdio_available | event_proc_profile_data |
37 event_read_packet_available |
38 event_read_thread_exiting ,
39
40 sticky_event_bits = event_proc_thread_running | event_read_thread_running,
41
42 all_event_bits = sticky_event_bits | normal_event_bits
43 } event_t;
44 // Constructors and Destructors
45 RNBContext() = default;
46 virtual ~RNBContext();
47
48 nub_process_t ProcessID() const { return m_pid; }
49 bool HasValidProcessID() const { return m_pid != INVALID_NUB_PROCESS; }
50 void SetProcessID(nub_process_t pid);
51 nub_size_t GetProcessStopCount() const { return m_pid_stop_count; }
52 bool SetProcessStopCount(nub_size_t count) {
53 // Returns true if this class' notion of the PID state changed
54 if (m_pid_stop_count == count)
55 return false; // Didn't change
56 m_pid_stop_count = count;
57 return true; // The stop count has changed.
58 }
59
60 bool ProcessStateRunning() const;
61 PThreadEvent &Events() { return m_events; }
62 nub_event_t AllEventBits() const { return all_event_bits; }
63 nub_event_t NormalEventBits() const { return normal_event_bits; }
64 nub_event_t StickyEventBits() const { return sticky_event_bits; }
65 const char *EventsAsString(nub_event_t events, std::string &s);
66
67 size_t ArgumentCount() const { return m_arg_vec.size(); }
68 const char *ArgumentAtIndex(size_t index);
69 void PushArgument(const char *arg) {
70 if (arg)
71 m_arg_vec.push_back(x: arg);
72 }
73 void ClearArgv() { m_arg_vec.erase(first: m_arg_vec.begin(), last: m_arg_vec.end()); }
74
75 size_t EnvironmentCount() const { return m_env_vec.size(); }
76 const char *EnvironmentAtIndex(size_t index);
77 void PushEnvironment(const char *arg) {
78 if (arg)
79 m_env_vec.push_back(x: arg);
80 }
81 void PushEnvironmentIfNeeded(const char *arg);
82 void ClearEnvironment() {
83 m_env_vec.erase(first: m_env_vec.begin(), last: m_env_vec.end());
84 }
85 DNBError &LaunchStatus() { return m_launch_status; }
86 const char *LaunchStatusAsString(std::string &s);
87 nub_launch_flavor_t LaunchFlavor() const { return m_launch_flavor; }
88 void SetLaunchFlavor(nub_launch_flavor_t flavor) { m_launch_flavor = flavor; }
89
90 const char *GetWorkingDirectory() const {
91 if (!m_working_directory.empty())
92 return m_working_directory.c_str();
93 return NULL;
94 }
95
96 bool SetWorkingDirectory(const char *path);
97
98 std::string &GetSTDIN() { return m_stdin; }
99 std::string &GetSTDOUT() { return m_stdout; }
100 std::string &GetSTDERR() { return m_stderr; }
101 std::string &GetWorkingDir() { return m_working_dir; }
102
103 const char *GetSTDINPath() {
104 return m_stdin.empty() ? NULL : m_stdin.c_str();
105 }
106 const char *GetSTDOUTPath() {
107 return m_stdout.empty() ? NULL : m_stdout.c_str();
108 }
109 const char *GetSTDERRPath() {
110 return m_stderr.empty() ? NULL : m_stderr.c_str();
111 }
112 const char *GetWorkingDirPath() {
113 return m_working_dir.empty() ? NULL : m_working_dir.c_str();
114 }
115
116 void PushProcessEvent(const char *p) { m_process_event.assign(s: p); }
117 const char *GetProcessEvent() { return m_process_event.c_str(); }
118
119 void SetDetachOnError(bool detach) { m_detach_on_error = detach; }
120 bool GetDetachOnError() { return m_detach_on_error; }
121
122 bool AddIgnoredException(const char *exception_name);
123
124 void AddDefaultIgnoredExceptions();
125
126 const IgnoredExceptions &GetIgnoredExceptions() {
127 return m_ignored_exceptions;
128 }
129
130protected:
131 // Classes that inherit from RNBContext can see and modify these
132 nub_process_t m_pid = INVALID_NUB_PROCESS;
133 std::string m_stdin;
134 std::string m_stdout;
135 std::string m_stderr;
136 std::string m_working_dir;
137 nub_size_t m_pid_stop_count = 0;
138 /// Threaded events that we can wait for.
139 PThreadEvent m_events{0, all_event_bits};
140 pthread_t m_pid_pthread;
141 /// How to launch our inferior process.
142 nub_launch_flavor_t m_launch_flavor = eLaunchFlavorDefault;
143 /// This holds the status from the last launch attempt.
144 DNBError m_launch_status;
145 std::vector<std::string> m_arg_vec;
146 /// This will be unparsed entries FOO=value
147 std::vector<std::string> m_env_vec;
148 std::string m_working_directory;
149 std::string m_process_event;
150 bool m_detach_on_error = false;
151 IgnoredExceptions m_ignored_exceptions;
152
153 void StartProcessStatusThread();
154 void StopProcessStatusThread();
155 static void *ThreadFunctionProcessStatus(void *arg);
156
157private:
158 RNBContext(const RNBContext &rhs) = delete;
159 RNBContext &operator=(const RNBContext &rhs) = delete;
160};
161
162#endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H
163

source code of lldb/tools/debugserver/source/RNBContext.h