main.py revision 3127:c56885d6dc6d
19852Sandreas.hansson@arm.com# Copyright (c) 2005 The Regents of The University of Michigan 28999Suri.wiener@arm.com# All rights reserved. 38999Suri.wiener@arm.com# 48999Suri.wiener@arm.com# Redistribution and use in source and binary forms, with or without 58999Suri.wiener@arm.com# modification, are permitted provided that the following conditions are 68999Suri.wiener@arm.com# met: redistributions of source code must retain the above copyright 78999Suri.wiener@arm.com# notice, this list of conditions and the following disclaimer; 88999Suri.wiener@arm.com# redistributions in binary form must reproduce the above copyright 98999Suri.wiener@arm.com# notice, this list of conditions and the following disclaimer in the 108999Suri.wiener@arm.com# documentation and/or other materials provided with the distribution; 118999Suri.wiener@arm.com# neither the name of the copyright holders nor the names of its 128999Suri.wiener@arm.com# contributors may be used to endorse or promote products derived from 138999Suri.wiener@arm.com# this software without specific prior written permission. 148999Suri.wiener@arm.com# 158999Suri.wiener@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 168999Suri.wiener@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 178999Suri.wiener@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 188999Suri.wiener@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 198999Suri.wiener@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 208999Suri.wiener@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 218999Suri.wiener@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 228999Suri.wiener@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 238999Suri.wiener@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 248999Suri.wiener@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 258999Suri.wiener@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 268999Suri.wiener@arm.com# 278999Suri.wiener@arm.com# Authors: Nathan Binkert 288999Suri.wiener@arm.com 298999Suri.wiener@arm.comimport code, optparse, os, socket, sys 308999Suri.wiener@arm.comfrom datetime import datetime 318999Suri.wiener@arm.comfrom attrdict import attrdict 328999Suri.wiener@arm.com 338999Suri.wiener@arm.comtry: 348999Suri.wiener@arm.com import info 358999Suri.wiener@arm.comexcept ImportError: 368999Suri.wiener@arm.com info = None 378999Suri.wiener@arm.com 3811418Ssascha.bischoff@arm.com__all__ = [ 'options', 'arguments', 'main' ] 398999Suri.wiener@arm.com 408999Suri.wiener@arm.comusage="%prog [m5 options] script.py [script options]" 418999Suri.wiener@arm.comversion="%prog 2.0" 428999Suri.wiener@arm.combrief_copyright=''' 438999Suri.wiener@arm.comCopyright (c) 2001-2006 448999Suri.wiener@arm.comThe Regents of The University of Michigan 459852Sandreas.hansson@arm.comAll Rights Reserved 469852Sandreas.hansson@arm.com''' 479852Sandreas.hansson@arm.com 489852Sandreas.hansson@arm.com# there's only one option parsing done, so make it global and add some 499852Sandreas.hansson@arm.com# helper functions to make it work well. 509852Sandreas.hansson@arm.comparser = optparse.OptionParser(usage=usage, version=version, 519852Sandreas.hansson@arm.com description=brief_copyright, 529852Sandreas.hansson@arm.com formatter=optparse.TitledHelpFormatter()) 539852Sandreas.hansson@arm.comparser.disable_interspersed_args() 549852Sandreas.hansson@arm.com 558999Suri.wiener@arm.com# current option group 568999Suri.wiener@arm.comgroup = None 578999Suri.wiener@arm.com 588999Suri.wiener@arm.comdef set_group(*args, **kwargs): 598999Suri.wiener@arm.com '''set the current option group''' 6013714Sandreas.sandberg@arm.com global group 6113714Sandreas.sandberg@arm.com if not args and not kwargs: 6213714Sandreas.sandberg@arm.com group = None 638999Suri.wiener@arm.com else: 648999Suri.wiener@arm.com group = parser.add_option_group(*args, **kwargs) 6512202Sgabeblack@google.com 669528Ssascha.bischoff@arm.comclass splitter(object): 678999Suri.wiener@arm.com def __init__(self, split): 688999Suri.wiener@arm.com self.split = split 698999Suri.wiener@arm.com def __call__(self, option, opt_str, value, parser): 708999Suri.wiener@arm.com getattr(parser.values, option.dest).extend(value.split(self.split)) 718999Suri.wiener@arm.com 7212202Sgabeblack@google.comdef add_option(*args, **kwargs): 7313709Sandreas.sandberg@arm.com '''add an option to the current option group, or global none set''' 7412202Sgabeblack@google.com 7512202Sgabeblack@google.com # if action=split, but allows the option arguments 7612202Sgabeblack@google.com # themselves to be lists separated by the split variable''' 7712202Sgabeblack@google.com 7812202Sgabeblack@google.com if kwargs.get('action', None) == 'append' and 'split' in kwargs: 7912202Sgabeblack@google.com split = kwargs.pop('split') 8012202Sgabeblack@google.com kwargs['default'] = [] 8112202Sgabeblack@google.com kwargs['type'] = 'string' 8212202Sgabeblack@google.com kwargs['action'] = 'callback' 838999Suri.wiener@arm.com kwargs['callback'] = splitter(split) 848999Suri.wiener@arm.com 858999Suri.wiener@arm.com if group: 868999Suri.wiener@arm.com return group.add_option(*args, **kwargs) 878999Suri.wiener@arm.com 888999Suri.wiener@arm.com return parser.add_option(*args, **kwargs) 898999Suri.wiener@arm.com 909852Sandreas.hansson@arm.comdef bool_option(name, default, help): 919852Sandreas.hansson@arm.com '''add a boolean option called --name and --no-name. 928999Suri.wiener@arm.com Display help depending on which is the default''' 938999Suri.wiener@arm.com 948999Suri.wiener@arm.com tname = '--%s' % name 958999Suri.wiener@arm.com fname = '--no-%s' % name 968999Suri.wiener@arm.com dest = name.replace('-', '_') 978999Suri.wiener@arm.com if default: 988999Suri.wiener@arm.com thelp = optparse.SUPPRESS_HELP 998999Suri.wiener@arm.com fhelp = help 1008999Suri.wiener@arm.com else: 1018999Suri.wiener@arm.com thelp = help 1028999Suri.wiener@arm.com fhelp = optparse.SUPPRESS_HELP 1038999Suri.wiener@arm.com 1048999Suri.wiener@arm.com add_option(tname, action="store_true", default=default, help=thelp) 10512202Sgabeblack@google.com add_option(fname, action="store_false", dest=dest, help=fhelp) 10612202Sgabeblack@google.com 1078999Suri.wiener@arm.com# Help options 1088999Suri.wiener@arm.comadd_option('-A', "--authors", action="store_true", default=False, 1098999Suri.wiener@arm.com help="Show author information") 1108999Suri.wiener@arm.comadd_option('-C', "--copyright", action="store_true", default=False, 1118999Suri.wiener@arm.com help="Show full copyright information") 1128999Suri.wiener@arm.comadd_option('-R', "--readme", action="store_true", default=False, 1138999Suri.wiener@arm.com help="Show the readme") 1148999Suri.wiener@arm.comadd_option('-N', "--release-notes", action="store_true", default=False, 1158999Suri.wiener@arm.com help="Show the release notes") 1168999Suri.wiener@arm.com 1178999Suri.wiener@arm.com# Options for configuring the base simulator 1188999Suri.wiener@arm.comadd_option('-d', "--outdir", metavar="DIR", default=".", 11910176Ssascha.bischoff@arm.com help="Set the output directory to DIR [Default: %default]") 1208999Suri.wiener@arm.comadd_option('-i', "--interactive", action="store_true", default=False, 1218999Suri.wiener@arm.com help="Invoke the interactive interpreter after running the script") 1228999Suri.wiener@arm.comadd_option("--pdb", action="store_true", default=False, 1238999Suri.wiener@arm.com help="Invoke the python debugger before running the script") 1248999Suri.wiener@arm.comadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 1258999Suri.wiener@arm.com help="Prepend PATH to the system path when invoking the script") 12612202Sgabeblack@google.comadd_option('-q', "--quiet", action="count", default=0, 12712202Sgabeblack@google.com help="Reduce verbosity") 1288999Suri.wiener@arm.comadd_option('-v', "--verbose", action="count", default=0, 1298999Suri.wiener@arm.com help="Increase verbosity") 1308999Suri.wiener@arm.com 1318999Suri.wiener@arm.com# Statistics options 1328999Suri.wiener@arm.comset_group("Statistics Options") 1338999Suri.wiener@arm.comadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 1348999Suri.wiener@arm.com help="Sets the output file for statistics [Default: %default]") 1358999Suri.wiener@arm.com 1369854Sandreas.hansson@arm.com# Debugging options 1379854Sandreas.hansson@arm.comset_group("Debugging Options") 1389854Sandreas.hansson@arm.comadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1399854Sandreas.hansson@arm.com help="Cycle to create a breakpoint") 1409854Sandreas.hansson@arm.com 1419854Sandreas.hansson@arm.com# Tracing options 1429854Sandreas.hansson@arm.comset_group("Trace Options") 1439854Sandreas.hansson@arm.comadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 1449854Sandreas.hansson@arm.com help="Sets the flags for tracing") 1459854Sandreas.hansson@arm.comadd_option("--trace-start", metavar="TIME", default='0s', 1469854Sandreas.hansson@arm.com help="Start tracing at TIME (must have units)") 1478999Suri.wiener@arm.comadd_option("--trace-cycle", metavar="CYCLE", default='0', 1488999Suri.wiener@arm.com help="Start tracing at CYCLE") 1498999Suri.wiener@arm.comadd_option("--trace-file", metavar="FILE", default="cout", 1508999Suri.wiener@arm.com help="Sets the output file for tracing [Default: %default]") 1519854Sandreas.hansson@arm.comadd_option("--trace-circlebuf", metavar="SIZE", type="int", default=0, 1528999Suri.wiener@arm.com help="If SIZE is non-zero, turn on the circular buffer with SIZE lines") 1538999Suri.wiener@arm.comadd_option("--no-trace-circlebuf", action="store_const", const=0, 1549853Sandreas.hansson@arm.com dest='trace_circlebuf', help=optparse.SUPPRESS_HELP) 1558999Suri.wiener@arm.combool_option("trace-dumponexit", default=False, 1568999Suri.wiener@arm.com help="Dump trace buffer on exit") 1578999Suri.wiener@arm.comadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1588999Suri.wiener@arm.com help="Ignore EXPR sim objects") 1598999Suri.wiener@arm.com 1608999Suri.wiener@arm.com# Execution Trace options 1618999Suri.wiener@arm.comset_group("Execution Trace Options") 1628999Suri.wiener@arm.combool_option("speculative", default=True, 1638999Suri.wiener@arm.com help="Don't capture speculative instructions") 1648999Suri.wiener@arm.combool_option("print-cycle", default=True, 1658999Suri.wiener@arm.com help="Don't print cycle numbers in trace output") 1668999Suri.wiener@arm.combool_option("print-symbol", default=True, 1679853Sandreas.hansson@arm.com help="Disable PC symbols in trace output") 1688999Suri.wiener@arm.combool_option("print-opclass", default=True, 1698999Suri.wiener@arm.com help="Don't print op class type in trace output") 1708999Suri.wiener@arm.combool_option("print-thread", default=True, 1718999Suri.wiener@arm.com help="Don't print thread number in trace output") 1728999Suri.wiener@arm.combool_option("print-effaddr", default=True, 1739853Sandreas.hansson@arm.com help="Don't print effective address in trace output") 1749853Sandreas.hansson@arm.combool_option("print-data", default=True, 1759853Sandreas.hansson@arm.com help="Don't print result data in trace output") 1769853Sandreas.hansson@arm.combool_option("print-iregs", default=False, 1779853Sandreas.hansson@arm.com help="Print fetch sequence numbers in trace output") 1789853Sandreas.hansson@arm.combool_option("print-fetch-seq", default=False, 17910405Sandreas.hansson@arm.com help="Print fetch sequence numbers in trace output") 1809853Sandreas.hansson@arm.combool_option("print-cpseq", default=False, 1819853Sandreas.hansson@arm.com help="Print correct path sequence numbers in trace output") 1829853Sandreas.hansson@arm.com#bool_option("print-reg-delta", default=False, 1839853Sandreas.hansson@arm.com# help="Print which registers changed to what in trace output") 1849853Sandreas.hansson@arm.com 1859853Sandreas.hansson@arm.comoptions = attrdict() 1869853Sandreas.hansson@arm.comarguments = [] 1879853Sandreas.hansson@arm.com 1889853Sandreas.hansson@arm.comdef usage(exitcode=None): 1899853Sandreas.hansson@arm.com parser.print_help() 1909853Sandreas.hansson@arm.com if exitcode is not None: 1919853Sandreas.hansson@arm.com sys.exit(exitcode) 1929853Sandreas.hansson@arm.com 1939853Sandreas.hansson@arm.comdef parse_args(): 1949853Sandreas.hansson@arm.com _opts,args = parser.parse_args() 1959853Sandreas.hansson@arm.com opts = attrdict(_opts.__dict__) 19610405Sandreas.hansson@arm.com 19710405Sandreas.hansson@arm.com # setting verbose and quiet at the same time doesn't make sense 1989853Sandreas.hansson@arm.com if opts.verbose > 0 and opts.quiet > 0: 1999853Sandreas.hansson@arm.com usage(2) 2009853Sandreas.hansson@arm.com 2019853Sandreas.hansson@arm.com # store the verbosity in a single variable. 0 is default, 2029853Sandreas.hansson@arm.com # negative numbers represent quiet and positive values indicate verbose 2039853Sandreas.hansson@arm.com opts.verbose -= opts.quiet 2049853Sandreas.hansson@arm.com 2059853Sandreas.hansson@arm.com del opts.quiet 2069853Sandreas.hansson@arm.com 2079853Sandreas.hansson@arm.com options.update(opts) 2089853Sandreas.hansson@arm.com arguments.extend(args) 2099853Sandreas.hansson@arm.com return opts,args 2109853Sandreas.hansson@arm.com 21110405Sandreas.hansson@arm.comdef main(): 2129853Sandreas.hansson@arm.com import cc_main 2139853Sandreas.hansson@arm.com 2149853Sandreas.hansson@arm.com parse_args() 2159853Sandreas.hansson@arm.com 2169853Sandreas.hansson@arm.com done = False 2179853Sandreas.hansson@arm.com if options.copyright: 2189853Sandreas.hansson@arm.com done = True 2199853Sandreas.hansson@arm.com print info.LICENSE 2209853Sandreas.hansson@arm.com print 2219853Sandreas.hansson@arm.com 2229853Sandreas.hansson@arm.com if options.authors: 2239853Sandreas.hansson@arm.com done = True 2249853Sandreas.hansson@arm.com print 'Author information:' 2259853Sandreas.hansson@arm.com print 2269853Sandreas.hansson@arm.com print info.AUTHORS 2279853Sandreas.hansson@arm.com print 2289853Sandreas.hansson@arm.com 2299853Sandreas.hansson@arm.com if options.readme: 2309853Sandreas.hansson@arm.com done = True 2319853Sandreas.hansson@arm.com print 'Readme:' 2329853Sandreas.hansson@arm.com print 2339853Sandreas.hansson@arm.com print info.README 2349853Sandreas.hansson@arm.com print 2359853Sandreas.hansson@arm.com 2369853Sandreas.hansson@arm.com if options.release_notes: 2379853Sandreas.hansson@arm.com done = True 2389853Sandreas.hansson@arm.com print 'Release Notes:' 2399853Sandreas.hansson@arm.com print 2409853Sandreas.hansson@arm.com print info.RELEASE_NOTES 2419853Sandreas.hansson@arm.com print 2429853Sandreas.hansson@arm.com 2439853Sandreas.hansson@arm.com if done: 2449853Sandreas.hansson@arm.com sys.exit(0) 2459853Sandreas.hansson@arm.com 2469853Sandreas.hansson@arm.com if options.verbose >= 0: 2479853Sandreas.hansson@arm.com print "M5 Simulator System" 2489853Sandreas.hansson@arm.com print brief_copyright 2499853Sandreas.hansson@arm.com print 2509853Sandreas.hansson@arm.com print "M5 compiled %s" % cc_main.cvar.compileDate; 2519853Sandreas.hansson@arm.com print "M5 started %s" % datetime.now().ctime() 2529853Sandreas.hansson@arm.com print "M5 executing on %s" % socket.gethostname() 2539853Sandreas.hansson@arm.com print "command line:", 2549853Sandreas.hansson@arm.com for argv in sys.argv: 2559853Sandreas.hansson@arm.com print argv, 2568999Suri.wiener@arm.com print 2579852Sandreas.hansson@arm.com 2589852Sandreas.hansson@arm.com # check to make sure we can find the listed script 2599852Sandreas.hansson@arm.com if not arguments or not os.path.isfile(arguments[0]): 2608999Suri.wiener@arm.com if arguments and not os.path.isfile(arguments[0]): 2618999Suri.wiener@arm.com print "Script %s not found" % arguments[0] 26211418Ssascha.bischoff@arm.com usage(2) 26311418Ssascha.bischoff@arm.com 26411418Ssascha.bischoff@arm.com # tell C++ about output directory 26511418Ssascha.bischoff@arm.com cc_main.setOutputDir(options.outdir) 26611418Ssascha.bischoff@arm.com 26711418Ssascha.bischoff@arm.com # update the system path with elements from the -p option 26811418Ssascha.bischoff@arm.com sys.path[0:0] = options.path 26911418Ssascha.bischoff@arm.com 27011418Ssascha.bischoff@arm.com import objects 27111418Ssascha.bischoff@arm.com 27211418Ssascha.bischoff@arm.com # set stats options 27311418Ssascha.bischoff@arm.com objects.Statistics.text_file = options.stats_file 27411418Ssascha.bischoff@arm.com 27511418Ssascha.bischoff@arm.com # set debugging options 27611418Ssascha.bischoff@arm.com objects.Debug.break_cycles = options.debug_break 27711418Ssascha.bischoff@arm.com 27811418Ssascha.bischoff@arm.com # set tracing options 27911418Ssascha.bischoff@arm.com objects.Trace.flags = options.trace_flags 28011418Ssascha.bischoff@arm.com objects.Trace.start = options.trace_start 28111418Ssascha.bischoff@arm.com objects.Trace.file = options.trace_file 28211418Ssascha.bischoff@arm.com objects.Trace.bufsize = options.trace_circlebuf 28311418Ssascha.bischoff@arm.com objects.Trace.dump_on_exit = options.trace_dumponexit 28411418Ssascha.bischoff@arm.com objects.Trace.ignore = options.trace_ignore 28511418Ssascha.bischoff@arm.com 28611418Ssascha.bischoff@arm.com # set execution trace options 28711418Ssascha.bischoff@arm.com objects.ExecutionTrace.speculative = options.speculative 28811418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_cycle = options.print_cycle 28911418Ssascha.bischoff@arm.com objects.ExecutionTrace.pc_symbol = options.print_symbol 29011418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_opclass = options.print_opclass 29111418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_thread = options.print_thread 29211418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_effaddr = options.print_effaddr 29311418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_data = options.print_data 29411418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_iregs = options.print_iregs 29511418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq 29611418Ssascha.bischoff@arm.com objects.ExecutionTrace.print_cpseq = options.print_cpseq 29711418Ssascha.bischoff@arm.com #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta 29811418Ssascha.bischoff@arm.com 29911418Ssascha.bischoff@arm.com sys.argv = arguments 30011418Ssascha.bischoff@arm.com sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 30111418Ssascha.bischoff@arm.com 30211418Ssascha.bischoff@arm.com scope = { '__file__' : sys.argv[0] } 30311418Ssascha.bischoff@arm.com 30412202Sgabeblack@google.com # we want readline if we're doing anything interactive 30512202Sgabeblack@google.com if options.interactive or options.pdb: 30612202Sgabeblack@google.com exec("import readline", scope) 30712202Sgabeblack@google.com 30812202Sgabeblack@google.com # if pdb was requested, execfile the thing under pdb, otherwise, 30912202Sgabeblack@google.com # just do the execfile normally 31012202Sgabeblack@google.com if options.pdb: 31112202Sgabeblack@google.com from pdb import Pdb 31212202Sgabeblack@google.com debugger = Pdb() 31311418Ssascha.bischoff@arm.com debugger.run('execfile("%s")' % sys.argv[0], scope) 31412202Sgabeblack@google.com else: 31512202Sgabeblack@google.com execfile(sys.argv[0], scope) 31612202Sgabeblack@google.com 31712202Sgabeblack@google.com # once the script is done 31812202Sgabeblack@google.com if options.interactive: 31912202Sgabeblack@google.com interact = code.InteractiveConsole(scope) 32011418Ssascha.bischoff@arm.com interact.interact("M5 Interactive Console") 32112202Sgabeblack@google.com 32212202Sgabeblack@google.comif __name__ == '__main__': 32311418Ssascha.bischoff@arm.com from pprint import pprint 32411418Ssascha.bischoff@arm.com 32511418Ssascha.bischoff@arm.com parse_args() 32611418Ssascha.bischoff@arm.com 32711418Ssascha.bischoff@arm.com print 'opts:' 32811418Ssascha.bischoff@arm.com pprint(options, indent=4) 3298999Suri.wiener@arm.com print 3308999Suri.wiener@arm.com 3318999Suri.wiener@arm.com print 'args:' 3329852Sandreas.hansson@arm.com pprint(arguments, indent=4) 3339852Sandreas.hansson@arm.com