+struct stringCompare {
+ bool operator() (const char *a, const char *b) const {
+ return strcmp(a, b) < 0;
+ }
+};
+
+struct trim_options {
+ /* Calls to be included in trace. */
+ trace::CallSet calls;
+
+ /* Frames to be included in trace. */
+ trace::CallSet frames;
+
+ /* Whether dependency analysis should be performed. */
+ bool dependency_analysis;
+
+ /* Whether uninteresting calls should be pruned.. */
+ bool prune_uninteresting;
+
+ /* Output filename */
+ std::string output;
+
+ /* Emit only calls from this thread (-1 == all threads) */
+ int thread;
+
+ /* Print resulting callset */
+ int print_callset;
+};
+
+static int
+trim_trace(const char *filename, struct trim_options *options)
+{
+ trace::ParseBookmark beginning;
+ trace::Parser p;
+ TraceAnalyzer analyzer;
+ std::set<unsigned> *required;
+ unsigned frame;
+ int call_range_first, call_range_last;
+
+ if (!p.open(filename)) {
+ std::cerr << "error: failed to open " << filename << "\n";
+ return 1;
+ }
+
+ /* Mark the beginning so we can return here for pass 2. */
+ p.getBookmark(beginning);
+
+ /* In pass 1, analyze which calls are needed. */
+ frame = 0;
+ trace::Call *call;
+ while ((call = p.parse_call())) {
+
+ /* There's no use doing any work past the last call or frame
+ * requested by the user. */
+ if (call->no > options->calls.getLast() ||
+ frame > options->frames.getLast()) {
+
+ delete call;
+ break;
+ }
+
+ /* If requested, ignore all calls not belonging to the specified thread. */
+ if (options->thread != -1 && call->thread_id != options->thread) {
+ goto NEXT;
+ }
+
+ /* Also, prune if uninteresting (unless the user asked for no pruning. */
+ if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_VERBOSE) {
+ goto NEXT;
+ }
+
+ /* If this call is included in the user-specified call set,
+ * then require it (and all dependencies) in the trimmed
+ * output. */
+ if (options->calls.contains(*call) ||
+ options->frames.contains(frame, call->flags)) {
+
+ analyzer.require(call);
+ }
+
+ /* Regardless of whether we include this call or not, we do
+ * some dependency tracking (unless disabled by the user). We
+ * do this even for calls we have included in the output so
+ * that any state updates get performed. */
+ if (options->dependency_analysis) {
+ analyzer.analyze(call);
+ }
+
+ NEXT:
+ if (call->flags & trace::CALL_FLAG_END_FRAME)
+ frame++;
+
+ delete call;
+ }
+
+ /* Prepare output file and writer for output. */
+ if (options->output.empty()) {
+ os::String base(filename);
+ base.trimExtension();
+
+ options->output = std::string(base.str()) + std::string("-trim.trace");
+ }
+
+ trace::Writer writer;
+ if (!writer.open(options->output.c_str())) {
+ std::cerr << "error: failed to create " << filename << "\n";
+ return 1;
+ }
+
+ /* Reset bookmark for pass 2. */
+ p.setBookmark(beginning);
+
+ /* In pass 2, emit the calls that are required. */
+ required = analyzer.get_required();
+
+ frame = 0;
+ call_range_first = -1;
+ call_range_last = -1;
+ while ((call = p.parse_call())) {
+
+ /* There's no use doing any work past the last call or frame
+ * requested by the user. */
+ if (call->no > options->calls.getLast() ||
+ frame > options->frames.getLast()) {
+
+ break;
+ }
+
+ if (required->find(call->no) != required->end()) {
+ writer.writeCall(call);
+
+ if (options->print_callset) {
+ if (call_range_first < 0) {
+ call_range_first = call->no;
+ printf ("%d", call_range_first);
+ } else if (call->no != call_range_last + 1) {
+ if (call_range_last != call_range_first)
+ printf ("-%d", call_range_last);
+ call_range_first = call->no;
+ printf (",%d", call_range_first);
+ }
+ call_range_last = call->no;
+ }
+ }
+
+ if (call->flags & trace::CALL_FLAG_END_FRAME) {
+ frame++;
+ }
+
+ delete call;
+ }
+
+ if (options->print_callset) {
+ if (call_range_last != call_range_first)
+ printf ("-%d\n", call_range_last);
+ }
+
+ std::cerr << "Trimmed trace is available as " << options->output << "\n";
+
+ return 0;
+}
+