xref: /freebsd/contrib/kyua/cli/cmd_debug.cpp (revision 8a7df6c58d549bd2eaa5cad30b2ef93c3eca3c18)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "cli/cmd_debug.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include <cstdlib>
36 #include <cstring>
37 #include <iostream>
38 
39 #include "cli/common.ipp"
40 #include "drivers/debug_test.hpp"
41 #include "engine/filters.hpp"
42 #include "utils/cmdline/exceptions.hpp"
43 #include "utils/cmdline/options.hpp"
44 #include "utils/cmdline/parser.ipp"
45 #include "utils/cmdline/ui.hpp"
46 #include "utils/format/macros.hpp"
47 #include "utils/fs/path.hpp"
48 #include "utils/process/child.ipp"
49 #include "utils/process/executor.hpp"
50 #include "utils/process/operations.hpp"
51 #include "utils/process/status.hpp"
52 
53 namespace cmdline = utils::cmdline;
54 namespace config = utils::config;
55 namespace executor = utils::process::executor;
56 namespace process = utils::process;
57 
58 using cli::cmd_debug;
59 using utils::process::args_vector;
60 using utils::process::child;
61 
62 
63 namespace {
64 
65 
66 const cmdline::bool_option pause_before_cleanup_upon_fail_option(
67     'p',
68     "pause-before-cleanup-upon-fail",
69     "Pauses right before the test cleanup upon fail");
70 
71 
72 const cmdline::bool_option pause_before_cleanup_option(
73     'P',
74     "pause-before-cleanup",
75     "Pauses right before the test cleanup");
76 
77 
78 static const char* DEFAULT_CMD = "$SHELL";
79 const cmdline::string_option execute_option(
80     'x', "execute",
81     "A command to run within the given execenv upon test failure",
82     "cmd", DEFAULT_CMD, true);
83 
84 
85 /// Functor to execute a program.
86 class execute {
87     const std::string& _cmd;
88     executor::exit_handle& _eh;
89 
90 public:
91     /// Constructor.
92     ///
93     /// \param program Program binary absolute path.
94     /// \param args Program arguments.
95     execute(
96         const std::string& cmd_,
97         executor::exit_handle& eh_) :
98         _cmd(cmd_),
99         _eh(eh_)
100     {
101     }
102 
103     /// Body of the subprocess.
104     void
105     operator()(void)
106     {
107         if (::chdir(_eh.work_directory().c_str()) == -1) {
108             std::cerr << "execute: chdir() errors: "
109                 << std::strerror(errno) << ".\n";
110             std::exit(EXIT_FAILURE);
111         }
112 
113         std::string program_path = "/bin/sh";
114         const char* shell = std::getenv("SHELL");
115         if (shell)
116             program_path = shell;
117 
118         args_vector av;
119         if (!(_cmd.empty() || _cmd == DEFAULT_CMD)) {
120             av.push_back("-c");
121             av.push_back(_cmd);
122         }
123 
124         process::exec(utils::fs::path(program_path), av);
125     }
126 };
127 
128 
129 /// The debugger interface implementation.
130 class dbg : public engine::debugger {
131     /// Object to interact with the I/O of the program.
132     cmdline::ui* _ui;
133 
134     /// Representation of the command line to the subcommand.
135     const cmdline::parsed_cmdline& _cmdline;
136 
137 public:
138     /// Constructor.
139     ///
140     /// \param ui_ Object to interact with the I/O of the program.
141     /// \param cmdline Representation of the command line to the subcommand.
142     dbg(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline) :
143         _ui(ui), _cmdline(cmdline)
144     {}
145 
146     void before_cleanup(
147         const model::test_program_ptr&,
148         const model::test_case&,
149         optional< model::test_result >& result,
150         executor::exit_handle& eh) const
151     {
152         if (_cmdline.has_option(pause_before_cleanup_upon_fail_option
153             .long_name())) {
154             if (result && !result.get().good()) {
155                 _ui->out("The test failed and paused right before its cleanup "
156                     "routine.");
157                 _ui->out(F("Test work dir: %s") % eh.work_directory().str());
158                 _ui->out("Press any key to continue...");
159                 (void) std::cin.get();
160             }
161         } else if (_cmdline.has_option(pause_before_cleanup_option
162             .long_name())) {
163             _ui->out("The test paused right before its cleanup routine.");
164             _ui->out(F("Test work dir: %s") % eh.work_directory().str());
165             _ui->out("Press any key to continue...");
166             (void) std::cin.get();
167         }
168     };
169 
170     void upon_test_failure(
171         const model::test_program_ptr&,
172         const model::test_case&,
173         optional< model::test_result >&,
174         executor::exit_handle& eh) const
175     {
176         if (!_cmdline.has_option(execute_option.long_name()))
177             return;
178         const std::string& cmd = _cmdline.get_option<cmdline::string_option>(
179             execute_option.long_name());
180         std::unique_ptr< process::child > child = child::fork_interactive(
181             execute(cmd, eh));
182         (void) child->wait();
183     };
184 
185 };
186 
187 
188 }  // anonymous namespace
189 
190 
191 /// Default constructor for cmd_debug.
192 cmd_debug::cmd_debug(void) : cli_command(
193     "debug", "test_case", 1, 1,
194     "Executes a single test case providing facilities for debugging")
195 {
196     add_option(build_root_option);
197     add_option(kyuafile_option);
198 
199     add_option(pause_before_cleanup_upon_fail_option);
200     add_option(pause_before_cleanup_option);
201 
202     add_option(cmdline::path_option(
203         "stdout", "Where to direct the standard output of the test case",
204         "path", "/dev/stdout"));
205 
206     add_option(cmdline::path_option(
207         "stderr", "Where to direct the standard error of the test case",
208         "path", "/dev/stderr"));
209 
210     add_option(execute_option);
211 }
212 
213 
214 /// Entry point for the "debug" subcommand.
215 ///
216 /// \param ui Object to interact with the I/O of the program.
217 /// \param cmdline Representation of the command line to the subcommand.
218 /// \param user_config The runtime debuguration of the program.
219 ///
220 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
221 /// opened.
222 int
223 cmd_debug::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
224                const config::tree& user_config)
225 {
226     const std::string& test_case_name = cmdline.arguments()[0];
227     if (test_case_name.find(':') == std::string::npos)
228         throw cmdline::usage_error(F("'%s' is not a test case identifier "
229                                      "(missing ':'?)") % test_case_name);
230     const engine::test_filter filter = engine::test_filter::parse(
231         test_case_name);
232 
233     engine::debugger_ptr debugger = nullptr;
234     if (cmdline.has_option(pause_before_cleanup_upon_fail_option.long_name())
235         || cmdline.has_option(pause_before_cleanup_option.long_name())
236         || cmdline.has_option(execute_option.long_name())) {
237         debugger = std::shared_ptr< engine::debugger >(new dbg(ui, cmdline));
238     }
239 
240     const drivers::debug_test::result result = drivers::debug_test::drive(
241         debugger,
242         kyuafile_path(cmdline), build_root_path(cmdline), filter, user_config,
243         cmdline.get_option< cmdline::path_option >("stdout"),
244         cmdline.get_option< cmdline::path_option >("stderr"));
245 
246     ui->out(F("%s  ->  %s") % cli::format_test_case_id(result.test_case) %
247             cli::format_result(result.test_result));
248 
249     return result.test_result.good() ? EXIT_SUCCESS : EXIT_FAILURE;
250 }
251