55#include < stdint.h>
66#include < fstream>
77#include < string>
8+ #include < vector>
89
910#ifdef _MSC_VER
1011// clang-format off
@@ -31,30 +32,32 @@ TEST(ProcessDetectorUtilsTest, FormFilePath)
3132 EXPECT_EQ (exe_path, " /proc/1234/exe" );
3233}
3334
34- TEST (ProcessDetectorUtilsTest, ExtractCommand )
35+ TEST (ProcessDetectorUtilsTest, ExtractCommandWithArgs )
3536{
36- std::string filename{" test_command .txt" };
37+ std::string filename{" test_command_args .txt" };
3738
3839 {
3940 std::ofstream outfile (filename, std::ios::binary);
4041 const char raw_data[] = " test_command\0 arg1\0 arg2\0 arg3\0 " ;
4142 outfile.write (raw_data, sizeof (raw_data) - 1 );
4243 }
4344
44- std::string command = opentelemetry::resource_detector::detail::ExtractCommand (filename);
45- EXPECT_EQ (command, std::string{" test_command" });
45+ std::vector<std::string> args =
46+ opentelemetry::resource_detector::detail::ExtractCommandWithArgs (filename);
47+ EXPECT_EQ (args, (std::vector<std::string>{" test_command" , " arg1" , " arg2" , " arg3" }));
4648
4749 std::remove (filename.c_str ()); // Cleanup
4850}
4951
50- TEST (ProcessDetectorUtilsTest, EmptyCommandFile )
52+ TEST (ProcessDetectorUtilsTest, EmptyCommandWithArgsFile )
5153{
52- std::string filename{" empty_command .txt" };
54+ std::string filename{" empty_command_args .txt" };
5355 std::ofstream outfile (filename, std::ios::binary);
5456 outfile.close ();
5557
56- std::string command = opentelemetry::resource_detector::detail::ExtractCommand (filename);
57- EXPECT_EQ (command, std::string{" " });
58+ std::vector<std::string> args =
59+ opentelemetry::resource_detector::detail::ExtractCommandWithArgs (filename);
60+ EXPECT_TRUE (args.empty ());
5861
5962 std::remove (filename.c_str ()); // Cleanup
6063}
@@ -109,40 +112,79 @@ TEST(ProcessDetectorUtilsTest, GetExecutablePathTest)
109112 EXPECT_EQ (path, expected_path);
110113}
111114
112- TEST (ProcessDetectorUtilsTest, GetCommandTest )
115+ TEST (ProcessDetectorUtilsTest, CommandTest )
113116{
114117 int32_t pid = getpid ();
115118 std::string command;
116119#ifdef _MSC_VER
117- // On Windows, GetCommandLineW only works for the CURRENT process,
118- // so we ignore `pid` and just return the current process's command line.
119- LPCWSTR wcmd = GetCommandLineW ();
120- if (!wcmd )
120+ int argc = 0 ;
121+ LPWSTR *argvW = CommandLineToArgvW ( GetCommandLineW (), &argc);
122+
123+ if (argvW && argc > 0 )
121124 {
122- command = std::string ();
125+ int size_needed = WideCharToMultiByte (CP_UTF8, 0 , argvW[0 ], -1 , NULL , 0 , NULL , NULL );
126+ if (size_needed > 0 )
127+ {
128+ std::string arg (size_needed - 1 , 0 );
129+ WideCharToMultiByte (CP_UTF8, 0 , argvW[0 ], -1 , &arg[0 ], size_needed, NULL , NULL );
130+ command = arg;
131+ }
132+
133+ LocalFree (argvW);
123134 }
124135 else
125136 {
137+ command = std::string ();
138+ }
139+ #else
140+ std::string command_line_path =
141+ opentelemetry::resource_detector::detail::FormFilePath (pid, " cmdline" );
142+ std::ifstream command_line_file (command_line_path, std::ios::in | std::ios::binary);
143+ std::getline (command_line_file, command, ' \0 ' );
144+ #endif
145+ std::vector<std::string> expected_command_with_args =
146+ opentelemetry::resource_detector::detail::GetCommandWithArgs (pid);
147+ std::string expected_command;
148+ if (!expected_command_with_args.empty ())
149+ {
150+ expected_command = expected_command_with_args[0 ];
151+ }
152+ EXPECT_EQ (command, expected_command);
153+ }
126154
127- // Convert UTF-16 to UTF-8
128- int size_needed = WideCharToMultiByte (CP_UTF8, 0 , wcmd, -1 , NULL , 0 , NULL , NULL );
129- if (size_needed <= 0 )
130- {
131- command = std::string ();
132- }
133- else
155+ TEST (ProcessDetectorUtilsTest, GetCommandWithArgsTest)
156+ {
157+ int32_t pid = getpid ();
158+ std::vector<std::string> args;
159+ #ifdef _MSC_VER
160+ int argc = 0 ;
161+ LPWSTR *argvW = CommandLineToArgvW (GetCommandLineW (), &argc);
162+ if (!argvW)
163+ {
164+ args = {};
165+ }
166+ else
167+ {
168+ for (int i = 0 ; i < argc; i++)
134169 {
135- std::string utf8_command (size_needed - 1 , 0 ); // exclude null terminator
136- WideCharToMultiByte (CP_UTF8, 0 , wcmd, -1 , &utf8_command[0 ], size_needed, NULL , NULL );
137- command = utf8_command;
170+ // Convert UTF-16 to UTF-8
171+ int size_needed = WideCharToMultiByte (CP_UTF8, 0 , argvW[i], -1 , NULL , 0 , NULL , NULL );
172+ if (size_needed > 0 )
173+ {
174+ std::string arg (size_needed - 1 , 0 );
175+ WideCharToMultiByte (CP_UTF8, 0 , argvW[i], -1 , &arg[0 ], size_needed, NULL , NULL );
176+ args.push_back (arg);
177+ }
138178 }
139179 }
180+
181+ LocalFree (argvW);
140182#else
141- // This is the path to get the command that was used to start the process
142183 std::string command_line_path =
143184 opentelemetry::resource_detector::detail::FormFilePath (pid, " cmdline" );
144- command = opentelemetry::resource_detector::detail::ExtractCommand (command_line_path);
185+ args = opentelemetry::resource_detector::detail::ExtractCommandWithArgs (command_line_path);
145186#endif
146- std::string expected_command = opentelemetry::resource_detector::detail::GetCommand (pid);
147- EXPECT_EQ (command, expected_command);
187+ std::vector<std::string> expected_args =
188+ opentelemetry::resource_detector::detail::GetCommandWithArgs (pid);
189+ EXPECT_EQ (args, expected_args);
148190}
0 commit comments