diff --git a/lldb/tools/lldb-vscode/lldb-vscode.cpp b/lldb/tools/lldb-vscode/lldb-vscode.cpp --- a/lldb/tools/lldb-vscode/lldb-vscode.cpp +++ b/lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -1346,28 +1346,19 @@ // Run any initialize LLDB commands the user specified in the launch.json g_vsc.RunInitCommands(); - // Grab the current working directory if there is one and set it in the - // launch info. - const auto cwd = GetString(arguments, "cwd"); - if (!cwd.empty()) - g_vsc.launch_info.SetWorkingDirectory(cwd.data()); - - // Grab the name of the program we need to debug and set it as the first - // argument that will be passed to the program we will debug. + // Grab the name of the program we need to debug and create a new target using + // the given program as an argument and replace previous dummy target with a + // new one. New target might have a different type then the previous one, + // based on the ABI or architecture of the file. Settings program path in + // LaunchInfo is useless, because launch info will recycle existing target, + // which might be incorrect for the selected file. llvm::StringRef program = GetString(arguments, "program"); if (!program.empty()) { - lldb::SBFileSpec program_fspec(program.data(), true /*resolve_path*/); - g_vsc.launch_info.SetExecutableFile(program_fspec, - true /*add_as_first_arg*/); - const char *target_triple = nullptr; - const char *uuid_cstr = nullptr; - // Stand alone debug info file if different from executable - const char *symfile = nullptr; - lldb::SBModule module = g_vsc.target.AddModule( - program.data(), target_triple, uuid_cstr, symfile); - if (!module.IsValid()) { - response["success"] = llvm::json::Value(false); + // Create a target that matches the file. + g_vsc.target = g_vsc.debugger.CreateTarget(program.data()); + if (!g_vsc.target.IsValid()) { + response["success"] = llvm::json::Value(false); EmplaceSafeString( response, "message", llvm::formatv("Could not load program '{0}'.", program).str()); @@ -1376,6 +1367,15 @@ } } + // Instantiate a launch info instance for the target. + g_vsc.launch_info = g_vsc.target.GetLaunchInfo(); + + // Grab the current working directory if there is one and set it in the + // launch info. + const auto cwd = GetString(arguments, "cwd"); + if (!cwd.empty()) + g_vsc.launch_info.SetWorkingDirectory(cwd.data()); + // Extract any extra arguments and append them to our program arguments for // when we launch auto args = GetStrings(arguments, "args");