6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
+ #include < chrono>
10
+ #include < cstdarg>
11
+ #include < fstream>
12
+ #include < mutex>
13
+
9
14
#include " DAP.h"
10
15
#include " JSONUtils.h"
11
16
#include " LLDBUtils.h"
12
- #include " OutputRedirector.h"
13
- #include " lldb/API/SBBreakpoint.h"
14
17
#include " lldb/API/SBCommandInterpreter.h"
15
- #include " lldb/API/SBCommandReturnObject.h"
16
18
#include " lldb/API/SBLanguageRuntime.h"
17
19
#include " lldb/API/SBListener.h"
18
- #include " lldb/API/SBProcess.h"
19
20
#include " lldb/API/SBStream.h"
20
- #include " lldb/Host/FileSystem.h"
21
- #include " lldb/Utility/Status.h"
22
- #include " lldb/lldb-defines.h"
23
- #include " lldb/lldb-enumerations.h"
24
- #include " llvm/ADT/ArrayRef.h"
25
21
#include " llvm/ADT/StringExtras.h"
26
- #include " llvm/ADT/Twine.h"
27
- #include " llvm/Support/Error.h"
28
- #include " llvm/Support/ErrorHandling.h"
29
22
#include " llvm/Support/FormatVariadic.h"
30
- #include " llvm/Support/raw_ostream.h"
31
- #include < algorithm>
32
- #include < cassert>
33
- #include < chrono>
34
- #include < cstdarg>
35
- #include < cstdio>
36
- #include < fstream>
37
- #include < mutex>
38
- #include < utility>
39
23
40
24
#if defined(_WIN32)
41
25
#define NOMINMAX
42
26
#include < fcntl.h>
43
27
#include < io.h>
44
28
#include < windows.h>
45
- #else
46
- #include < unistd.h>
47
29
#endif
48
30
49
31
using namespace lldb_dap ;
50
32
51
- namespace {
52
- #ifdef _WIN32
53
- const char DEV_NULL[] = " nul" ;
54
- #else
55
- const char DEV_NULL[] = " /dev/null" ;
56
- #endif
57
- } // namespace
58
-
59
33
namespace lldb_dap {
60
34
61
- DAP::DAP (llvm::StringRef path, std::ofstream *log, ReplMode repl_mode,
62
- StreamDescriptor input, StreamDescriptor output)
63
- : debug_adaptor_path(path), log(log), input(std::move(input)),
64
- output (std::move(output)), broadcaster(" lldb-dap" ),
35
+ DAP::DAP (llvm::StringRef path, ReplMode repl_mode)
36
+ : debug_adaptor_path(path), broadcaster(" lldb-dap" ),
65
37
exception_breakpoints (), focus_tid(LLDB_INVALID_THREAD_ID),
66
38
stop_at_entry(false ), is_attach(false ),
67
39
enable_auto_variable_summaries(false ),
@@ -71,7 +43,21 @@ DAP::DAP(llvm::StringRef path, std::ofstream *log, ReplMode repl_mode,
71
43
configuration_done_sent(false ), waiting_for_run_in_terminal(false ),
72
44
progress_event_reporter(
73
45
[&](const ProgressEvent &event) { SendJSON (event.ToJSON ()); }),
74
- reverse_request_seq (0 ), repl_mode(repl_mode) {}
46
+ reverse_request_seq (0 ), repl_mode(repl_mode) {
47
+ const char *log_file_path = getenv (" LLDBDAP_LOG" );
48
+ #if defined(_WIN32)
49
+ // Windows opens stdout and stdin in text mode which converts \n to 13,10
50
+ // while the value is just 10 on Darwin/Linux. Setting the file mode to binary
51
+ // fixes this.
52
+ int result = _setmode (fileno (stdout), _O_BINARY);
53
+ assert (result);
54
+ result = _setmode (fileno (stdin), _O_BINARY);
55
+ UNUSED_IF_ASSERT_DISABLED (result);
56
+ assert (result);
57
+ #endif
58
+ if (log_file_path)
59
+ log.reset (new std::ofstream (log_file_path));
60
+ }
75
61
76
62
DAP::~DAP () = default ;
77
63
@@ -187,45 +173,6 @@ ExceptionBreakpoint *DAP::GetExceptionBreakpoint(const lldb::break_id_t bp_id) {
187
173
return nullptr ;
188
174
}
189
175
190
- llvm::Error DAP::ConfigureIO (std::FILE *overrideOut, std::FILE *overrideErr) {
191
- in = lldb::SBFile (std::fopen (DEV_NULL, " r" ), /* transfer_ownership=*/ true );
192
-
193
- if (auto Error = out.RedirectTo ([this ](llvm::StringRef output) {
194
- SendOutput (OutputType::Stdout, output);
195
- }))
196
- return Error;
197
-
198
- if (overrideOut) {
199
- auto fd = out.GetWriteFileDescriptor ();
200
- if (auto Error = fd.takeError ())
201
- return Error;
202
-
203
- if (dup2 (*fd, fileno (overrideOut)) == -1 )
204
- return llvm::errorCodeToError (llvm::errnoAsErrorCode ());
205
- }
206
-
207
- if (auto Error = err.RedirectTo ([this ](llvm::StringRef output) {
208
- SendOutput (OutputType::Stderr, output);
209
- }))
210
- return Error;
211
-
212
- if (overrideErr) {
213
- auto fd = err.GetWriteFileDescriptor ();
214
- if (auto Error = fd.takeError ())
215
- return Error;
216
-
217
- if (dup2 (*fd, fileno (overrideErr)) == -1 )
218
- return llvm::errorCodeToError (llvm::errnoAsErrorCode ());
219
- }
220
-
221
- return llvm::Error::success ();
222
- }
223
-
224
- void DAP::StopIO () {
225
- out.Stop ();
226
- err.Stop ();
227
- }
228
-
229
176
// Send the JSON in "json_str" to the "out" stream. Correctly send the
230
177
// "Content-Length:" field followed by the length, followed by the raw
231
178
// JSON bytes.
@@ -261,19 +208,19 @@ std::string DAP::ReadJSON() {
261
208
std::string json_str;
262
209
int length;
263
210
264
- if (!input.read_expected (log, " Content-Length: " ))
211
+ if (!input.read_expected (log. get () , " Content-Length: " ))
265
212
return json_str;
266
213
267
- if (!input.read_line (log, length_str))
214
+ if (!input.read_line (log. get () , length_str))
268
215
return json_str;
269
216
270
217
if (!llvm::to_integer (length_str, length))
271
218
return json_str;
272
219
273
- if (!input.read_expected (log, " \r\n " ))
220
+ if (!input.read_expected (log. get () , " \r\n " ))
274
221
return json_str;
275
222
276
- if (!input.read_full (log, length, json_str))
223
+ if (!input.read_full (log. get () , length, json_str))
277
224
return json_str;
278
225
279
226
if (log) {
0 commit comments