Skip to content

Commit 83eeffb

Browse files
committed
Adjusting the error handling for launches and adding extra tests for additional launch failure cases.
1 parent 3c09391 commit 83eeffb

File tree

6 files changed

+59
-22
lines changed

6 files changed

+59
-22
lines changed

lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ def test_default(self):
2727
lines = output.splitlines()
2828
self.assertIn(program, lines[0], "make sure program path is in first argument")
2929

30+
def test_failing_launch_program(self):
31+
"""
32+
Tests launching with an invalid program.
33+
"""
34+
program = self.getBuildArtifact("a.out")
35+
self.create_debug_adapter()
36+
response = self.launch(program, expectFailure=True)
37+
self.assertFalse(response["success"])
38+
self.assertEqual(
39+
"'{0}' does not exist".format(program), response["body"]["error"]["format"]
40+
)
41+
42+
def test_failing_launch_commands_and_run_in_terminal(self):
43+
"""
44+
Tests launching with an invalid program.
45+
"""
46+
program = self.getBuildArtifact("a.out")
47+
self.create_debug_adapter()
48+
response = self.launch(
49+
program, launchCommands=["a b c"], runInTerminal=True, expectFailure=True
50+
)
51+
self.assertFalse(response["success"])
52+
self.assertTrue(self.get_dict_value(response, ["body", "error", "showUser"]))
53+
self.assertEqual(
54+
"launchCommands and runInTerminal are mutually exclusive",
55+
self.get_dict_value(response, ['body', 'error', 'format']),
56+
)
57+
3058
@skipIfWindows
3159
def test_termination(self):
3260
"""

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ DAP::DAP(Log *log, const ReplMode default_repl_mode,
8282
std::vector<std::string> pre_init_commands, Transport &transport)
8383
: log(log), transport(transport), broadcaster("lldb-dap"),
8484
exception_breakpoints(), focus_tid(LLDB_INVALID_THREAD_ID),
85-
is_attach(false), stop_at_entry(false),
85+
stop_at_entry(false), is_attach(false),
8686
restarting_process_id(LLDB_INVALID_PROCESS_ID),
8787
configuration_done_sent(false), waiting_for_run_in_terminal(false),
8888
progress_event_reporter(
@@ -668,7 +668,7 @@ lldb::SBTarget DAP::CreateTarget(lldb::SBError &error) {
668668
// creation. We also use target triple and platform from the launch
669669
// configuration, if given, since in some cases ELF file doesn't contain
670670
// enough information to determine correct arch and platform (or ELF can be
671-
// omitted at all), so it is good to leave the user an apportunity to specify
671+
// omitted at all), so it is good to leave the user an opportunity to specify
672672
// those. Any of those three can be left empty.
673673
auto target = this->debugger.CreateTarget(
674674
configuration.program.value_or("").data(),
@@ -677,12 +677,6 @@ lldb::SBTarget DAP::CreateTarget(lldb::SBError &error) {
677677
true, // Add dependent modules.
678678
error);
679679

680-
if (error.Fail()) {
681-
// Update message if there was an error.
682-
error.SetErrorStringWithFormat(
683-
"Could not create a target for a program: %s.", error.GetCString());
684-
}
685-
686680
return target;
687681
}
688682

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ struct DAP {
182182
lldb::tid_t focus_tid;
183183
bool disconnecting = false;
184184
llvm::once_flag terminated_event_flag;
185-
bool is_attach;
186185
bool stop_at_entry;
186+
bool is_attach;
187187
// The process event thread normally responds to process exited events by
188188
// shutting down the entire adapter. When we're restarting, we keep the id of
189189
// the old process here so we can detect this case and keep running.

lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12+
#include "LLDBUtils.h"
1213
#include "Protocol/ProtocolRequests.h"
1314
#include "RequestHandler.h"
1415
#include "llvm/Support/Error.h"
@@ -25,19 +26,19 @@ Error LaunchRequestHandler::Run(const LaunchRequestArguments &arguments) const {
2526
dap.last_launch_request = arguments;
2627
dap.stop_at_entry = arguments.stopOnEntry;
2728

28-
if (!arguments.launchCommands.empty() && arguments.runInTerminal) {
29-
return make_error<DAPError>("launchCommands and runInTerminal are mutually exclusive");
30-
}
29+
if (!arguments.launchCommands.empty() && arguments.runInTerminal)
30+
return make_error<DAPError>(
31+
"launchCommands and runInTerminal are mutually exclusive");
3132

3233
PrintWelcomeMessage();
3334

3435
// This is a hack for loading DWARF in .o files on Mac where the .o files
3536
// in the debug map of the main executable have relative paths which
3637
// require the lldb-dap binary to have its working directory set to that
3738
// relative root for the .o files in order to be able to load debug info.
38-
const std::string debuggerRoot = dap.configuration.debuggerRoot.value_or("");
39-
if (!debuggerRoot.empty())
40-
sys::fs::set_current_path(debuggerRoot);
39+
const std::string debugger_root = dap.configuration.debuggerRoot.value_or("");
40+
if (!debugger_root.empty())
41+
sys::fs::set_current_path(debugger_root);
4142

4243
// Run any initialize LLDB commands the user specified in the launch.json.
4344
// This is run before target is created, so commands can't do anything with
@@ -47,10 +48,12 @@ Error LaunchRequestHandler::Run(const LaunchRequestArguments &arguments) const {
4748

4849
dap.ConfigureSourceMaps();
4950

50-
lldb::SBError status;
51-
dap.SetTarget(dap.CreateTarget(status));
52-
if (status.Fail())
53-
return make_error<DAPError>(status.GetCString());
51+
lldb::SBError error;
52+
lldb::SBTarget target = dap.CreateTarget(error);
53+
if (error.Fail())
54+
return ToError(error);
55+
56+
dap.SetTarget(target);
5457

5558
// Run any pre run LLDB commands the user specified in the launch.json
5659
if (Error err = dap.RunPreRunCommands())

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class RequestHandler : public BaseRequestHandler {
176176

177177
protocol::ErrorResponseBody ToResponse(llvm::Error err) const {
178178
protocol::ErrorMessage error_message;
179+
// Default to showing the user errors unless otherwise specified by a
180+
// DAPError.
181+
error_message.showUser = true;
179182
error_message.sendTelemetry = false;
180183
if (llvm::Error unhandled = llvm::handleErrors(
181184
std::move(err), [&](const DAPError &E) -> llvm::Error {
@@ -185,8 +188,9 @@ class RequestHandler : public BaseRequestHandler {
185188
error_message.url = E.getURL();
186189
error_message.urlLabel = E.getURLLabel();
187190
return llvm::Error::success();
188-
}))
191+
})) {
189192
error_message.format = llvm::toString(std::move(unhandled));
193+
}
190194
protocol::ErrorResponseBody body;
191195
body.error = error_message;
192196
return body;

lldb/tools/lldb-dap/LLDBUtils.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "LLDBUtils.h"
10-
#include "DAP.h"
1110
#include "JSONUtils.h"
11+
#include "lldb/API/SBCommandInterpreter.h"
12+
#include "lldb/API/SBCommandReturnObject.h"
13+
#include "lldb/API/SBDebugger.h"
14+
#include "lldb/API/SBFrame.h"
1215
#include "lldb/API/SBStringList.h"
13-
16+
#include "lldb/API/SBThread.h"
17+
#include "lldb/lldb-enumerations.h"
18+
#include "llvm/ADT/ArrayRef.h"
19+
#include "llvm/Support/JSON.h"
20+
#include "llvm/Support/raw_ostream.h"
1421
#include <mutex>
22+
#include <system_error>
1523

1624
namespace lldb_dap {
1725

0 commit comments

Comments
 (0)