]> git.cworth.org Git - apitrace/blobdiff - cli/cli_trim.cpp
qapitrace: Determine binary dir in run-time.
[apitrace] / cli / cli_trim.cpp
index 04222622c31e259d61dbaba9f628f969b63bd868..eea55535e95395f8707afd7f6d1090e6edc873ec 100644 (file)
  *
  **************************************************************************/
 
+
 #include <string.h>
+#include <limits.h> // for CHAR_MAX
+#include <getopt.h>
 
 #include "cli.hpp"
 
 #include "os_string.hpp"
 
+#include "trace_callset.hpp"
 #include "trace_parser.hpp"
 #include "trace_writer.hpp"
 
@@ -39,63 +43,101 @@ static void
 usage(void)
 {
     std::cout
-        << "usage: apitrace trim <trace-file>\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 retain specified calls\n"
+        "        --thread=THREAD_ID   only retain calls from specified thread\n"
+        "    -o, --output=TRACE_FILE  output trace file\n"
+        "\n"
+    ;
 }
 
+enum {
+    CALLS_OPT = CHAR_MAX + 1,
+    THREAD_OPT,
+};
+
+const static char *
+shortOptions = "ho:";
+
+const static struct option
+longOptions[] = {
+    {"help", no_argument, 0, 'h'},
+    {"calls", required_argument, 0, CALLS_OPT},
+    {"thread", required_argument, 0, THREAD_OPT},
+    {"output", required_argument, 0, 'o'},
+    {0, 0, 0, 0}
+};
+
 static int
 command(int argc, char *argv[])
 {
+    std::string output;
+    trace::CallSet calls(trace::FREQUENCY_ALL);
+    int thread = -1;
     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 THREAD_OPT:
+            thread = atoi(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])) {
+            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::Writer writer;
-    writer.open(output.c_str());
+        trace::Call *call;
+        while ((call = p.parse_call())) {
+            if (calls.contains(*call) &&
+                (thread == -1 || call->thread_id == thread)) {
+                writer.writeCall(call);
+            }
+            delete call;
+        }
 
-    trace::Call *call;
-    while ((call = p.parse_call())) {
-        writer.writeCall(call);
-        delete call;
+        std::cout << "Trimmed trace is available as " << output << "\n";
     }
 
-    std::cout << "Trimmed trace is available as " << output << "\n";
-
     return 0;
 }