Skip to content

Commit

Permalink
重构launch调试
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jul 4, 2023
1 parent ce653b9 commit 6ce57d2
Show file tree
Hide file tree
Showing 10 changed files with 833 additions and 1,067 deletions.
13 changes: 7 additions & 6 deletions emmy_tool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ add_dependencies(
shared
)

target_sources(emmy_tool PRIVATE
src/utility.h
src/emmy_tool.h
src/command_line.h
target_include_directories(emmy_tool PRIVATE
src
)

src/utility.cpp
src/emmy_tool.windows.cpp
target_sources(emmy_tool PRIVATE
src/windows/utility.cpp
src/windows/emmy_tool.windows.cpp
src/command_line.cpp
src/main.cpp
src/emmy_tool.cpp
)

target_link_libraries(
Expand Down
86 changes: 28 additions & 58 deletions emmy_tool/src/command_line.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,58 @@
#include "command_line.h"

void CommandLine::AddTarget(const std::string& name, bool isParse)
{
void CommandLine::AddTarget(const std::string &name, bool isParse) {
_targets.insert({name, isParse});
}

std::string CommandLine::GetTarget() const noexcept
{
std::string CommandLine::GetTarget() const noexcept {
return _currentTarget;
}

std::string CommandLine::GetArg(int index) const noexcept
{
if (static_cast<std::size_t>(index) < _argvs.size())
{
std::string CommandLine::GetArg(int index) const noexcept {
if (static_cast<std::size_t>(index) < _argvs.size()) {
return _argvs[index];
}
else
{
} else {
return "";
}
}

bool CommandLine::Parse(int argc, char** argv)
{
if (argc < 2)
{
bool CommandLine::Parse(int argc, char **argv) {
if (argc < 2) {
return false;
}
_currentTarget = argv[1];

if (_targets.count(_currentTarget) == 0)
{
if (_targets.count(_currentTarget) == 0) {
return false;
}

bool isParse = _targets.at(_currentTarget);
_argvs.reserve(argc);
for (int i = 0; i != argc; i++)
{
for (int i = 0; i != argc; i++) {
_argvs.emplace_back(argv[i]);
}

if (!isParse)
{
if (!isParse) {
return true;
}

// index = 0 的参数是程序名
for (int index = 1; index < argc; index++)
{
for (int index = 1; index < argc; index++) {
std::string current = argv[index];
if (current.empty())
{
if (current.empty()) {
continue;
}
// not empty
if (current[0] == '-')
{
if (current[0] == '-') {
// 仅仅支持-dir这种形式
std::string optionName = current.substr(1);
// 如果该参数不存在
if (_args.count(optionName) == 0)
{
if (_args.count(optionName) == 0) {
return false;
}
CommandLineOption& option = _args[optionName];
CommandLineOption &option = _args[optionName];

if (option.Type == CommandLineValueType::Boolean)
{
if (option.Type == CommandLineValueType::Boolean) {
option.Value = "true";
continue;
}
Expand All @@ -78,57 +62,43 @@ bool CommandLine::Parse(int argc, char** argv)

// 该选项之后没有接参数
// 目前没有支持bool选项的必要
if (argc <= (index + 1) && !option.RestOfAll)
{
if (argc <= (index + 1) && !option.RestOfAll) {
return false;
}

do
{
if (argc <= (index + 1))
{
do {
if (argc <= (index + 1)) {
break;
}

std::string value = argv[++index];
if (value.empty())
{
if (value.empty()) {
continue;
}
if (option.RestOfAll)
{
if (option.RestOfAll) {
// 剩余参数通常会被传递到子进程再处理
// 如果剩余参数中存在路径,且路径存在空格,那么传递到子进程的创建就会失效
// 所以这里要特别的处理
if (!value.empty())
{
if (!value.empty()) {
// 认为该参数可能是选项
if (value[0] == '-')
{
if (value[0] == '-') {
optionValue.append(" ").append(value);
}
else //认为该参数是值,所以用引号包含起来
} else//认为该参数是值,所以用引号包含起来
{
optionValue.append(" ").append("\"" + value + "\"");
}
}
}
else
{
} else {
// 认为值是被一对引号包起来的
// windows下引号已经被自动处理了
if (value[0] == '\"' || value[0] == '\'')
{
if (value[0] == '\"' || value[0] == '\'') {
optionValue = value.substr(1, value.size() - 2);
}
else
{
} else {
optionValue = value;
}
break;
}
}
while (true);
} while (true);

option.Value = std::move(optionValue);
}
Expand Down
6 changes: 5 additions & 1 deletion emmy_tool/src/emmy_tool.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

#include "emmy_tool.h"

EmmyTool::EmmyTool(CommandLine &cmd)
: _cmd(cmd) {
}
33 changes: 20 additions & 13 deletions emmy_tool/src/emmy_tool.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#pragma once
#include <Windows.h>

#include "command_line.h"

bool StartProcessAndInjectDll(LPCSTR exeFileName,
LPSTR command,
LPCSTR workDirectory,
LPCSTR dllDirectory,
LPCSTR dllName,
bool blockOnExit,
int debugPort,
bool listenMode,
bool createNewWindow
);
class EmmyTool {
public:
explicit EmmyTool(CommandLine &cmd);

int Launch();

int Attach();

int ListProcesses();

int ArchFile();

int ArchPid();

int ReceiveLog();

private:

bool InjectDll(DWORD processId, const char* dllDir, const char* dllFileName, bool capture);
CommandLine _cmd;
};

void ReceiveLog(DWORD processId);
Loading

0 comments on commit 6ce57d2

Please sign in to comment.