main.py revision 2890
12SN/A# Copyright (c) 2005 The Regents of The University of Michigan
29448SAndreas.Sandberg@ARM.com# All rights reserved.
38733Sgeoffrey.blake@arm.com#
48733Sgeoffrey.blake@arm.com# Redistribution and use in source and binary forms, with or without
58733Sgeoffrey.blake@arm.com# modification, are permitted provided that the following conditions are
68733Sgeoffrey.blake@arm.com# met: redistributions of source code must retain the above copyright
78733Sgeoffrey.blake@arm.com# notice, this list of conditions and the following disclaimer;
88733Sgeoffrey.blake@arm.com# redistributions in binary form must reproduce the above copyright
98733Sgeoffrey.blake@arm.com# notice, this list of conditions and the following disclaimer in the
108733Sgeoffrey.blake@arm.com# documentation and/or other materials provided with the distribution;
118733Sgeoffrey.blake@arm.com# neither the name of the copyright holders nor the names of its
128733Sgeoffrey.blake@arm.com# contributors may be used to endorse or promote products derived from
138733Sgeoffrey.blake@arm.com# this software without specific prior written permission.
141762SN/A#
152SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
162SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
182SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
192SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
212SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
252SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262SN/A#
272SN/A# Authors: Nathan Binkert
282SN/A
292SN/Aimport code, optparse, os, socket, sys
302SN/Afrom datetime import datetime
312SN/Afrom attrdict import attrdict
322SN/A
332SN/Atry:
342SN/A    import info
352SN/Aexcept ImportError:
362SN/A    info = None
372SN/A
382SN/A__all__ = [ 'options', 'arguments', 'main' ]
392665Ssaidi@eecs.umich.edu
402665Ssaidi@eecs.umich.eduusage="%prog [m5 options] script.py [script options]"
412665Ssaidi@eecs.umich.eduversion="%prog 2.0"
422665Ssaidi@eecs.umich.edubrief_copyright='''
432SN/ACopyright (c) 2001-2006
442SN/AThe Regents of The University of Michigan
452623SN/AAll Rights Reserved
462623SN/A'''
472SN/A
481354SN/A# there's only one option parsing done, so make it global and add some
496658Snate@binkert.org# helper functions to make it work well.
501717SN/Aparser = optparse.OptionParser(usage=usage, version=version,
518887Sgeoffrey.blake@arm.com                               description=brief_copyright,
528229Snate@binkert.org                               formatter=optparse.TitledHelpFormatter())
532683Sktlim@umich.eduparser.disable_interspersed_args()
541354SN/A
552387SN/A# current option group
562387SN/Agroup = None
572387SN/A
5856SN/Adef set_group(*args, **kwargs):
598779Sgblack@eecs.umich.edu    '''set the current option group'''
605348Ssaidi@eecs.umich.edu    global group
612SN/A    if not args and not kwargs:
622SN/A        group = None
638779Sgblack@eecs.umich.edu    else:
648779Sgblack@eecs.umich.edu        group = parser.add_option_group(*args, **kwargs)
652SN/A
668779Sgblack@eecs.umich.educlass splitter(object):
672SN/A    def __init__(self, split):
684182Sgblack@eecs.umich.edu        self.split = split
694182Sgblack@eecs.umich.edu    def __call__(self, option, opt_str, value, parser):
708779Sgblack@eecs.umich.edu        getattr(parser.values, option.dest).extend(value.split(self.split))
718779Sgblack@eecs.umich.edu
724182Sgblack@eecs.umich.edudef add_option(*args, **kwargs):
732SN/A    '''add an option to the current option group, or global none set'''
742SN/A
752SN/A    # if action=split, but allows the option arguments
762SN/A    # themselves to be lists separated by the split variable'''
772SN/A
788737Skoansin.tan@gmail.com    if kwargs.get('action', None) == 'append' and 'split' in kwargs:
795529Snate@binkert.org        split = kwargs.pop('split')
802420SN/A        kwargs['default'] = []
812623SN/A        kwargs['type'] = 'string'
822SN/A        kwargs['action'] = 'callback'
832107SN/A        kwargs['callback'] = splitter(split)
842159SN/A
852455SN/A    if group:
862455SN/A        return group.add_option(*args, **kwargs)
872386SN/A
882623SN/A    return parser.add_option(*args, **kwargs)
892SN/A
901371SN/Adef bool_option(name, default, help):
915348Ssaidi@eecs.umich.edu    '''add a boolean option called --name and --no-name.
927720Sgblack@eecs.umich.edu    Display help depending on which is the default'''
935348Ssaidi@eecs.umich.edu
947720Sgblack@eecs.umich.edu    tname = '--%s' % name
955348Ssaidi@eecs.umich.edu    fname = '--no-%s' % name
967720Sgblack@eecs.umich.edu    dest = name.replace('-', '_')
977720Sgblack@eecs.umich.edu    if default:
985348Ssaidi@eecs.umich.edu        thelp = optparse.SUPPRESS_HELP
995348Ssaidi@eecs.umich.edu        fhelp = help
1002SN/A    else:
1015807Snate@binkert.org        thelp = help
1022SN/A        fhelp = optparse.SUPPRESS_HELP
1032SN/A
1042SN/A    add_option(tname, action="store_true", default=default, help=thelp)
1052SN/A    add_option(fname, action="store_false", dest=dest, help=fhelp)
1062SN/A
1072SN/A# Help options
1082SN/Aadd_option('-A', "--authors", action="store_true", default=False,
1092SN/A    help="Show author information")
1102SN/Aadd_option('-C', "--copyright", action="store_true", default=False,
1111400SN/A    help="Show full copyright information")
1125529Snate@binkert.orgadd_option('-R', "--readme", action="store_true", default=False,
1132623SN/A    help="Show the readme")
1142SN/Aadd_option('-N', "--release-notes", action="store_true", default=False,
1151400SN/A    help="Show the release notes")
1162683Sktlim@umich.edu
1172683Sktlim@umich.edu# Options for configuring the base simulator
1182190SN/Aadd_option('-d', "--outdir", metavar="DIR", default=".",
1192683Sktlim@umich.edu    help="Set the output directory to DIR [Default: %default]")
1202683Sktlim@umich.eduadd_option('-i', "--interactive", action="store_true", default=False,
1212683Sktlim@umich.edu    help="Invoke the interactive interpreter after running the script")
1222680Sktlim@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
1238733Sgeoffrey.blake@arm.com    help="Prepend PATH to the system path when invoking the script")
1248733Sgeoffrey.blake@arm.comadd_option('-q', "--quiet", action="count", default=0,
1258887Sgeoffrey.blake@arm.com    help="Reduce verbosity")
1265169Ssaidi@eecs.umich.eduadd_option('-v', "--verbose", action="count", default=0,
1275169Ssaidi@eecs.umich.edu    help="Increase verbosity")
1285496Ssaidi@eecs.umich.edu
1295496Ssaidi@eecs.umich.edu# Statistics options
1305496Ssaidi@eecs.umich.eduset_group("Statistics Options")
1318276SAli.Saidi@ARM.comadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
1325894Sgblack@eecs.umich.edu    help="Sets the output file for statistics [Default: %default]")
1335496Ssaidi@eecs.umich.edu
1345496Ssaidi@eecs.umich.edu# Debugging options
1355496Ssaidi@eecs.umich.eduset_group("Debugging Options")
1365894Sgblack@eecs.umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1375496Ssaidi@eecs.umich.edu    help="Cycle to create a breakpoint")
1385496Ssaidi@eecs.umich.edu
1395496Ssaidi@eecs.umich.edu# Tracing options
1405496Ssaidi@eecs.umich.eduset_group("Trace Options")
1415496Ssaidi@eecs.umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1425496Ssaidi@eecs.umich.edu    help="Sets the flags for tracing")
1435496Ssaidi@eecs.umich.eduadd_option("--trace-start", metavar="TIME", default='0s',
1445169Ssaidi@eecs.umich.edu    help="Start tracing at TIME (must have units)")
1452SN/Aadd_option("--trace-file", metavar="FILE", default="cout",
1462SN/A    help="Sets the output file for tracing [Default: %default]")
1472SN/Aadd_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
1482SN/A    help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
1492SN/Aadd_option("--no-trace-circlebuf", action="store_const", const=0,
1502SN/A    dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
1514181Sgblack@eecs.umich.edubool_option("trace-dumponexit", default=False,
1524181Sgblack@eecs.umich.edu    help="Dump trace buffer on exit")
1532107SN/Aadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1543276Sgblack@eecs.umich.edu    help="Ignore EXPR sim objects")
1551469SN/A
1564377Sgblack@eecs.umich.edu# Execution Trace options
1574377Sgblack@eecs.umich.eduset_group("Execution Trace Options")
1584377Sgblack@eecs.umich.edubool_option("speculative", default=True,
1594377Sgblack@eecs.umich.edu    help="Don't capture speculative instructions")
1604377Sgblack@eecs.umich.edubool_option("print-cycle", default=True,
1614377Sgblack@eecs.umich.edu    help="Don't print cycle numbers in trace output")
1622623SN/Abool_option("print-symbol", default=True,
1635894Sgblack@eecs.umich.edu    help="Disable PC symbols in trace output")
1642623SN/Abool_option("print-opclass", default=True,
1652623SN/A    help="Don't print op class type in trace output")
1662623SN/Abool_option("print-thread", default=True,
167180SN/A    help="Don't print thread number in trace output")
1688737Skoansin.tan@gmail.combool_option("print-effaddr", default=True,
1698737Skoansin.tan@gmail.com    help="Don't print effective address in trace output")
1702SN/Abool_option("print-data", default=True,
1712SN/A    help="Don't print result data in trace output")
172334SN/Abool_option("print-iregs", default=False,
173334SN/A    help="Print fetch sequence numbers in trace output")
1742SN/Abool_option("print-fetch-seq", default=False,
1759461Snilay@cs.wisc.edu    help="Print fetch sequence numbers in trace output")
1769461Snilay@cs.wisc.edubool_option("print-cpseq", default=False,
1772SN/A    help="Print correct path sequence numbers in trace output")
1782SN/A
179334SN/Aoptions = attrdict()
1805999Snate@binkert.orgarguments = []
1818834Satgutier@umich.edu
1828834Satgutier@umich.edudef usage(exitcode=None):
1838834Satgutier@umich.edu    print parser.help
184707SN/A    if exitcode is not None:
1854998Sgblack@eecs.umich.edu        sys.exit(exitcode)
1864998Sgblack@eecs.umich.edu
1878834Satgutier@umich.edudef parse_args():
1888834Satgutier@umich.edu    _opts,args = parser.parse_args()
1898834Satgutier@umich.edu    opts = attrdict(_opts.__dict__)
1908834Satgutier@umich.edu
1918834Satgutier@umich.edu    # setting verbose and quiet at the same time doesn't make sense
1928834Satgutier@umich.edu    if opts.verbose > 0 and opts.quiet > 0:
1938834Satgutier@umich.edu        usage(2)
1947897Shestness@cs.utexas.edu
1954998Sgblack@eecs.umich.edu    # store the verbosity in a single variable.  0 is default,
1964998Sgblack@eecs.umich.edu    # negative numbers represent quiet and positive values indicate verbose
1974998Sgblack@eecs.umich.edu    opts.verbose -= opts.quiet
1988834Satgutier@umich.edu
199707SN/A    del opts.quiet
200707SN/A
201707SN/A    options.update(opts)
2022SN/A    arguments.extend(args)
2038834Satgutier@umich.edu    return opts,args
2048834Satgutier@umich.edu
2058834Satgutier@umich.edudef main():
2068834Satgutier@umich.edu    import cc_main
2078834Satgutier@umich.edu
2087897Shestness@cs.utexas.edu    parse_args()
2097897Shestness@cs.utexas.edu
2107897Shestness@cs.utexas.edu    done = False
2117897Shestness@cs.utexas.edu    if options.copyright:
2127897Shestness@cs.utexas.edu        done = True
2137897Shestness@cs.utexas.edu        print info.LICENSE
2147897Shestness@cs.utexas.edu        print
2157897Shestness@cs.utexas.edu
2167897Shestness@cs.utexas.edu    if options.authors:
2177897Shestness@cs.utexas.edu        done = True
2187897Shestness@cs.utexas.edu        print 'Author information:'
2197897Shestness@cs.utexas.edu        print
2207897Shestness@cs.utexas.edu        print info.AUTHORS
2217897Shestness@cs.utexas.edu        print
2227897Shestness@cs.utexas.edu
2237897Shestness@cs.utexas.edu    if options.readme:
2247897Shestness@cs.utexas.edu        done = True
2257897Shestness@cs.utexas.edu        print 'Readme:'
2267897Shestness@cs.utexas.edu        print
2277897Shestness@cs.utexas.edu        print info.README
2287897Shestness@cs.utexas.edu        print
2297897Shestness@cs.utexas.edu
2307897Shestness@cs.utexas.edu    if options.release_notes:
2317897Shestness@cs.utexas.edu        done = True
2327897Shestness@cs.utexas.edu        print 'Release Notes:'
2337897Shestness@cs.utexas.edu        print
2342SN/A        print info.RELEASE_NOTES
2355999Snate@binkert.org        print
2367897Shestness@cs.utexas.edu
2377897Shestness@cs.utexas.edu    if done:
2387897Shestness@cs.utexas.edu        sys.exit(0)
2397897Shestness@cs.utexas.edu
2407897Shestness@cs.utexas.edu    if options.verbose >= 0:
2417897Shestness@cs.utexas.edu        print "M5 Simulator System"
2427897Shestness@cs.utexas.edu        print brief_copyright
2437897Shestness@cs.utexas.edu        print
2442SN/A        print "M5 compiled %s" % cc_main.cvar.compileDate;
245124SN/A        print "M5 started %s" % datetime.now().ctime()
246124SN/A        print "M5 executing on %s" % socket.gethostname()
247334SN/A
248124SN/A    # check to make sure we can find the listed script
2492SN/A    if not arguments or not os.path.isfile(arguments[0]):
2505999Snate@binkert.org        usage(2)
251729SN/A
2522SN/A    # tell C++ about output directory
2532390SN/A    cc_main.setOutputDir(options.outdir)
2545999Snate@binkert.org
2552SN/A    # update the system path with elements from the -p option
2562SN/A    sys.path[0:0] = options.path
2572390SN/A
2585999Snate@binkert.org    import objects
2592390SN/A
2602390SN/A    # set stats options
2612390SN/A    objects.Statistics.text_file = options.stats_file
2625999Snate@binkert.org
2632SN/A    # set debugging options
2642SN/A    objects.Debug.break_cycles = options.debug_break
2652390SN/A
2665999Snate@binkert.org    # set tracing options
2672390SN/A    objects.Trace.flags = options.trace_flags
2682390SN/A    objects.Trace.start = options.trace_start
2699448SAndreas.Sandberg@ARM.com    objects.Trace.file = options.trace_file
2709448SAndreas.Sandberg@ARM.com    objects.Trace.bufsize = options.trace_circlebuf
2719448SAndreas.Sandberg@ARM.com    objects.Trace.dump_on_exit = options.trace_dumponexit
2722SN/A    objects.Trace.ignore = options.trace_ignore
2731371SN/A
2741371SN/A    # set execution trace options
2752623SN/A    objects.ExecutionTrace.speculative = options.speculative
2765543Ssaidi@eecs.umich.edu    objects.ExecutionTrace.print_cycle = options.print_cycle
2773918Ssaidi@eecs.umich.edu    objects.ExecutionTrace.pc_symbol = options.print_symbol
2781371SN/A    objects.ExecutionTrace.print_opclass = options.print_opclass
279726SN/A    objects.ExecutionTrace.print_thread = options.print_thread
280726SN/A    objects.ExecutionTrace.print_effaddr = options.print_effaddr
281726SN/A    objects.ExecutionTrace.print_data = options.print_data
282726SN/A    objects.ExecutionTrace.print_iregs = options.print_iregs
283726SN/A    objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
284726SN/A    objects.ExecutionTrace.print_cpseq = options.print_cpseq
285726SN/A
286726SN/A    scope = { '__file__' : sys.argv[0] }
287726SN/A    sys.argv = arguments
288726SN/A    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
289705SN/A    exec("import readline", scope)
2903735Sstever@eecs.umich.edu    execfile(sys.argv[0], scope)
291726SN/A
2927897Shestness@cs.utexas.edu    # once the script is done
2932683Sktlim@umich.edu    if options.interactive:
294726SN/A        interact = code.InteractiveConsole(scope)
295705SN/A        interact.interact("M5 Interactive Console")
2963735Sstever@eecs.umich.edu
297726SN/Aif __name__ == '__main__':
2987897Shestness@cs.utexas.edu    from pprint import pprint
299726SN/A
3002683Sktlim@umich.edu    parse_args()
301726SN/A
302705SN/A    print 'opts:'
3033735Sstever@eecs.umich.edu    pprint(options, indent=4)
3042455SN/A    print
3057897Shestness@cs.utexas.edu
3062455SN/A    print 'args:'
3072683Sktlim@umich.edu    pprint(arguments, indent=4)
308726SN/A