X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fcmdline.cc;h=e774113a87f75bc5f8635f1f3312bd02a075d7dc;hb=bc11fb05ae329bcd07b8604987439c4ad9f2748f;hp=6ef407849cad8da6e523691dc3e71676f9999341;hpb=357b107e59d9918a0f3bdd7e4aad9493c70e03f1;p=m6w6%2Flibmemcached diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index 6ef40784..e774113a 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -19,13 +19,16 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include using namespace libtest; #include #include +#include #include +#include #include #include #include @@ -56,7 +59,7 @@ extern "C" { namespace { - std::string print_argv(char * * & built_argv, const size_t& argc, const pid_t& pid) + std::string print_argv(char * * & built_argv, const size_t& argc) { std::stringstream arg_buffer; @@ -68,12 +71,41 @@ namespace { return arg_buffer.str(); } + std::string print_argv(char** argv) + { + std::stringstream arg_buffer; + + for (char** ptr= argv; *ptr; ptr++) + { + arg_buffer << *ptr << " "; + } + + return arg_buffer.str(); + } + + static Application::error_t int_to_error_t(int arg) + { + switch (arg) + { + case 127: + return Application::INVALID; + + case 0: + return Application::SUCCESS; + + default: + case 1: + return Application::FAILURE; + } + } } namespace libtest { Application::Application(const std::string& arg, const bool _use_libtool_arg) : _use_libtool(_use_libtool_arg), + _use_valgrind(false), + _use_gdb(false), _argc(0), _exectuble(arg), built_argv(NULL), @@ -87,6 +119,19 @@ Application::Application(const std::string& arg, const bool _use_libtool_arg) : } } + // Find just the name of the application with no path + { + size_t found= arg.find_last_of("/\\"); + if (found) + { + _exectuble_name= arg.substr(found +1); + } + else + { + _exectuble_name= arg; + } + } + if (_use_libtool and getenv("PWD")) { _exectuble_with_path+= getenv("PWD"); @@ -118,13 +163,66 @@ Application::error_t Application::run(const char *args[]) create_argv(args); int spawn_ret; - if (_use_libtool) + if (_use_gdb) { - spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL); + std::string gdb_run_file= create_tmpfile(_exectuble_name); + std::fstream file_stream; + file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc); + + _gdb_filename= create_tmpfile(_exectuble_name); + file_stream + << "set logging redirect on" << std::endl + << "set logging file " << _gdb_filename << std::endl + << "set logging overwrite on" << std::endl + << "set logging on" << std::endl + << "set environment LIBTEST_IN_GDB=1" << std::endl + << "run " << arguments() << std::endl + << "thread apply all bt" << std::endl + << "quit" << std::endl; + + fatal_assert(file_stream.good()); + file_stream.close(); + + if (_use_libtool) + { + // libtool --mode=execute gdb -f -x binary + char *argv[]= { + const_cast(libtool()), + const_cast("--mode=execute"), + const_cast("gdb"), + const_cast("-batch"), + const_cast("-f"), + const_cast("-x"), + const_cast(gdb_run_file.c_str()), + const_cast(_exectuble_with_path.c_str()), + 0}; + + spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, NULL, argv, NULL); + } + else + { + // gdb binary + char *argv[]= { + const_cast("gdb"), + const_cast("-batch"), + const_cast("-f"), + const_cast("-x"), + const_cast(gdb_run_file.c_str()), + const_cast(_exectuble_with_path.c_str()), + 0}; + spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, NULL, argv, NULL); + } } else { - spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL); + if (_use_libtool) + { + spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL); + } + else + { + spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL); + } } posix_spawn_file_actions_destroy(&file_actions); @@ -135,7 +233,6 @@ Application::error_t Application::run(const char *args[]) if (spawn_ret) { - Error << print(); return Application::INVALID; } @@ -218,16 +315,16 @@ Application::error_t Application::wait() { throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Pid mismatch, %d != %d", int(waited_pid), int(_pid)); } - exit_code= error_t(exited_successfully(status)); + exit_code= int_to_error_t(exited_successfully(status)); } } +#if 0 if (exit_code == Application::INVALID) { -#if 0 - Error << print_argv(built_argv, _argc, _pid); -#endif + Error << print_argv(built_argv, _argc); } +#endif return exit_code; } @@ -256,26 +353,24 @@ void Application::Pipe::reset() close(WRITE); int ret; - if ((ret= pipe(_fd)) < 0) + if (pipe(_fd) == -1) { - throw strerror(ret); + throw strerror(errno); } _open[0]= true; _open[1]= true; { - ret= fcntl(_fd[0], F_GETFL, 0); - if (ret == -1) + if ((ret= fcntl(_fd[0], F_GETFL, 0)) == -1) { - Error << "fcntl(F_GETFL) " << strerror(ret); - throw strerror(ret); + Error << "fcntl(F_GETFL) " << strerror(errno); + throw strerror(errno); } - ret= fcntl(_fd[0], F_SETFL, ret | O_NONBLOCK); - if (ret == -1) + if ((ret= fcntl(_fd[0], F_SETFL, ret | O_NONBLOCK)) == -1) { - Error << "fcntl(F_SETFL) " << strerror(ret); - throw strerror(ret); + Error << "fcntl(F_SETFL) " << strerror(errno); + throw strerror(errno); } } } @@ -311,11 +406,12 @@ void Application::Pipe::close(const close_t& arg) if (_open[type]) { int ret; - if ((ret= ::close(_fd[type])) < 0) + if (::close(_fd[type]) == -1) { - Error << "close(" << strerror(ret) << ")"; + Error << "close(" << strerror(errno) << ")"; } _open[type]= false; + _fd[type]= -1; } } @@ -328,6 +424,18 @@ void Application::create_argv(const char *args[]) _argc+= 2; // +2 for libtool --mode=execute } + /* + valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE + */ + if (_use_valgrind) + { + _argc+= 7; + } + else if (_use_gdb) // gdb + { + _argc+= 1; + } + for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++) { _argc++; @@ -355,6 +463,25 @@ void Application::create_argv(const char *args[]) built_argv[x++]= strdup(libtool()); built_argv[x++]= strdup("--mode=execute"); } + + if (_use_valgrind) + { + /* + valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE + */ + built_argv[x++]= strdup("valgrind"); + built_argv[x++]= strdup("--error-exitcode=1"); + built_argv[x++]= strdup("--leak-check=yes"); + built_argv[x++]= strdup("--show-reachable=yes"); + built_argv[x++]= strdup("--track-fds=yes"); + built_argv[x++]= strdup("--malloc-fill=A5"); + built_argv[x++]= strdup("--free-fill=DE"); + } + else if (_use_gdb) + { + built_argv[x++]= strdup("gdb"); + } + built_argv[x++]= strdup(_exectuble_with_path.c_str()); for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++) @@ -378,26 +505,38 @@ void Application::create_argv(const char *args[]) std::string Application::print() { - return print_argv(built_argv, _argc, _pid); + return print_argv(built_argv, _argc); } -void Application::delete_argv() +std::string Application::arguments() { - if (built_argv == NULL) + std::stringstream arg_buffer; + + for (size_t x= 1 + _use_libtool ? 2 : 0; + x < _argc and built_argv[x]; + x++) { - return; + arg_buffer << built_argv[x] << " "; } - for (size_t x= 0; x < _argc; x++) + return arg_buffer.str(); +} + +void Application::delete_argv() +{ + if (built_argv) { - if (built_argv[x]) + for (size_t x= 0; x < _argc; x++) { - ::free(built_argv[x]); + if (built_argv[x]) + { + ::free(built_argv[x]); + } } + delete[] built_argv; + built_argv= NULL; + _argc= 0; } - delete[] built_argv; - built_argv= NULL; - _argc= 0; } @@ -411,9 +550,8 @@ int exec_cmdline(const std::string& command, const char *args[], bool use_libtoo { return int(ret); } - ret= app.wait(); - return int(ret); + return int(app.wait()); } const char *gearmand_binary()