X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=cli%2Fcli_trim.cpp;h=2ea525103991ba574e3f4a59aa38916926ad55e4;hb=2e93bcb671b82e75289cdd868bb5d4f9fc2c9134;hp=b35e14429d48786f78c2f29242fe7ba9d8898257;hpb=4c5f6fa4d7474bc2a13a6c00bd3f4ac47ff56920;p=apitrace diff --git a/cli/cli_trim.cpp b/cli/cli_trim.cpp index b35e144..2ea5251 100644 --- a/cli/cli_trim.cpp +++ b/cli/cli_trim.cpp @@ -24,14 +24,18 @@ * **************************************************************************/ + #include +#include // for CHAR_MAX +#include #include "cli.hpp" #include "os_string.hpp" +#include "trace_callset.hpp" #include "trace_parser.hpp" -#include "trace_copier.hpp" +#include "trace_writer.hpp" static const char *synopsis = "Create a new trace by trimming an existing trace."; @@ -39,62 +43,94 @@ static void usage(void) { std::cout - << "usage: apitrace trim \n" - << synopsis << "\n"; + << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" + << synopsis << "\n" + "\n" + " -h, --help show this help message and exit\n" + " --calls=CALLSET only trim specified calls\n" + " -o, --output=TRACE_FILE output trace file\n" + "\n" + ; } +enum { + CALLS_OPT = CHAR_MAX + 1, +}; + +const static char * +shortOptions = "ho"; + +const static struct option +longOptions[] = { + {"help", no_argument, 0, 'h'}, + {"calls", required_argument, 0, CALLS_OPT}, + {"output", optional_argument, 0, 'o'}, + {0, 0, 0, 0} +}; + static int command(int argc, char *argv[]) { + std::string output; + trace::CallSet calls(trace::FREQUENCY_ALL); int i; - for (i = 0; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - break; - } else if (!strcmp(arg, "--help")) { + int opt; + while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { + switch (opt) { + case 'h': usage(); return 0; - } else { - std::cerr << "error: unknown option " << arg << "\n"; + case CALLS_OPT: + calls = trace::CallSet(optarg); + break; + case 'o': + output = optarg; + break; + default: + std::cerr << "error: unexpected option `" << opt << "`\n"; usage(); return 1; } } - if (i >= argc) { - std::cerr << "Error: apitrace trim requires a trace file as an argument.\n"; + if (optind >= argc) { + std::cerr << "error: apitrace trim requires a trace file as an argument.\n"; usage(); return 1; } - trace::Parser p; + for (i = optind; i < argc; ++i) { + trace::Parser p; + if (!p.open(argv[i])) { + std::cerr << "error: failed to open " << argv[i] << "\n"; + return 1; + } - if (!p.open(argv[i])) { - std::cerr << "error: failed to open " << argv[i] << "\n"; - return 1; - } + if (output.empty()) { + os::String base(argv[i]); + base.trimExtension(); - os::String base(argv[i]); - base.trimExtension(); + output = std::string(base.str()) + std::string("-trim.trace"); + } - std::string output = std::string(base.str()) + std::string("-trim.trace"); + trace::Writer writer; + if (!writer.open(output.c_str())) { + std::cerr << "error: failed to create " << argv[i] << "\n"; + return 1; + } - trace::Copier copier(output.c_str()); + trace::Call *call; + while ((call = p.parse_call())) { + if (calls.contains(*call)) { + writer.writeCall(call); + } + delete call; + } - trace::Call *call; - while ((call = p.parse_call())) { - copier.visit(call); - delete call; + std::cout << "Trimmed trace is available as " << output << "\n"; } - std::cout << "Trimmed trace is available as " << output << "\n"; - return 0; }