-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlatformSessionImpl.cpp
More file actions
151 lines (125 loc) · 4.24 KB
/
Copy pathPlatformSessionImpl.cpp
File metadata and controls
151 lines (125 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Copyright (c) 2014-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the University of Illinois/NCSA Open
// Source License found in the LICENSE file in the root directory of this
// source tree. An additional grant of patent rights can be found in the
// PATENTS file in the same directory.
//
#include "DebugServer2/GDBRemote/PlatformSessionImpl.h"
#include "DebugServer2/GDBRemote/Session.h"
#include "DebugServer2/Host/Platform.h"
#include "DebugServer2/Host/ProcessSpawner.h"
#include "DebugServer2/Utils/Log.h"
using ds2::Host::ProcessSpawner;
namespace ds2 {
namespace GDBRemote {
PlatformSessionImplBase::PlatformSessionImplBase()
: DummySessionDelegateImpl() {}
ErrorCode PlatformSessionImplBase::onQueryProcessList(
Session &session, ProcessInfoMatch const &match, bool first,
ProcessInfo &info) const {
if (first) {
updateProcesses(match);
}
if (_processIterationState.it == _processIterationState.vals.end())
return kErrorProcessNotFound;
if (!Platform::GetProcessInfo(*_processIterationState.it++, info))
return kErrorProcessNotFound;
return kSuccess;
}
ErrorCode
PlatformSessionImplBase::onQueryProcessInfoPID(Session &, ProcessId pid,
ProcessInfo &info) const {
if (!Platform::GetProcessInfo(pid, info))
return kErrorProcessNotFound;
else
return kSuccess;
}
ErrorCode PlatformSessionImplBase::onExecuteProgram(
Session &, std::string const &command, uint32_t timeout,
std::string const &workingDirectory, ProgramResult &result) {
DS2LOG(Debug, "command='%s' timeout=%u", command.c_str(), timeout);
ProcessSpawner ps;
ps.setShellCommand(command);
ps.redirectInputToNull();
ps.redirectOutputToBuffer();
ps.redirectErrorToBuffer();
ps.setWorkingDirectory(workingDirectory);
ErrorCode error;
error = ps.run();
if (error != kSuccess)
return error;
error = ps.wait();
if (error != kSuccess)
return error;
result.status = ps.exitStatus();
result.signal = ps.signalCode();
result.output = ps.output();
return kSuccess;
}
ErrorCode PlatformSessionImplBase::onQueryUserName(Session &, UserId const &uid,
std::string &name) const {
if (!Platform::GetUserName(uid, name))
return kErrorNotFound;
else
return kSuccess;
}
ErrorCode PlatformSessionImplBase::onQueryGroupName(Session &,
GroupId const &gid,
std::string &name) const {
if (!Platform::GetGroupName(gid, name))
return kErrorNotFound;
else
return kSuccess;
}
ErrorCode PlatformSessionImplBase::onLaunchDebugServer(Session &session,
std::string const &host,
uint16_t &port,
ProcessId &pid) {
ProcessSpawner ps;
StringCollection args;
ps.setExecutable(Platform::GetSelfExecutablePath());
args.push_back("server");
if (GetLogLevel() == kLogLevelDebug) {
args.push_back("--debug");
} else if (GetLogLevel() == kLogLevelPacket) {
args.push_back("--remote-debug");
}
std::string const &outputFilename = GetLogOutputFilename();
if (outputFilename.length() > 0) {
args.push_back("--log-file");
args.push_back(outputFilename);
}
#if defined(OS_POSIX)
args.push_back("--setsid");
#endif
ps.setArguments(args);
ps.redirectInputToNull();
ps.redirectOutputToBuffer();
ErrorCode error;
error = ps.run();
if (error != kSuccess)
return error;
error = ps.wait();
if (error != kSuccess)
return error;
if (ps.exitStatus() != 0)
return kErrorInvalidArgument;
std::istringstream ss;
ss.str(ps.output());
ss >> port >> pid;
return kSuccess;
}
void PlatformSessionImplBase::updateProcesses(
ProcessInfoMatch const &match) const {
// TODO(fjricci) we should only add processes that match "match"
Platform::EnumerateProcesses(
true, UserId(), [&](ds2::ProcessInfo const &info) {
_processIterationState.vals.push_back(info.pid);
});
_processIterationState.it = _processIterationState.vals.begin();
}
} // namespace GDBRemote
} // namespace ds2