main.py revision 2973:56dea3a9d279
19885Sstever@gmail.com# Copyright (c) 2005 The Regents of The University of Michigan
29885Sstever@gmail.com# All rights reserved.
39885Sstever@gmail.com#
410036SAli.Saidi@ARM.com# Redistribution and use in source and binary forms, with or without
59885Sstever@gmail.com# modification, are permitted provided that the following conditions are
610036SAli.Saidi@ARM.com# met: redistributions of source code must retain the above copyright
79885Sstever@gmail.com# notice, this list of conditions and the following disclaimer;
89885Sstever@gmail.com# redistributions in binary form must reproduce the above copyright
99885Sstever@gmail.com# notice, this list of conditions and the following disclaimer in the
109885Sstever@gmail.com# documentation and/or other materials provided with the distribution;
119885Sstever@gmail.com# neither the name of the copyright holders nor the names of its
129885Sstever@gmail.com# contributors may be used to endorse or promote products derived from
1310315Snilay@cs.wisc.edu# this software without specific prior written permission.
149885Sstever@gmail.com#
159885Sstever@gmail.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
169885Sstever@gmail.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1710036SAli.Saidi@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1811312Santhony.gutierrez@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
199885Sstever@gmail.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
209885Sstever@gmail.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2110315Snilay@cs.wisc.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229885Sstever@gmail.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2310315Snilay@cs.wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249885Sstever@gmail.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
259885Sstever@gmail.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269885Sstever@gmail.com#
2710736Snilay@cs.wisc.edu# Authors: Nathan Binkert
2811219Snilay@cs.wisc.edu
299885Sstever@gmail.comimport code, optparse, os, socket, sys
309885Sstever@gmail.comfrom datetime import datetime
319885Sstever@gmail.comfrom attrdict import attrdict
3211440SCurtis.Dunham@arm.com
3311440SCurtis.Dunham@arm.comtry:
349885Sstever@gmail.com    import info
359885Sstever@gmail.comexcept ImportError:
369885Sstever@gmail.com    info = None
379885Sstever@gmail.com
389885Sstever@gmail.com__all__ = [ 'options', 'arguments', 'main' ]
399885Sstever@gmail.com
409885Sstever@gmail.comusage="%prog [m5 options] script.py [script options]"
419885Sstever@gmail.comversion="%prog 2.0"
429885Sstever@gmail.combrief_copyright='''
439885Sstever@gmail.comCopyright (c) 2001-2006
449885Sstever@gmail.comThe Regents of The University of Michigan
459885Sstever@gmail.comAll Rights Reserved
469885Sstever@gmail.com'''
4710315Snilay@cs.wisc.edu
4810036SAli.Saidi@ARM.com# there's only one option parsing done, so make it global and add some
4910315Snilay@cs.wisc.edu# helper functions to make it work well.
509885Sstever@gmail.comparser = optparse.OptionParser(usage=usage, version=version,
519885Sstever@gmail.com                               description=brief_copyright,
529885Sstever@gmail.com                               formatter=optparse.TitledHelpFormatter())
539885Sstever@gmail.comparser.disable_interspersed_args()
5410036SAli.Saidi@ARM.com
559885Sstever@gmail.com# current option group
569885Sstever@gmail.comgroup = None
579885Sstever@gmail.com
589885Sstever@gmail.comdef set_group(*args, **kwargs):
599885Sstever@gmail.com    '''set the current option group'''
609885Sstever@gmail.com    global group
619885Sstever@gmail.com    if not args and not kwargs:
6210036SAli.Saidi@ARM.com        group = None
639885Sstever@gmail.com    else:
649885Sstever@gmail.com        group = parser.add_option_group(*args, **kwargs)
659885Sstever@gmail.com
6610315Snilay@cs.wisc.educlass splitter(object):
6710315Snilay@cs.wisc.edu    def __init__(self, split):
6810315Snilay@cs.wisc.edu        self.split = split
6910315Snilay@cs.wisc.edu    def __call__(self, option, opt_str, value, parser):
7010315Snilay@cs.wisc.edu        getattr(parser.values, option.dest).extend(value.split(self.split))
7110315Snilay@cs.wisc.edu
7210315Snilay@cs.wisc.edudef add_option(*args, **kwargs):
7310315Snilay@cs.wisc.edu    '''add an option to the current option group, or global none set'''
749885Sstever@gmail.com
7510451Snilay@cs.wisc.edu    # if action=split, but allows the option arguments
769885Sstever@gmail.com    # themselves to be lists separated by the split variable'''
7710036SAli.Saidi@ARM.com
7810736Snilay@cs.wisc.edu    if kwargs.get('action', None) == 'append' and 'split' in kwargs:
7910736Snilay@cs.wisc.edu        split = kwargs.pop('split')
8010736Snilay@cs.wisc.edu        kwargs['default'] = []
819885Sstever@gmail.com        kwargs['type'] = 'string'
829885Sstever@gmail.com        kwargs['action'] = 'callback'
839885Sstever@gmail.com        kwargs['callback'] = splitter(split)
849885Sstever@gmail.com
859885Sstever@gmail.com    if group:
869885Sstever@gmail.com        return group.add_option(*args, **kwargs)
879885Sstever@gmail.com
8810997Sandreas.sandberg@arm.com    return parser.add_option(*args, **kwargs)
899885Sstever@gmail.com
909885Sstever@gmail.comdef bool_option(name, default, help):
919885Sstever@gmail.com    '''add a boolean option called --name and --no-name.
929885Sstever@gmail.com    Display help depending on which is the default'''
939885Sstever@gmail.com
949885Sstever@gmail.com    tname = '--%s' % name
959885Sstever@gmail.com    fname = '--no-%s' % name
969885Sstever@gmail.com    dest = name.replace('-', '_')
979885Sstever@gmail.com    if default:
989885Sstever@gmail.com        thelp = optparse.SUPPRESS_HELP
9910036SAli.Saidi@ARM.com        fhelp = help
1009885Sstever@gmail.com    else:
1019885Sstever@gmail.com        thelp = help
1029885Sstever@gmail.com        fhelp = optparse.SUPPRESS_HELP
1039885Sstever@gmail.com
1049885Sstever@gmail.com    add_option(tname, action="store_true", default=default, help=thelp)
1059885Sstever@gmail.com    add_option(fname, action="store_false", dest=dest, help=fhelp)
10610315Snilay@cs.wisc.edu
1079885Sstever@gmail.com# Help options
1089885Sstever@gmail.comadd_option('-A', "--authors", action="store_true", default=False,
1099885Sstever@gmail.com    help="Show author information")
1109885Sstever@gmail.comadd_option('-C', "--copyright", action="store_true", default=False,
1119885Sstever@gmail.com    help="Show full copyright information")
11210997Sandreas.sandberg@arm.comadd_option('-R', "--readme", action="store_true", default=False,
11310997Sandreas.sandberg@arm.com    help="Show the readme")
11410736Snilay@cs.wisc.eduadd_option('-N', "--release-notes", action="store_true", default=False,
11510736Snilay@cs.wisc.edu    help="Show the release notes")
11610736Snilay@cs.wisc.edu
11710997Sandreas.sandberg@arm.com# Options for configuring the base simulator
11810736Snilay@cs.wisc.eduadd_option('-d', "--outdir", metavar="DIR", default=".",
11910736Snilay@cs.wisc.edu    help="Set the output directory to DIR [Default: %default]")
12010997Sandreas.sandberg@arm.comadd_option('-i', "--interactive", action="store_true", default=False,
12110997Sandreas.sandberg@arm.com    help="Invoke the interactive interpreter after running the script")
12210997Sandreas.sandberg@arm.comadd_option("--pdb", action="store_true", default=False,
12310736Snilay@cs.wisc.edu    help="Invoke the python debugger before running the script")
12410736Snilay@cs.wisc.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
12510997Sandreas.sandberg@arm.com    help="Prepend PATH to the system path when invoking the script")
12610997Sandreas.sandberg@arm.comadd_option('-q', "--quiet", action="count", default=0,
12710997Sandreas.sandberg@arm.com    help="Reduce verbosity")
12810997Sandreas.sandberg@arm.comadd_option('-v', "--verbose", action="count", default=0,
12910997Sandreas.sandberg@arm.com    help="Increase verbosity")
13010997Sandreas.sandberg@arm.com
13110997Sandreas.sandberg@arm.com# Statistics options
13211440SCurtis.Dunham@arm.comset_group("Statistics Options")
13310997Sandreas.sandberg@arm.comadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
1349885Sstever@gmail.com    help="Sets the output file for statistics [Default: %default]")
1359885Sstever@gmail.com
1369885Sstever@gmail.com# Debugging options
1379885Sstever@gmail.comset_group("Debugging Options")
1389885Sstever@gmail.comadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
13910036SAli.Saidi@ARM.com    help="Cycle to create a breakpoint")
1409885Sstever@gmail.com
1419885Sstever@gmail.com# Tracing options
1429885Sstever@gmail.comset_group("Trace Options")
1439885Sstever@gmail.comadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1449885Sstever@gmail.com    help="Sets the flags for tracing")
1459885Sstever@gmail.comadd_option("--trace-start", metavar="TIME", default='0s',
1469885Sstever@gmail.com    help="Start tracing at TIME (must have units)")
147add_option("--trace-file", metavar="FILE", default="cout",
148    help="Sets the output file for tracing [Default: %default]")
149add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
150    help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
151add_option("--no-trace-circlebuf", action="store_const", const=0,
152    dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
153bool_option("trace-dumponexit", default=False,
154    help="Dump trace buffer on exit")
155add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
156    help="Ignore EXPR sim objects")
157
158# Execution Trace options
159set_group("Execution Trace Options")
160bool_option("speculative", default=True,
161    help="Don't capture speculative instructions")
162bool_option("print-cycle", default=True,
163    help="Don't print cycle numbers in trace output")
164bool_option("print-symbol", default=True,
165    help="Disable PC symbols in trace output")
166bool_option("print-opclass", default=True,
167    help="Don't print op class type in trace output")
168bool_option("print-thread", default=True,
169    help="Don't print thread number in trace output")
170bool_option("print-effaddr", default=True,
171    help="Don't print effective address in trace output")
172bool_option("print-data", default=True,
173    help="Don't print result data in trace output")
174bool_option("print-iregs", default=False,
175    help="Print fetch sequence numbers in trace output")
176bool_option("print-fetch-seq", default=False,
177    help="Print fetch sequence numbers in trace output")
178bool_option("print-cpseq", default=False,
179    help="Print correct path sequence numbers in trace output")
180#bool_option("print-reg-delta", default=False,
181#    help="Print which registers changed to what in trace output")
182
183options = attrdict()
184arguments = []
185
186def usage(exitcode=None):
187    parser.print_help()
188    if exitcode is not None:
189        sys.exit(exitcode)
190
191def parse_args():
192    _opts,args = parser.parse_args()
193    opts = attrdict(_opts.__dict__)
194
195    # setting verbose and quiet at the same time doesn't make sense
196    if opts.verbose > 0 and opts.quiet > 0:
197        usage(2)
198
199    # store the verbosity in a single variable.  0 is default,
200    # negative numbers represent quiet and positive values indicate verbose
201    opts.verbose -= opts.quiet
202
203    del opts.quiet
204
205    options.update(opts)
206    arguments.extend(args)
207    return opts,args
208
209def main():
210    import cc_main
211
212    parse_args()
213
214    done = False
215    if options.copyright:
216        done = True
217        print info.LICENSE
218        print
219
220    if options.authors:
221        done = True
222        print 'Author information:'
223        print
224        print info.AUTHORS
225        print
226
227    if options.readme:
228        done = True
229        print 'Readme:'
230        print
231        print info.README
232        print
233
234    if options.release_notes:
235        done = True
236        print 'Release Notes:'
237        print
238        print info.RELEASE_NOTES
239        print
240
241    if done:
242        sys.exit(0)
243
244    if options.verbose >= 0:
245        print "M5 Simulator System"
246        print brief_copyright
247        print
248        print "M5 compiled %s" % cc_main.cvar.compileDate;
249        print "M5 started %s" % datetime.now().ctime()
250        print "M5 executing on %s" % socket.gethostname()
251        print "command line:",
252        for argv in sys.argv:
253            print argv,
254        print
255
256    # check to make sure we can find the listed script
257    if not arguments or not os.path.isfile(arguments[0]):
258        if arguments and not os.path.isfile(arguments[0]):
259            print "Script %s not found" % arguments[0]
260        usage(2)
261
262    # tell C++ about output directory
263    cc_main.setOutputDir(options.outdir)
264
265    # update the system path with elements from the -p option
266    sys.path[0:0] = options.path
267
268    import objects
269
270    # set stats options
271    objects.Statistics.text_file = options.stats_file
272
273    # set debugging options
274    objects.Debug.break_cycles = options.debug_break
275
276    # set tracing options
277    objects.Trace.flags = options.trace_flags
278    objects.Trace.start = options.trace_start
279    objects.Trace.file = options.trace_file
280    objects.Trace.bufsize = options.trace_circlebuf
281    objects.Trace.dump_on_exit = options.trace_dumponexit
282    objects.Trace.ignore = options.trace_ignore
283
284    # set execution trace options
285    objects.ExecutionTrace.speculative = options.speculative
286    objects.ExecutionTrace.print_cycle = options.print_cycle
287    objects.ExecutionTrace.pc_symbol = options.print_symbol
288    objects.ExecutionTrace.print_opclass = options.print_opclass
289    objects.ExecutionTrace.print_thread = options.print_thread
290    objects.ExecutionTrace.print_effaddr = options.print_effaddr
291    objects.ExecutionTrace.print_data = options.print_data
292    objects.ExecutionTrace.print_iregs = options.print_iregs
293    objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
294    objects.ExecutionTrace.print_cpseq = options.print_cpseq
295    #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta
296
297    sys.argv = arguments
298    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
299
300    scope = { '__file__' : sys.argv[0] }
301
302    # we want readline if we're doing anything interactive
303    if options.interactive or options.pdb:
304        exec("import readline", scope)
305
306    # if pdb was requested, execfile the thing under pdb, otherwise,
307    # just do the execfile normally
308    if options.pdb:
309        from pdb import Pdb
310        debugger = Pdb()
311        debugger.run('execfile("%s")' % sys.argv[0], scope)
312    else:
313        execfile(sys.argv[0], scope)
314
315    # once the script is done
316    if options.interactive:
317        interact = code.InteractiveConsole(scope)
318        interact.interact("M5 Interactive Console")
319
320if __name__ == '__main__':
321    from pprint import pprint
322
323    parse_args()
324
325    print 'opts:'
326    pprint(options, indent=4)
327    print
328
329    print 'args:'
330    pprint(arguments, indent=4)
331