Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lldb/tools/lldb-dap/JSONUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,19 +1395,21 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
if (!cwd.empty())
run_in_terminal_args.try_emplace("cwd", cwd);

std::unordered_map<std::string, std::string> envMap =
GetStringMap(*launch_request_arguments, "env");
llvm::json::Object environment;
for (const auto &[key, value] : envMap) {
auto envs = GetEnvironmentFromArguments(*launch_request_arguments);
llvm::json::Object env_json;
for (size_t index = 0; index < envs.GetNumValues(); index++) {
auto key = llvm::StringRef(envs.GetNameAtIndex(index));
auto value = llvm::StringRef(envs.GetValueAtIndex(index));

if (key.empty())
g_dap.SendOutput(OutputType::Stderr,
"empty environment variable for value: \"" + value +
'\"');
"empty environment variable for value: \"" +
value.str() + '\"');
else
environment.try_emplace(key, value);
env_json.try_emplace(key, value);
}
run_in_terminal_args.try_emplace("env",
llvm::json::Value(std::move(environment)));
llvm::json::Value(std::move(env_json)));

return run_in_terminal_args;
}
Expand Down
25 changes: 25 additions & 0 deletions lldb/tools/lldb-dap/LLDBUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,29 @@ int64_t MakeDAPFrameID(lldb::SBFrame &frame) {
frame.GetFrameID();
}

lldb::SBEnvironment
GetEnvironmentFromArguments(const llvm::json::Object &arguments) {
lldb::SBEnvironment envs{};
constexpr llvm::StringRef env_key = "env";
const auto *env_type = arguments.get(env_key);

if (!env_type)
return envs;

if (env_type->kind() == llvm::json::Value::Object) {
auto env_map = GetStringMap(arguments, env_key);
for (const auto &[key, value] : env_map) {
envs.Set(key.c_str(), value.c_str(), true);
}
} else if (env_type->kind() == llvm::json::Value::Array) {
const auto envs_strings = GetStrings(&arguments, env_key);
lldb::SBStringList entries{};
for (const auto &env : envs_strings) {
entries.AppendString(env.c_str());
}
envs.SetEntries(entries, true);
}
return envs;
}

} // namespace lldb_dap
14 changes: 13 additions & 1 deletion lldb/tools/lldb-dap/LLDBUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#define LLDB_TOOLS_LLDB_DAP_LLDBUTILS_H

#include "DAPForward.h"
#include "lldb/API/SBEnvironment.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
#include <vector>

namespace lldb_dap {

Expand Down Expand Up @@ -135,6 +136,17 @@ uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id);
/// The LLDB frame index ID.
uint32_t GetLLDBFrameID(uint64_t dap_frame_id);

/// Gets all the environment variables from the json object depending on if the
/// kind is an object or an array.
///
/// \param[in] arguments
/// The json object with the launch options
///
/// \return
/// The environment variables stored in the env key
lldb::SBEnvironment
GetEnvironmentFromArguments(const llvm::json::Object &arguments);

} // namespace lldb_dap

#endif
27 changes: 0 additions & 27 deletions lldb/tools/lldb-dap/lldb-dap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,6 @@ std::vector<const char *> MakeArgv(const llvm::ArrayRef<std::string> &strs) {
return argv;
}

// Gets all the environment variables from the json object depending on if the
// kind is an object or an array.
lldb::SBEnvironment
GetEnvironmentFromArguments(const llvm::json::Object &arguments) {
lldb::SBEnvironment envs{};
constexpr llvm::StringRef env_key = "env";
const auto *env_type = arguments.get(env_key);

if (!env_type)
return envs;

if (env_type->kind() == llvm::json::Value::Object) {
auto env_map = GetStringMap(arguments, env_key);
for (const auto &[key, value] : env_map) {
envs.Set(key.c_str(), value.c_str(), true);
}
} else if (env_type->kind() == llvm::json::Value::Array) {
const auto envs_strings = GetStrings(&arguments, env_key);
lldb::SBStringList entries{};
for (const auto &env : envs_strings) {
entries.AppendString(env.c_str());
}
envs.SetEntries(entries, true);
}
return envs;
}

// Send a "exited" event to indicate the process has exited.
void SendProcessExitedEvent(lldb::SBProcess &process) {
llvm::json::Object event(CreateEventObject("exited"));
Expand Down
5 changes: 3 additions & 2 deletions lldb/tools/lldb-dap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@
"type": "array",
"description": "Additional environment variables to set when launching the program. E.g. `[\"FOO=1\", \"BAR\"]`",
"items": {
"type": "string"
"type": "string",
"pattern": "^((\\w+=.*)|^\\w+)$"
},
"default": {}
"default": []
}
]
},
Expand Down