Debian Codegraph @

DebugAdapterClient/DapLocator.cpp

raw
1 #include "DapLocator.hpp"
2
3 #include "Platform.hpp"
4 #include "asyncprocess.h"
5 #include "file_logger.h"
6 #include "globals.h"
7 #include "processreaderthread.h"
8 #include "procutils.h"
9
10 #include <wx/filefn.h>
11
12 DapLocator::DapLocator() {}
13
14 DapLocator::~DapLocator() {}
15
16 size_t DapLocator::Locate(std::vector<DapEntry>* entries)
17 {
18 find_lldb_vscode(entries);
19 find_debugpy(entries);
20 return entries->size();
21 }
22
23 namespace
24 {
25 wxString wrap_string(const wxString& str)
26 {
27 wxString s = str;
28 if(s.Contains(" ")) {
29 s.Prepend("\"").Append("\"");
30 }
31 return s;
32 }
33
34 wxString get_exe_name(const wxString& base, size_t counter = wxString::npos)
35 {
36 wxString exename = base;
37 if(counter != wxString::npos) {
38 exename << "-" << counter;
39 }
40 #ifdef __WXMSW__
41 exename << ".exe";
42 #endif
43 return exename;
44 }
45
46 DapEntry create_entry(const wxString& name, int port, const std::vector<wxString>& cmd, DapLaunchType launch_type)
47 {
48 DapEntry entry;
49 entry.SetName(name);
50 #ifdef __WXMSW__
51 entry.SetUseForwardSlash(true);
52 entry.SetUseVolume(false);
53 entry.SetUseRelativePath(false);
54 #else
55 entry.SetUseNativePath();
56 #endif
57 wxString connection_string;
58 connection_string << "tcp://127.0.0.1:" << port;
59 entry.SetConnectionString(connection_string);
60
61 wxString command;
62 for(const wxString& c : cmd) {
63 command << wrap_string(c) << " ";
64 }
65 command.RemoveLast();
66 entry.SetCommand(command);
67 entry.SetLaunchType(launch_type);
68 entry.SetEnvFormat(dap::EnvFormat::DICTIONARY);
69 return entry;
70 }
71 } // namespace
72
73 void DapLocator::find_lldb_vscode(std::vector<DapEntry>* entries)
74 {
75 wxArrayString paths;
76 wxString lldb_vscode;
77 if(!ThePlatform->Which("lldb-vscode", &lldb_vscode)) {
78 return;
79 }
80
81 auto entry = create_entry("lldb-vscode", 12345, { lldb_vscode, "--port", "12345" }, DapLaunchType::LAUNCH);
82 entry.SetEnvFormat(dap::EnvFormat::LIST);
83 entries->push_back(entry);
84 }
85
86 void DapLocator::find_debugpy(std::vector<DapEntry>* entries)
87 {
88 // locate pip first
89 wxArrayString paths;
90 wxString python;
91
92 // locate python3
93 if(!ThePlatform->Which("python", &python) && !ThePlatform->Which("python3", &python)) {
94 return;
95 }
96
97 // we got pip
98 wxString line = ProcUtils::GrepCommandOutput({ python, "-m", "pip", "list" }, "debugpy");
99 if(line.empty())
100 return;
101
102 // we have a match
103 auto entry =
104 create_entry("debugpy", 12345,
105 { python, "-m", "debugpy", "--listen", "12345", "--wait-for-client", "$(CurrentFileFullPath)" },
106 DapLaunchType::ATTACH);
107 entry.SetUseNativePath();
108 entries->push_back(entry);
109 }