xref: /freebsd/contrib/kyua/cli/cmd_debug.cpp (revision 576ee62dd2e5e0454a5316eb9207f4ebaa543171)
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 <iostream>
37 
38 #include "cli/common.ipp"
39 #include "drivers/debug_test.hpp"
40 #include "engine/filters.hpp"
41 #include "utils/cmdline/exceptions.hpp"
42 #include "utils/cmdline/options.hpp"
43 #include "utils/cmdline/parser.ipp"
44 #include "utils/cmdline/ui.hpp"
45 #include "utils/format/macros.hpp"
46 #include "utils/fs/path.hpp"
47 #include "utils/process/child.ipp"
48 #include "utils/process/executor.hpp"
49 #include "utils/process/operations.hpp"
50 #include "utils/process/status.hpp"
51 
52 namespace cmdline = utils::cmdline;
53 namespace config = utils::config;
54 namespace executor = utils::process::executor;
55 namespace process = utils::process;
56 
57 using cli::cmd_debug;
58 using utils::process::args_vector;
59 using utils::process::child;
60 
61 
62 namespace {
63 
64 
65 const cmdline::bool_option pause_before_cleanup_upon_fail_option(
66     'p',
67     "pause-before-cleanup-upon-fail",
68     "Pauses right before the test cleanup upon fail");
69 
70 
71 const cmdline::bool_option pause_before_cleanup_option(
72     "pause-before-cleanup",
73     "Pauses right before the test cleanup");
74 
75 
76 static const char* DEFAULT_CMD = "$SHELL";
77 const cmdline::string_option execute_option(
78     'x', "execute",
79     "A command to run within the given execenv upon test failure",
80     "cmd", DEFAULT_CMD, true);
81 
82 
83 /// Functor to execute a program.
84 class execute {
85     const std::string& _cmd;
86     executor::exit_handle& _eh;
87 
88 public:
89     /// Constructor.
90     ///
91     /// \param program Program binary absolute path.
92     /// \param args Program arguments.
93     execute(
94         const std::string& cmd_,
95         executor::exit_handle& eh_) :
96         _cmd(cmd_),
97         _eh(eh_)
98     {
99     }
100 
101     /// Body of the subprocess.
102     void
103     operator()(void)
104     {
105         if (::chdir(_eh.work_directory().c_str()) == -1) {
106             std::cerr << "execute: chdir() errors: "
107                 << strerror(errno) << ".\n";
108             std::exit(EXIT_FAILURE);
109         }
110 
111         std::string program_path = "/bin/sh";
112         const char* shell = std::getenv("SHELL");
113         if (shell)
114             program_path = shell;
115 
116         args_vector av;
117         if (!(_cmd.empty() || _cmd == DEFAULT_CMD)) {
118             av.push_back("-c");
119             av.push_back(_cmd);
120         }
121 
122         process::exec(utils::fs::path(program_path), av);
123     }
124 };
125 
126 
127 /// The debugger interface implementation.
128 class dbg : public engine::debugger {
129     /// Object to interact with the I/O of the program.
130     cmdline::ui* _ui;
131 
132     /// Representation of the command line to the subcommand.
133     const cmdline::parsed_cmdline& _cmdline;
134 
135 public:
136     /// Constructor.
137     ///
138     /// \param ui_ Object to interact with the I/O of the program.
139     /// \param cmdline Representation of the command line to the subcommand.
140     dbg(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline) :
141         _ui(ui), _cmdline(cmdline)
142     {}
143 
144     void before_cleanup(
145         const model::test_program_ptr&,
146         const model::test_case&,
147         optional< model::test_result >& result,
148         executor::exit_handle& eh) const
149     {
150         if (_cmdline.has_option(pause_before_cleanup_upon_fail_option
151             .long_name())) {
152             if (result && !result.get().good()) {
153                 _ui->out("The test failed and paused right before its cleanup "
154                     "routine.");
155                 _ui->out(F("Test work dir: %s") % eh.work_directory().str());
156                 _ui->out("Press <Enter> to continue...");
157                 (void) std::cin.get();
158             }
159         } else if (_cmdline.has_option(pause_before_cleanup_option
160             .long_name())) {
161             _ui->out("The test paused right before its cleanup routine.");
162             _ui->out(F("Test work dir: %s") % eh.work_directory().str());
163             _ui->out("Press <Enter> to continue...");
164             (void) std::cin.get();
165         }
166     };
167 
168     void upon_test_failure(
169         const model::test_program_ptr&,
170         const model::test_case&,
171         optional< model::test_result >&,
172         executor::exit_handle& eh) const
173     {
174         if (!_cmdline.has_option(execute_option.long_name()))
175             return;
176         const std::string& cmd = _cmdline.get_option<cmdline::string_option>(
177             execute_option.long_name());
178         std::unique_ptr< process::child > child = child::fork_interactive(
179             execute(cmd, eh));
180         (void) child->wait();
181     };
182 
183 };
184 
185 
186 }  // anonymous namespace
187 
188 
189 /// Default constructor for cmd_debug.
190 cmd_debug::cmd_debug(void) : cli_command(
191     "debug", "test_case", 1, 1,
192     "Executes a single test case providing facilities for debugging")
193 {
194     add_option(build_root_option);
195     add_option(kyuafile_option);
196 
197     add_option(pause_before_cleanup_upon_fail_option);
198     add_option(pause_before_cleanup_option);
199 
200     add_option(cmdline::path_option(
201         "stdout", "Where to direct the standard output of the test case",
202         "path", "/dev/stdout"));
203 
204     add_option(cmdline::path_option(
205         "stderr", "Where to direct the standard error of the test case",
206         "path", "/dev/stderr"));
207 
208     add_option(execute_option);
209 }
210 
211 
212 /// Entry point for the "debug" subcommand.
213 ///
214 /// \param ui Object to interact with the I/O of the program.
215 /// \param cmdline Representation of the command line to the subcommand.
216 /// \param user_config The runtime debuguration of the program.
217 ///
218 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
219 /// opened.
220 int
221 cmd_debug::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
222                const config::tree& user_config)
223 {
224     const std::string& test_case_name = cmdline.arguments()[0];
225     if (test_case_name.find(':') == std::string::npos)
226         throw cmdline::usage_error(F("'%s' is not a test case identifier "
227                                      "(missing ':'?)") % test_case_name);
228     const engine::test_filter filter = engine::test_filter::parse(
229         test_case_name);
230 
231     engine::debugger_ptr debugger = nullptr;
232     if (cmdline.has_option(pause_before_cleanup_upon_fail_option.long_name())
233         || cmdline.has_option(pause_before_cleanup_option.long_name())
234         || cmdline.has_option(execute_option.long_name())) {
235         debugger = std::shared_ptr< engine::debugger >(new dbg(ui, cmdline));
236     }
237 
238     const drivers::debug_test::result result = drivers::debug_test::drive(
239         debugger,
240         kyuafile_path(cmdline), build_root_path(cmdline), filter, user_config,
241         cmdline.get_option< cmdline::path_option >("stdout"),
242         cmdline.get_option< cmdline::path_option >("stderr"));
243 
244     ui->out(F("%s  ->  %s") % cli::format_test_case_id(result.test_case) %
245             cli::format_result(result.test_result));
246 
247     return result.test_result.good() ? EXIT_SUCCESS : EXIT_FAILURE;
248 }
249