blob: 13da998f93652b7ef44b3663f2f6b4fb659ddbf1 [file] [log] [blame]
José Fonseca2cce1922012-12-11 19:51:26 +00001#!/usr/bin/env python
2##########################################################################
3#
4# Copyright 2012 VMware Inc.
5# All Rights Reserved.
6#
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:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
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
23# THE SOFTWARE.
24#
25##########################################################################/
26
27'''Convert traces to/from PIX.
28'''
29
30
31import optparse
32import os.path
33import subprocess
34import platform
35import sys
36
37
José Fonseca8b9f78b2013-04-25 15:22:53 +010038def getPixExe():
José Fonseca2cce1922012-12-11 19:51:26 +000039 try:
José Fonseca87f97d22012-12-11 20:37:39 +000040 programFiles = os.environ['ProgramFiles(x86)']
41 except KeyError:
42 programFiles = os.environ['ProgramFiles']
43 try:
José Fonseca2cce1922012-12-11 19:51:26 +000044 dxsdkDir = os.environ['DXSDK_DIR']
45 except KeyError:
46 dxsdkDir = os.path.join(programFiles, "Microsoft DirectX SDL (June 2010)")
José Fonseca8b9f78b2013-04-25 15:22:53 +010047 pixExe = os.path.join(dxsdkDir, "Utilities", "bin", 'x86', 'PIXwin.exe')
48 return pixExe
49
50
51def callProcess(cmd):
52 if options.verbose:
53 sys.stderr.write(' '.join(cmd) + '\n')
54 ret = subprocess.call(cmd)
55 if ret:
56 exeName = os.path.basename(cmd[0])
57 sys.stderr.write('error: %s failed with exit code %u\n' % (exeName, ret))
58 sys.exit(ret)
59 return ret
60
61
62def convertToPix(inTrace, outPixrun):
63 pix = getPixPath()
José Fonseca2cce1922012-12-11 19:51:26 +000064
65 pixExp = os.path.join(os.path.dirname(__file__), 'apitrace.PIXExp')
66
67 # http://social.msdn.microsoft.com/Forums/sv/devdocs/thread/15addc0c-036d-413a-854a-35637ccbb834
68 # http://src.chromium.org/svn/trunk/o3d/tests/test_driver.py
69 cmd = [
José Fonseca8b9f78b2013-04-25 15:22:53 +010070 getPixExe(),
José Fonseca2cce1922012-12-11 19:51:26 +000071 pixExp,
72 '-start',
73 '-runfile', os.path.abspath(outPixrun),
74 '-targetpath', os.path.abspath(options.retrace),
75 #'-targetstartfolder', ...,
76 '-targetargs', os.path.abspath(inTrace),
77 ]
78
José Fonseca8b9f78b2013-04-25 15:22:53 +010079 callProcess(cmd)
José Fonseca2cce1922012-12-11 19:51:26 +000080 if os.path.exists(outPixrun):
81 sys.stderr.write('info: %s written\n' % outPixrun)
José Fonseca8b9f78b2013-04-25 15:22:53 +010082 if options.verify:
José Fonseca2cce1922012-12-11 19:51:26 +000083 subprocess.call([pix, os.path.abspath(outPixrun)])
84 else:
85 sys.stderr.write('error: %s not written\n' % outPixrun)
86 sys.exit(1)
87
88
José Fonseca8b9f78b2013-04-25 15:22:53 +010089def convertFromPix(inPix, outTrace):
90 pixExe = getPixExe()
91
92 if False:
José Fonseca5ad7df82013-04-26 12:05:05 +010093 # TODO: Use -exporttocsv option to detect which API to use
José Fonseca8b9f78b2013-04-25 15:22:53 +010094 cmd = [
95 pixExe,
96 inPix,
José Fonseca5ad7df82013-04-26 12:05:05 +010097 '-exporttocsv', # XXX: output filename is ignored
José Fonseca8b9f78b2013-04-25 15:22:53 +010098 ]
99 callProcess(cmd)
100
101 cmd = [
102 options.apitrace,
103 'trace',
104 '-a', options.api,
105 '-o', outTrace,
106 pixExe,
107 inPix,
108 '-playstandalone',
109 ]
110
111 callProcess(cmd)
112 if os.path.exists(outTrace):
113 sys.stderr.write('info: %s written\n' % outTrace)
114 if options.verify:
115 subprocess.call([options.retrace, os.path.abspath(outTrace)])
116 else:
117 sys.stderr.write('error: %s not written\n' % outTrace)
118 sys.exit(1)
119
120
José Fonseca2cce1922012-12-11 19:51:26 +0000121def main():
122 global options
123
124 # Parse command line options
125 optparser = optparse.OptionParser(
126 usage='\n\t%prog [options] <trace> ...',
127 version='%%prog')
128 optparser.add_option(
José Fonseca8b9f78b2013-04-25 15:22:53 +0100129 '--apitrace', metavar='PROGRAM',
130 type='string', dest='apitrace', default='apitrace.exe',
131 help='retrace command [default: %default]')
132 optparser.add_option(
133 '-a', '--api', metavar='API',
134 type='string', dest='api', default='d3d9',
135 help='api [default: %default]')
136 optparser.add_option(
José Fonseca2cce1922012-12-11 19:51:26 +0000137 '-r', '--retrace', metavar='PROGRAM',
José Fonseca8b9f78b2013-04-25 15:22:53 +0100138 type='string', dest='retrace', default='d3dretrace.exe',
José Fonseca2cce1922012-12-11 19:51:26 +0000139 help='retrace command [default: %default]')
140 optparser.add_option(
141 '-v', '--verbose',
142 action='store_true', dest='verbose', default=False,
143 help='verbose output')
144 optparser.add_option(
145 '-o', '--output', metavar='FILE',
146 type="string", dest="output",
147 help="output file [default: stdout]")
José Fonseca8b9f78b2013-04-25 15:22:53 +0100148 optparser.add_option(
149 '--verify',
150 action='store_true', dest='verify', default=False,
151 help='verify output by replaying it')
José Fonseca2cce1922012-12-11 19:51:26 +0000152
153 (options, args) = optparser.parse_args(sys.argv[1:])
154 if not args:
155 optparser.error("incorrect number of arguments")
156
José Fonseca8b9f78b2013-04-25 15:22:53 +0100157 for inFile in args:
158 name, inExt = os.path.splitext(os.path.basename(inFile))
159 inExt = inExt
160 if inExt.lower() == '.trace':
161 convert = convertToPix
162 outExt = '.PIXRun'
163 elif inExt.lower() == '.pixrun':
164 convert = convertFromPix
165 outExt = '.trace'
José Fonseca2cce1922012-12-11 19:51:26 +0000166 else:
José Fonseca8b9f78b2013-04-25 15:22:53 +0100167 optparser.error("unexpected file extensions `%s`" % inExt)
168 if options.output:
169 outFile = options.output
170 else:
171 outFile = name + outExt
172 convert(inFile, outFile)
José Fonseca2cce1922012-12-11 19:51:26 +0000173
174
175if __name__ == '__main__':
176 main()