1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
4 * Copyright 2011 Intel corporation
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
29 #include <limits.h> // for CHAR_MAX
39 #include "os_string.hpp"
41 #include "trace_callset.hpp"
42 #include "trace_parser.hpp"
43 #include "trace_writer.hpp"
45 #define MAX(a, b) ((a) > (b) ? (a) : (b))
46 #define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
48 static const char *synopsis = "Create a new trace by trimming an existing trace.";
54 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
57 " -h, --help Show detailed help for trim options and exit\n"
58 " --calls=CALLSET Include specified calls in the trimmed output.\n"
59 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
60 " --deps Include additional calls to satisfy dependencies\n"
61 " --no-deps Do not include calls from dependency analysis\n"
62 " --prune Omit uninteresting calls from the trace output\n"
63 " --no-prune Do not prune uninteresting calls from the trace.\n"
64 " -x, --exact Trim exactly to calls specified in --calls/--frames\n"
65 " Equivalent to both --no-deps and --no-prune\n"
66 " --print-callset Print the final set of calls included in output\n"
67 " --thread=THREAD_ID Only retain calls from specified thread\n"
68 " -o, --output=TRACE_FILE Output trace file\n"
76 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
79 " -h, --help Show this help message and exit\n"
81 " --calls=CALLSET Include specified calls in the trimmed output.\n"
82 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
83 " Note that due to dependency analysis and pruning\n"
84 " of uninteresting calls the resulting trace may\n"
85 " include more and less calls than specified.\n"
86 " See --no-deps, --no-prune, and --exact to change\n"
89 " --deps Perform dependency analysis and include dependent\n"
90 " calls as needed, (even if those calls were not\n"
91 " explicitly requested with --calls or --frames).\n"
92 " This is the default behavior. See --no-deps and\n"
93 " --exact to change the behavior.\n"
95 " --no-deps Do not perform dependency analysis. In this mode\n"
96 " the trimmed trace will never include calls from\n"
97 " outside what is specified in --calls or --frames.\n"
99 " --prune Omit calls with no side effects, even if the call\n"
100 " is within the range specified by --calls/--frames.\n"
101 " This is the default behavior. See --no-prune.\n"
103 " --no-prune Do not prune uninteresting calls from the trace.\n"
104 " In this mode the trimmed trace will never omit\n"
105 " any calls within the user-specified range.\n"
107 " -x, --exact Trim the trace to exactly the calls specified in\n"
108 " --calls and --frames. This option is equivalent\n"
109 " to passing both --no-deps and --no-prune.\n"
111 " --print-callset Print to stdout the final set of calls included\n"
112 " in the trim output. This can be useful for\n"
113 " debugging trim operations by using a modified\n"
114 " callset on the command-line along with --exact.\n"
115 " Use --calls=@<file> to read callset from a file.\n"
117 " --thread=THREAD_ID Only retain calls from specified thread\n"
119 " -o, --output=TRACE_FILE Output trace file\n"
125 CALLS_OPT = CHAR_MAX + 1,
136 shortOptions = "ho:x";
138 const static struct option
140 {"help", no_argument, 0, 'h'},
141 {"calls", required_argument, 0, CALLS_OPT},
142 {"frames", required_argument, 0, FRAMES_OPT},
143 {"deps", no_argument, 0, DEPS_OPT},
144 {"no-deps", no_argument, 0, NO_DEPS_OPT},
145 {"prune", no_argument, 0, PRUNE_OPT},
146 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
147 {"exact", no_argument, 0, 'x'},
148 {"thread", required_argument, 0, THREAD_OPT},
149 {"output", required_argument, 0, 'o'},
150 {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
154 struct stringCompare {
155 bool operator() (const char *a, const char *b) const {
156 return strcmp(a, b) < 0;
160 class TraceAnalyzer {
161 /* Maps for tracking resource dependencies between calls. */
162 std::map<std::string, std::set<unsigned> > resources;
163 std::map<std::string, std::set<std::string> > dependencies;
165 /* Maps for tracking OpenGL state. */
166 std::map<GLenum, unsigned> texture_map;
168 /* The final set of calls required. This consists of calls added
169 * explicitly with the require() method as well as all calls
170 * implicitly required by those through resource dependencies. */
171 std::set<unsigned> required;
173 bool transformFeedbackActive;
174 bool framebufferObjectActive;
176 GLuint activeProgram;
177 GLenum activeTextureUnit;
179 /* Rendering often has no side effects, but it can in some cases,
180 * (such as when transform feedback is active, or when rendering
181 * targets a framebuffer object). */
182 bool renderingHasSideEffect() {
183 return transformFeedbackActive || framebufferObjectActive;
186 /* Provide: Record that the given call affects the given resource
187 * as a side effect. */
188 void provide(std::string resource, trace::CallNo call_no) {
189 resources[resource].insert(call_no);
192 /* Like provide, but with a simply-formatted string, (appending an
193 * integer to the given string). */
194 void providef(std::string resource, int resource_no, trace::CallNo call_no) {
195 std::stringstream ss;
196 ss << resource << resource_no;
197 provide(ss.str(), call_no);
200 /* Link: Establish a dependency between resource 'resource' and
201 * resource 'dependency'. This dependency is captured by name so
202 * that if the list of calls that provide 'dependency' grows
203 * before 'resource' is consumed, those calls will still be
205 void link(std::string resource, std::string dependency) {
206 dependencies[resource].insert(dependency);
209 /* Like link, but with a simply-formatted string, (appending an
210 * integer to the given string). */
211 void linkf(std::string resource, std::string dependency, int dep_no) {
213 std::stringstream ss;
214 ss << dependency << dep_no;
215 link(resource, ss.str());
218 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
219 void unlink(std::string resource, std::string dependency) {
220 dependencies[resource].erase(dependency);
221 if (dependencies[resource].size() == 0) {
222 dependencies.erase(resource);
226 /* Like unlink, but with a simply-formated string, (appending an
227 * integer to the given string). */
228 void unlinkf(std::string resource, std::string dependency, int dep_no) {
230 std::stringstream ss;
231 ss << dependency << dep_no;
232 unlink(resource, ss.str());
235 /* Unlink all: Remove dependencies from 'resource' to all other
237 void unlinkAll(std::string resource) {
238 dependencies.erase(resource);
241 /* Resolve: Recursively compute all calls providing 'resource',
242 * (including linked dependencies of 'resource' on other
244 std::set<unsigned> resolve(std::string resource) {
245 std::set<std::string> *deps;
246 std::set<std::string>::iterator dep;
248 std::set<unsigned> *calls;
249 std::set<unsigned>::iterator call;
251 std::set<unsigned> result, deps_set;
253 /* Recursively chase dependencies. */
254 if (dependencies.count(resource)) {
255 deps = &dependencies[resource];
256 for (dep = deps->begin(); dep != deps->end(); dep++) {
257 deps_set = resolve(*dep);
258 for (call = deps_set.begin(); call != deps_set.end(); call++) {
259 result.insert(*call);
264 /* Also look for calls that directly provide 'resource' */
265 if (resources.count(resource)) {
266 calls = &resources[resource];
267 for (call = calls->begin(); call != calls->end(); call++) {
268 result.insert(*call);
275 /* Consume: Resolve all calls that provide the given resource, and
276 * add them to the required list. Then clear the call list for
277 * 'resource' along with any dependencies. */
278 void consume(std::string resource) {
280 std::set<unsigned> calls;
281 std::set<unsigned>::iterator call;
283 calls = resolve(resource);
285 dependencies.erase(resource);
286 resources.erase(resource);
288 for (call = calls.begin(); call != calls.end(); call++) {
289 required.insert(*call);
293 void stateTrackPreCall(trace::Call *call) {
295 const char *name = call->name();
297 if (strcmp(name, "glBegin") == 0) {
298 insideBeginEnd = true;
302 if (strcmp(name, "glBeginTransformFeedback") == 0) {
303 transformFeedbackActive = true;
307 if (strcmp(name, "glActiveTexture") == 0) {
308 activeTextureUnit = static_cast<GLenum>(call->arg(0).toSInt());
312 if (strcmp(name, "glBindTexture") == 0) {
316 target = static_cast<GLenum>(call->arg(0).toSInt());
317 texture = call->arg(1).toUInt();
320 texture_map.erase(target);
322 texture_map[target] = texture;
328 if (strcmp(name, "glUseProgram") == 0) {
329 activeProgram = call->arg(0).toUInt();
332 if (strcmp(name, "glBindFramebuffer") == 0) {
336 target = static_cast<GLenum>(call->arg(0).toSInt());
337 framebuffer = call->arg(1).toUInt();
339 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
340 if (framebuffer == 0) {
341 framebufferObjectActive = false;
343 framebufferObjectActive = true;
350 void stateTrackPostCall(trace::Call *call) {
352 const char *name = call->name();
354 if (strcmp(name, "glEnd") == 0) {
355 insideBeginEnd = false;
359 if (strcmp(name, "glEndTransformFeedback") == 0) {
360 transformFeedbackActive = false;
364 /* If this swapbuffers was included in the trace then it will
365 * have already consumed all framebuffer dependencies. If not,
366 * then clear them now so that they don't carry over into the
368 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
369 call->flags & trace::CALL_FLAG_END_FRAME) {
370 dependencies.erase("framebuffer");
371 resources.erase("framebuffer");
376 void recordSideEffects(trace::Call *call) {
378 const char *name = call->name();
380 /* If call is flagged as no side effects, then we are done here. */
381 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
385 /* Similarly, swap-buffers calls don't have interesting side effects. */
386 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
387 call->flags & trace::CALL_FLAG_END_FRAME) {
391 if (strcmp(name, "glGenTextures") == 0) {
392 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
397 for (i = 0; i < textures->size(); i++) {
398 texture = textures->values[i]->toUInt();
399 providef("texture-", texture, call->no);
405 if (strcmp(name, "glBindTexture") == 0) {
409 std::stringstream ss_target, ss_texture;
411 target = static_cast<GLenum>(call->arg(0).toSInt());
412 texture = call->arg(1).toUInt();
414 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
415 ss_texture << "texture-" << texture;
417 resources.erase(ss_target.str());
418 provide(ss_target.str(), call->no);
420 unlinkAll(ss_target.str());
421 link(ss_target.str(), ss_texture.str());
426 /* FIXME: Need to handle glMultTexImage and friends. */
427 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
428 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
429 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
430 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
431 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
432 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
433 strcmp(name, "glInvalidateTexImage") == 0 ||
434 strcmp(name, "glInvalidateTexSubImage") == 0) {
436 std::set<unsigned> *calls;
437 std::set<unsigned>::iterator c;
438 std::stringstream ss_target, ss_texture;
440 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
442 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
443 ss_texture << "texture-" << texture_map[target];
445 /* The texture resource depends on this call and any calls
446 * providing the given texture target. */
447 provide(ss_texture.str(), call->no);
449 if (resources.count(ss_target.str())) {
450 calls = &resources[ss_target.str()];
451 for (c = calls->begin(); c != calls->end(); c++) {
452 provide(ss_texture.str(), *c);
459 if (strcmp(name, "glEnable") == 0) {
462 cap = static_cast<GLenum>(call->arg(0).toSInt());
464 if (cap == GL_TEXTURE_1D ||
465 cap == GL_TEXTURE_2D ||
466 cap == GL_TEXTURE_3D ||
467 cap == GL_TEXTURE_CUBE_MAP)
469 std::stringstream ss;
471 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
473 link("render-state", ss.str());
476 provide("state", call->no);
480 if (strcmp(name, "glDisable") == 0) {
483 cap = static_cast<GLenum>(call->arg(0).toSInt());
485 if (cap == GL_TEXTURE_1D ||
486 cap == GL_TEXTURE_2D ||
487 cap == GL_TEXTURE_3D ||
488 cap == GL_TEXTURE_CUBE_MAP)
490 std::stringstream ss;
492 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
494 unlink("render-state", ss.str());
497 provide("state", call->no);
501 if (strcmp(name, "glCreateShader") == 0 ||
502 strcmp(name, "glCreateShaderObjectARB") == 0) {
504 GLuint shader = call->ret->toUInt();
505 providef("shader-", shader, call->no);
509 if (strcmp(name, "glShaderSource") == 0 ||
510 strcmp(name, "glShaderSourceARB") == 0 ||
511 strcmp(name, "glCompileShader") == 0 ||
512 strcmp(name, "glCompileShaderARB") == 0 ||
513 strcmp(name, "glGetShaderiv") == 0 ||
514 strcmp(name, "glGetShaderInfoLog") == 0) {
516 GLuint shader = call->arg(0).toUInt();
517 providef("shader-", shader, call->no);
521 if (strcmp(name, "glCreateProgram") == 0 ||
522 strcmp(name, "glCreateProgramObjectARB") == 0) {
524 GLuint program = call->ret->toUInt();
525 providef("program-", program, call->no);
529 if (strcmp(name, "glAttachShader") == 0 ||
530 strcmp(name, "glAttachObjectARB") == 0) {
532 GLuint program, shader;
533 std::stringstream ss_program, ss_shader;
535 program = call->arg(0).toUInt();
536 shader = call->arg(1).toUInt();
538 ss_program << "program-" << program;
539 ss_shader << "shader-" << shader;
541 link(ss_program.str(), ss_shader.str());
542 provide(ss_program.str(), call->no);
547 if (strcmp(name, "glDetachShader") == 0 ||
548 strcmp(name, "glDetachObjectARB") == 0) {
550 GLuint program, shader;
551 std::stringstream ss_program, ss_shader;
553 program = call->arg(0).toUInt();
554 shader = call->arg(1).toUInt();
556 ss_program << "program-" << program;
557 ss_shader << "shader-" << shader;
559 unlink(ss_program.str(), ss_shader.str());
564 if (strcmp(name, "glUseProgram") == 0 ||
565 strcmp(name, "glUseProgramObjectARB") == 0) {
569 program = call->arg(0).toUInt();
571 unlinkAll("render-program-state");
574 unlink("render-state", "render-program-state");
575 provide("state", call->no);
577 std::stringstream ss;
579 ss << "program-" << program;
581 link("render-state", "render-program-state");
582 link("render-program-state", ss.str());
584 provide(ss.str(), call->no);
590 if (strcmp(name, "glGetUniformLocation") == 0 ||
591 strcmp(name, "glGetUniformLocationARB") == 0 ||
592 strcmp(name, "glGetFragDataLocation") == 0 ||
593 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
594 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
595 strcmp(name, "glGetProgramResourceLocation") == 0 ||
596 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
597 strcmp(name, "glGetVaryingLocationNV") == 0) {
599 GLuint program = call->arg(0).toUInt();
601 providef("program-", program, call->no);
606 /* For any call that accepts 'location' as its first argument,
607 * perform a lookup in our location->program map and add a
608 * dependence on the program we find there. */
609 if (call->sig->num_args > 0 &&
610 strcmp(call->sig->arg_names[0], "location") == 0) {
612 providef("program-", activeProgram, call->no);
614 /* We can't easily tell if this uniform is being used to
615 * associate a sampler in the shader with a texture
616 * unit. The conservative option is to assume that it is
617 * and create a link from the active program to any bound
618 * textures for the given unit number.
620 * FIXME: We should be doing the same thing for calls to
622 if (strcmp(name, "glUniform1i") == 0 ||
623 strcmp(name, "glUniform1iARB") == 0) {
625 GLint max_unit = MAX(GL_MAX_TEXTURE_COORDS, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
627 GLint unit = call->arg(1).toSInt();
628 std::stringstream ss_program;
629 std::stringstream ss_texture;
631 if (unit < max_unit) {
633 ss_program << "program-" << activeProgram;
635 ss_texture << "texture-unit-" << GL_TEXTURE0 + unit << "-target-";
637 /* We don't know what target(s) might get bound to
638 * this texture unit, so conservatively link to
639 * all. Only bound textures will actually get inserted
640 * into the output call stream. */
641 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_1D);
642 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_2D);
643 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_3D);
644 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_CUBE_MAP);
651 /* FIXME: We cut a huge swath by assuming that any unhandled
652 * call that has a first argument named "program" should not
653 * be included in the trimmed output unless the program of
654 * that number is also included.
656 * This heuristic is correct for many cases, but we should
657 * actually carefully verify if this includes some calls
658 * inappropriately, or if it misses some.
660 if (strcmp(name, "glLinkProgram") == 0 ||
661 strcmp(name, "glLinkProgramARB") == 0 ||
662 (call->sig->num_args > 0 &&
663 (strcmp(call->sig->arg_names[0], "program") == 0 ||
664 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
666 GLuint program = call->arg(0).toUInt();
667 providef("program-", program, call->no);
671 /* Handle all rendering operations, (even though only glEnd is
672 * flagged as a rendering operation we treat everything from
673 * glBegin through glEnd as a rendering operation). */
674 if (call->flags & trace::CALL_FLAG_RENDER ||
677 std::set<unsigned> calls;
678 std::set<unsigned>::iterator c;
680 provide("framebuffer", call->no);
682 calls = resolve("render-state");
684 for (c = calls.begin(); c != calls.end(); c++) {
685 provide("framebuffer", *c);
688 /* In some cases, rendering has side effects beyond the
689 * framebuffer update. */
690 if (renderingHasSideEffect()) {
691 provide("state", call->no);
692 for (c = calls.begin(); c != calls.end(); c++) {
693 provide("state", *c);
700 /* By default, assume this call affects the state somehow. */
701 resources["state"].insert(call->no);
704 void requireDependencies(trace::Call *call) {
706 /* Swap-buffers calls depend on framebuffer state. */
707 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
708 call->flags & trace::CALL_FLAG_END_FRAME) {
709 consume("framebuffer");
712 /* By default, just assume this call depends on generic state. */
718 TraceAnalyzer(): transformFeedbackActive(false),
719 framebufferObjectActive(false),
720 insideBeginEnd(false),
721 activeTextureUnit(GL_TEXTURE0)
726 /* Analyze this call by tracking state and recording all the
727 * resources provided by this call as side effects.. */
728 void analyze(trace::Call *call) {
730 stateTrackPreCall(call);
732 recordSideEffects(call);
734 stateTrackPostCall(call);
737 /* Require this call and all of its dependencies to be included in
738 * the final trace. */
739 void require(trace::Call *call) {
741 /* First, find and insert all calls that this call depends on. */
742 requireDependencies(call);
744 /* Then insert this call itself. */
745 required.insert(call->no);
748 /* Return a set of all the required calls, (both those calls added
749 * explicitly with require() and those implicitly depended
751 std::set<unsigned> *get_required(void) {
756 struct trim_options {
757 /* Calls to be included in trace. */
758 trace::CallSet calls;
760 /* Frames to be included in trace. */
761 trace::CallSet frames;
763 /* Whether dependency analysis should be performed. */
764 bool dependency_analysis;
766 /* Whether uninteresting calls should be pruned.. */
767 bool prune_uninteresting;
769 /* Output filename */
772 /* Emit only calls from this thread (-1 == all threads) */
775 /* Print resulting callset */
780 trim_trace(const char *filename, struct trim_options *options)
782 trace::ParseBookmark beginning;
784 TraceAnalyzer analyzer;
785 std::set<unsigned> *required;
787 int call_range_first, call_range_last;
789 if (!p.open(filename)) {
790 std::cerr << "error: failed to open " << filename << "\n";
794 /* Mark the beginning so we can return here for pass 2. */
795 p.getBookmark(beginning);
797 /* In pass 1, analyze which calls are needed. */
800 while ((call = p.parse_call())) {
802 /* There's no use doing any work past the last call or frame
803 * requested by the user. */
804 if (call->no > options->calls.getLast() ||
805 frame > options->frames.getLast()) {
811 /* If requested, ignore all calls not belonging to the specified thread. */
812 if (options->thread != -1 && call->thread_id != options->thread) {
816 /* Also, prune if uninteresting (unless the user asked for no pruning. */
817 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
821 /* If this call is included in the user-specified call set,
822 * then require it (and all dependencies) in the trimmed
824 if (options->calls.contains(*call) ||
825 options->frames.contains(frame, call->flags)) {
827 analyzer.require(call);
830 /* Regardless of whether we include this call or not, we do
831 * some dependency tracking (unless disabled by the user). We
832 * do this even for calls we have included in the output so
833 * that any state updates get performed. */
834 if (options->dependency_analysis) {
835 analyzer.analyze(call);
839 if (call->flags & trace::CALL_FLAG_END_FRAME)
845 /* Prepare output file and writer for output. */
846 if (options->output.empty()) {
847 os::String base(filename);
848 base.trimExtension();
850 options->output = std::string(base.str()) + std::string("-trim.trace");
853 trace::Writer writer;
854 if (!writer.open(options->output.c_str())) {
855 std::cerr << "error: failed to create " << filename << "\n";
859 /* Reset bookmark for pass 2. */
860 p.setBookmark(beginning);
862 /* In pass 2, emit the calls that are required. */
863 required = analyzer.get_required();
866 call_range_first = -1;
867 call_range_last = -1;
868 while ((call = p.parse_call())) {
870 /* There's no use doing any work past the last call or frame
871 * requested by the user. */
872 if (call->no > options->calls.getLast() ||
873 frame > options->frames.getLast()) {
878 if (required->find(call->no) != required->end()) {
879 writer.writeCall(call);
881 if (options->print_callset) {
882 if (call_range_first < 0) {
883 call_range_first = call->no;
884 printf ("%d", call_range_first);
885 } else if (call->no != call_range_last + 1) {
886 if (call_range_last != call_range_first)
887 printf ("-%d", call_range_last);
888 call_range_first = call->no;
889 printf (",%d", call_range_first);
891 call_range_last = call->no;
895 if (call->flags & trace::CALL_FLAG_END_FRAME) {
902 if (options->print_callset) {
903 if (call_range_last != call_range_first)
904 printf ("-%d\n", call_range_last);
907 std::cout << "Trimmed trace is available as " << options->output << "\n";
913 command(int argc, char *argv[])
915 struct trim_options options;
917 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
918 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
919 options.dependency_analysis = true;
920 options.prune_uninteresting = true;
923 options.print_callset = 0;
926 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
932 options.calls = trace::CallSet(optarg);
935 options.frames = trace::CallSet(optarg);
938 options.dependency_analysis = true;
941 options.dependency_analysis = false;
944 options.prune_uninteresting = true;
947 options.prune_uninteresting = false;
950 options.dependency_analysis = false;
951 options.prune_uninteresting = false;
954 options.thread = atoi(optarg);
957 options.output = optarg;
959 case PRINT_CALLSET_OPT:
960 options.print_callset = 1;
963 std::cerr << "error: unexpected option `" << opt << "`\n";
969 /* If neither of --calls nor --frames was set, default to the
970 * entire set of calls. */
971 if (options.calls.empty() && options.frames.empty()) {
972 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
975 if (optind >= argc) {
976 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
981 if (argc > optind + 1) {
982 std::cerr << "error: extraneous arguments:";
983 for (int i = optind + 1; i < argc; i++) {
984 std::cerr << " " << argv[i];
991 return trim_trace(argv[optind], &options);
994 const Command trim_command = {