main.py revision 14213
114213Sandreas.sandberg@arm.com# Copyright (c) 2016, 2019 Arm Limited
211923Sandreas.sandberg@arm.com# All rights reserved.
311923Sandreas.sandberg@arm.com#
411923Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall
511923Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual
611923Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating
711923Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software
811923Sandreas.sandberg@arm.com# licensed hereunder.  You may use the software subject to the license
911923Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated
1011923Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software,
1111923Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form.
1211923Sandreas.sandberg@arm.com#
132889Sbinkertn@umich.edu# Copyright (c) 2005 The Regents of The University of Michigan
142889Sbinkertn@umich.edu# All rights reserved.
152889Sbinkertn@umich.edu#
162889Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
172889Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
182889Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
192889Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
202889Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
212889Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
222889Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
232889Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
242889Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
252889Sbinkertn@umich.edu# this software without specific prior written permission.
262889Sbinkertn@umich.edu#
272889Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
282889Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
292889Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
302889Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
312889Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
322889Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
332889Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
342889Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
352889Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
362889Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
372889Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
382889Sbinkertn@umich.edu#
392889Sbinkertn@umich.edu# Authors: Nathan Binkert
402889Sbinkertn@umich.edu
4112563Sgabeblack@google.comfrom __future__ import print_function
4212563Sgabeblack@google.com
434850Snate@binkert.orgimport code
444850Snate@binkert.orgimport datetime
454850Snate@binkert.orgimport os
464850Snate@binkert.orgimport socket
474850Snate@binkert.orgimport sys
484850Snate@binkert.org
492889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ]
502889Sbinkertn@umich.edu
518327Sgblack@eecs.umich.eduusage="%prog [gem5 options] script.py [script options]"
525470Snate@binkert.orgversion="%prog 2.0"
538333Snate@binkert.orgbrief_copyright=\
548333Snate@binkert.org    "gem5 is copyrighted software; use the --copyright option for details."
552889Sbinkertn@umich.edu
5614213Sandreas.sandberg@arm.comdef _stats_help(option, opt, value, parser):
5714213Sandreas.sandberg@arm.com    import m5
5814213Sandreas.sandberg@arm.com    print("A stat file can either be specified as a URI or a plain")
5914213Sandreas.sandberg@arm.com    print("path. When specified as a path, gem5 uses the default text ")
6014213Sandreas.sandberg@arm.com    print("format.")
6114213Sandreas.sandberg@arm.com    print()
6214213Sandreas.sandberg@arm.com    print("The following stat formats are supported:")
6314213Sandreas.sandberg@arm.com    print()
6414213Sandreas.sandberg@arm.com    m5.stats.printStatVisitorTypes()
6514213Sandreas.sandberg@arm.com    sys.exit(0)
6614213Sandreas.sandberg@arm.com
6714213Sandreas.sandberg@arm.com
688234Snate@binkert.orgdef parse_options():
6913714Sandreas.sandberg@arm.com    from . import config
7013714Sandreas.sandberg@arm.com    from .options import OptionParser
712889Sbinkertn@umich.edu
728234Snate@binkert.org    options = OptionParser(usage=usage, version=version,
738234Snate@binkert.org                           description=brief_copyright)
748234Snate@binkert.org    option = options.add_option
758234Snate@binkert.org    group = options.set_group
762889Sbinkertn@umich.edu
7711923Sandreas.sandberg@arm.com    listener_modes = ( "on", "off", "auto" )
7811923Sandreas.sandberg@arm.com
798234Snate@binkert.org    # Help options
808234Snate@binkert.org    option('-B', "--build-info", action="store_true", default=False,
818234Snate@binkert.org        help="Show build information")
828234Snate@binkert.org    option('-C', "--copyright", action="store_true", default=False,
838234Snate@binkert.org        help="Show full copyright information")
848234Snate@binkert.org    option('-R', "--readme", action="store_true", default=False,
858234Snate@binkert.org        help="Show the readme")
862889Sbinkertn@umich.edu
878234Snate@binkert.org    # Options for configuring the base simulator
888234Snate@binkert.org    option('-d', "--outdir", metavar="DIR", default="m5out",
898234Snate@binkert.org        help="Set the output directory to DIR [Default: %default]")
908234Snate@binkert.org    option('-r', "--redirect-stdout", action="store_true", default=False,
918234Snate@binkert.org        help="Redirect stdout (& stderr, without -e) to file")
928234Snate@binkert.org    option('-e', "--redirect-stderr", action="store_true", default=False,
938234Snate@binkert.org        help="Redirect stderr to file")
948234Snate@binkert.org    option("--stdout-file", metavar="FILE", default="simout",
958234Snate@binkert.org        help="Filename for -r redirection [Default: %default]")
968234Snate@binkert.org    option("--stderr-file", metavar="FILE", default="simerr",
978234Snate@binkert.org        help="Filename for -e redirection [Default: %default]")
9811923Sandreas.sandberg@arm.com    option("--listener-mode", metavar="{on,off,auto}",
9911923Sandreas.sandberg@arm.com        choices=listener_modes, default="auto",
10011923Sandreas.sandberg@arm.com        help="Port (e.g., gdb) listener mode (auto: Enable if running " \
10111923Sandreas.sandberg@arm.com        "interactively) [Default: %default]")
10212012Sgabeblack@google.com    option("--listener-loopback-only", action="store_true", default=False,
10312012Sgabeblack@google.com        help="Port listeners will only accept connections over the " \
10412012Sgabeblack@google.com        "loopback device")
1058234Snate@binkert.org    option('-i', "--interactive", action="store_true", default=False,
1068234Snate@binkert.org        help="Invoke the interactive interpreter after running the script")
1078234Snate@binkert.org    option("--pdb", action="store_true", default=False,
1088234Snate@binkert.org        help="Invoke the python debugger before running the script")
1098234Snate@binkert.org    option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
1108234Snate@binkert.org        help="Prepend PATH to the system path when invoking the script")
1118234Snate@binkert.org    option('-q', "--quiet", action="count", default=0,
1128234Snate@binkert.org        help="Reduce verbosity")
1138234Snate@binkert.org    option('-v', "--verbose", action="count", default=0,
1148234Snate@binkert.org        help="Increase verbosity")
1152889Sbinkertn@umich.edu
1168234Snate@binkert.org    # Statistics options
1178234Snate@binkert.org    group("Statistics Options")
1188234Snate@binkert.org    option("--stats-file", metavar="FILE", default="stats.txt",
1198234Snate@binkert.org        help="Sets the output file for statistics [Default: %default]")
12014213Sandreas.sandberg@arm.com    option("--stats-help",
12114213Sandreas.sandberg@arm.com           action="callback", callback=_stats_help,
12214213Sandreas.sandberg@arm.com           help="Display documentation for available stat visitors")
1235773Snate@binkert.org
1248234Snate@binkert.org    # Configuration Options
1258234Snate@binkert.org    group("Configuration Options")
1268234Snate@binkert.org    option("--dump-config", metavar="FILE", default="config.ini",
1278234Snate@binkert.org        help="Dump configuration output file [Default: %default]")
1288664SAli.Saidi@ARM.com    option("--json-config", metavar="FILE", default="config.json",
1298664SAli.Saidi@ARM.com        help="Create JSON output of the configuration [Default: %default]")
1308998Suri.wiener@arm.com    option("--dot-config", metavar="FILE", default="config.dot",
1318998Suri.wiener@arm.com        help="Create DOT & pdf outputs of the configuration [Default: %default]")
13211431Ssascha.bischoff@arm.com    option("--dot-dvfs-config", metavar="FILE", default=None,
13311418Ssascha.bischoff@arm.com        help="Create DOT & pdf outputs of the DVFS configuration" + \
13411418Ssascha.bischoff@arm.com             " [Default: %default]")
1352889Sbinkertn@umich.edu
1368234Snate@binkert.org    # Debugging options
1378234Snate@binkert.org    group("Debugging Options")
13811299Ssteve.reinhardt@amd.com    option("--debug-break", metavar="TICK[,TICK]", action='append', split=',',
13911299Ssteve.reinhardt@amd.com        help="Create breakpoint(s) at TICK(s) " \
14011299Ssteve.reinhardt@amd.com             "(kills process if no debugger attached)")
1418234Snate@binkert.org    option("--debug-help", action='store_true',
1429960Sandreas.hansson@arm.com        help="Print help on debug flags")
1438234Snate@binkert.org    option("--debug-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1449960Sandreas.hansson@arm.com        help="Sets the flags for debug output (-FLAG disables a flag)")
14511299Ssteve.reinhardt@amd.com    option("--debug-start", metavar="TICK", type='int',
14611304Ssteve.reinhardt@amd.com        help="Start debug output at TICK")
14711338SMichael.Lebeane@amd.com    option("--debug-end", metavar="TICK", type='int',
14811338SMichael.Lebeane@amd.com        help="End debug output at TICK")
1499960Sandreas.hansson@arm.com    option("--debug-file", metavar="FILE", default="cout",
1509960Sandreas.hansson@arm.com        help="Sets the output file for debug [Default: %default]")
1519960Sandreas.hansson@arm.com    option("--debug-ignore", metavar="EXPR", action='append', split=':',
1529960Sandreas.hansson@arm.com        help="Ignore EXPR sim objects")
1538234Snate@binkert.org    option("--remote-gdb-port", type='int', default=7000,
1548234Snate@binkert.org        help="Remote gdb base port (set to 0 to disable listening)")
1552889Sbinkertn@umich.edu
1568234Snate@binkert.org    # Help options
1578234Snate@binkert.org    group("Help Options")
1588234Snate@binkert.org    option("--list-sim-objects", action='store_true', default=False,
1598234Snate@binkert.org        help="List all built-in SimObjects, their params and default values")
1606171Snate@binkert.org
1618234Snate@binkert.org    # load the options.py config file to allow people to set their own
1628234Snate@binkert.org    # default options
1638234Snate@binkert.org    options_file = config.get('options.py')
1648234Snate@binkert.org    if options_file:
1658234Snate@binkert.org        scope = { 'options' : options }
16613671Sandreas.sandberg@arm.com        exec(compile(open(options_file).read(), options_file, 'exec'), scope)
1678234Snate@binkert.org
1688234Snate@binkert.org    arguments = options.parse_args()
1698234Snate@binkert.org    return options,arguments
1706171Snate@binkert.org
1718219Snate@binkert.orgdef interact(scope):
1728327Sgblack@eecs.umich.edu    banner = "gem5 Interactive Console"
1739512Sandreas@sandberg.pp.se
1749512Sandreas@sandberg.pp.se    ipshell = None
1759512Sandreas@sandberg.pp.se    prompt_in1 = "gem5 \\#> "
1769512Sandreas@sandberg.pp.se    prompt_out = "gem5 \\#: "
1779512Sandreas@sandberg.pp.se
1789512Sandreas@sandberg.pp.se    # Is IPython version 0.10 or earlier available?
1798219Snate@binkert.org    try:
1808219Snate@binkert.org        from IPython.Shell import IPShellEmbed
1819512Sandreas@sandberg.pp.se        ipshell = IPShellEmbed(argv=["-prompt_in1", prompt_in1,
1829512Sandreas@sandberg.pp.se                                     "-prompt_out", prompt_out],
1839512Sandreas@sandberg.pp.se                               banner=banner, user_ns=scope)
1849512Sandreas@sandberg.pp.se    except ImportError:
1859512Sandreas@sandberg.pp.se        pass
1869512Sandreas@sandberg.pp.se
1879512Sandreas@sandberg.pp.se    # Is IPython version 0.11 or later available?
1889512Sandreas@sandberg.pp.se    if not ipshell:
1899512Sandreas@sandberg.pp.se        try:
1909512Sandreas@sandberg.pp.se            import IPython
1919512Sandreas@sandberg.pp.se            from IPython.config.loader import Config
19211635SCurtis.Dunham@arm.com            from IPython.terminal.embed import InteractiveShellEmbed
1939512Sandreas@sandberg.pp.se
1949512Sandreas@sandberg.pp.se            cfg = Config()
1959512Sandreas@sandberg.pp.se            cfg.PromptManager.in_template = prompt_in1
1969512Sandreas@sandberg.pp.se            cfg.PromptManager.out_template = prompt_out
1979512Sandreas@sandberg.pp.se            ipshell = InteractiveShellEmbed(config=cfg, user_ns=scope,
1989512Sandreas@sandberg.pp.se                                            banner1=banner)
1999512Sandreas@sandberg.pp.se        except ImportError:
2009512Sandreas@sandberg.pp.se            pass
2019512Sandreas@sandberg.pp.se
2029512Sandreas@sandberg.pp.se    if ipshell:
2038219Snate@binkert.org        ipshell()
2049512Sandreas@sandberg.pp.se    else:
2059512Sandreas@sandberg.pp.se        # Use the Python shell in the standard library if IPython
2069512Sandreas@sandberg.pp.se        # isn't available.
2078219Snate@binkert.org        code.InteractiveConsole(scope).interact(banner)
2088219Snate@binkert.org
20913671Sandreas.sandberg@arm.com
21013671Sandreas.sandberg@arm.comdef _check_tracing():
21113868Sciro.santilli@arm.com    from . import defines
21213674Sandreas.sandberg@arm.com
21313671Sandreas.sandberg@arm.com    if defines.TRACING_ON:
21413671Sandreas.sandberg@arm.com        return
21513671Sandreas.sandberg@arm.com
21613671Sandreas.sandberg@arm.com    fatal("Tracing is not enabled.  Compile with TRACING_ON")
21713671Sandreas.sandberg@arm.com
2188234Snate@binkert.orgdef main(*args):
2198245Snate@binkert.org    import m5
2208245Snate@binkert.org
22113714Sandreas.sandberg@arm.com    from . import core
22213714Sandreas.sandberg@arm.com    from . import debug
22313714Sandreas.sandberg@arm.com    from . import defines
22413714Sandreas.sandberg@arm.com    from . import event
22513714Sandreas.sandberg@arm.com    from . import info
22613714Sandreas.sandberg@arm.com    from . import stats
22713714Sandreas.sandberg@arm.com    from . import trace
2285799Snate@binkert.org
22913714Sandreas.sandberg@arm.com    from .util import inform, fatal, panic, isInteractive
2308234Snate@binkert.org
2318234Snate@binkert.org    if len(args) == 0:
2328234Snate@binkert.org        options, arguments = parse_options()
2338234Snate@binkert.org    elif len(args) == 2:
2348234Snate@binkert.org        options, arguments = args
2358234Snate@binkert.org    else:
23613663Sandreas.sandberg@arm.com        raise TypeError("main() takes 0 or 2 arguments (%d given)" % len(args))
2378234Snate@binkert.org
2388245Snate@binkert.org    m5.options = options
2398245Snate@binkert.org
2409983Sstever@gmail.com    # Set the main event queue for the main thread.
2419983Sstever@gmail.com    event.mainq = event.getEventQueue(0)
2429983Sstever@gmail.com    event.setEventQueue(event.mainq)
2439983Sstever@gmail.com
2445524Sstever@gmail.com    if not os.path.isdir(options.outdir):
2455524Sstever@gmail.com        os.makedirs(options.outdir)
2465524Sstever@gmail.com
2475524Sstever@gmail.com    # These filenames are used only if the redirect_std* options are set
2485524Sstever@gmail.com    stdout_file = os.path.join(options.outdir, options.stdout_file)
2495524Sstever@gmail.com    stderr_file = os.path.join(options.outdir, options.stderr_file)
2505524Sstever@gmail.com
2515524Sstever@gmail.com    # Print redirection notices here before doing any redirection
2525524Sstever@gmail.com    if options.redirect_stdout and not options.redirect_stderr:
25312563Sgabeblack@google.com        print("Redirecting stdout and stderr to", stdout_file)
2545524Sstever@gmail.com    else:
2555524Sstever@gmail.com        if options.redirect_stdout:
25612563Sgabeblack@google.com            print("Redirecting stdout to", stdout_file)
2575524Sstever@gmail.com        if options.redirect_stderr:
25812563Sgabeblack@google.com            print("Redirecting stderr to", stderr_file)
2595524Sstever@gmail.com
2605524Sstever@gmail.com    # Now redirect stdout/stderr as desired
2615524Sstever@gmail.com    if options.redirect_stdout:
2625524Sstever@gmail.com        redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
2635524Sstever@gmail.com        os.dup2(redir_fd, sys.stdout.fileno())
2645524Sstever@gmail.com        if not options.redirect_stderr:
2655524Sstever@gmail.com            os.dup2(redir_fd, sys.stderr.fileno())
2665524Sstever@gmail.com
2675524Sstever@gmail.com    if options.redirect_stderr:
2685524Sstever@gmail.com        redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
2695524Sstever@gmail.com        os.dup2(redir_fd, sys.stderr.fileno())
2705524Sstever@gmail.com
2712889Sbinkertn@umich.edu    done = False
2724850Snate@binkert.org
2734850Snate@binkert.org    if options.build_info:
2744850Snate@binkert.org        done = True
27512563Sgabeblack@google.com        print('Build information:')
27612563Sgabeblack@google.com        print()
27712563Sgabeblack@google.com        print('compiled %s' % defines.compileDate)
27812563Sgabeblack@google.com        print('build options:')
27913709Sandreas.sandberg@arm.com        keys = list(defines.buildEnv.keys())
2804850Snate@binkert.org        keys.sort()
2814850Snate@binkert.org        for key in keys:
2825801Snate@binkert.org            val = defines.buildEnv[key]
28312563Sgabeblack@google.com            print('    %s = %s' % (key, val))
28412563Sgabeblack@google.com        print()
2854850Snate@binkert.org
2862889Sbinkertn@umich.edu    if options.copyright:
2872889Sbinkertn@umich.edu        done = True
28812563Sgabeblack@google.com        print(info.COPYING)
28912563Sgabeblack@google.com        print()
2902889Sbinkertn@umich.edu
2912889Sbinkertn@umich.edu    if options.readme:
2922889Sbinkertn@umich.edu        done = True
29312563Sgabeblack@google.com        print('Readme:')
29412563Sgabeblack@google.com        print()
29512563Sgabeblack@google.com        print(info.README)
29612563Sgabeblack@google.com        print()
2972889Sbinkertn@umich.edu
2988232Snate@binkert.org    if options.debug_help:
2994053Sbinkertn@umich.edu        done = True
30013671Sandreas.sandberg@arm.com        _check_tracing()
3018232Snate@binkert.org        debug.help()
3024053Sbinkertn@umich.edu
3035473Snate@binkert.org    if options.list_sim_objects:
30413714Sandreas.sandberg@arm.com        from . import SimObject
3055473Snate@binkert.org        done = True
30612563Sgabeblack@google.com        print("SimObjects:")
30713709Sandreas.sandberg@arm.com        objects = list(SimObject.allClasses.keys())
3085473Snate@binkert.org        objects.sort()
3095473Snate@binkert.org        for name in objects:
3105473Snate@binkert.org            obj = SimObject.allClasses[name]
31112563Sgabeblack@google.com            print("    %s" % obj)
31213709Sandreas.sandberg@arm.com            params = list(obj._params.keys())
3135473Snate@binkert.org            params.sort()
3145473Snate@binkert.org            for pname in params:
3155473Snate@binkert.org                param = obj._params[pname]
3165473Snate@binkert.org                default = getattr(param, 'default', '')
31712563Sgabeblack@google.com                print("        %s" % pname)
3185473Snate@binkert.org                if default:
31912563Sgabeblack@google.com                    print("            default: %s" % default)
32012563Sgabeblack@google.com                print("            desc: %s" % param.desc)
32112563Sgabeblack@google.com                print()
32212563Sgabeblack@google.com            print()
3235473Snate@binkert.org
3242889Sbinkertn@umich.edu    if done:
3252889Sbinkertn@umich.edu        sys.exit(0)
3262889Sbinkertn@umich.edu
3275470Snate@binkert.org    # setting verbose and quiet at the same time doesn't make sense
3285470Snate@binkert.org    if options.verbose > 0 and options.quiet > 0:
3295470Snate@binkert.org        options.usage(2)
3305470Snate@binkert.org
3315470Snate@binkert.org    verbose = options.verbose - options.quiet
33210134Sstan.czerniawski@arm.com    if verbose >= 0:
33312563Sgabeblack@google.com        print("gem5 Simulator System.  http://gem5.org")
33412563Sgabeblack@google.com        print(brief_copyright)
33512563Sgabeblack@google.com        print()
3365801Snate@binkert.org
33712563Sgabeblack@google.com        print("gem5 compiled %s" % defines.compileDate)
3385456Ssaidi@eecs.umich.edu
33912563Sgabeblack@google.com        print("gem5 started %s" %
34012563Sgabeblack@google.com              datetime.datetime.now().strftime("%b %e %Y %X"))
34112563Sgabeblack@google.com        print("gem5 executing on %s, pid %d" %
34212563Sgabeblack@google.com              (socket.gethostname(), os.getpid()))
3435528Sstever@gmail.com
34410758Ssteve.reinhardt@amd.com        # in Python 3 pipes.quote() is moved to shlex.quote()
34510758Ssteve.reinhardt@amd.com        import pipes
34612563Sgabeblack@google.com        print("command line:", " ".join(map(pipes.quote, sys.argv)))
34712563Sgabeblack@google.com        print()
3482889Sbinkertn@umich.edu
3492889Sbinkertn@umich.edu    # check to make sure we can find the listed script
3502889Sbinkertn@umich.edu    if not arguments or not os.path.isfile(arguments[0]):
3512922Sktlim@umich.edu        if arguments and not os.path.isfile(arguments[0]):
35212563Sgabeblack@google.com            print("Script %s not found" % arguments[0])
3534053Sbinkertn@umich.edu
3545470Snate@binkert.org        options.usage(2)
3552889Sbinkertn@umich.edu
3562889Sbinkertn@umich.edu    # tell C++ about output directory
3575801Snate@binkert.org    core.setOutputDir(options.outdir)
3582889Sbinkertn@umich.edu
3592889Sbinkertn@umich.edu    # update the system path with elements from the -p option
3602889Sbinkertn@umich.edu    sys.path[0:0] = options.path
3612889Sbinkertn@umich.edu
3622889Sbinkertn@umich.edu    # set stats options
36311878Sandreas.sandberg@arm.com    stats.addStatVisitor(options.stats_file)
3642889Sbinkertn@umich.edu
36511923Sandreas.sandberg@arm.com    # Disable listeners unless running interactively or explicitly
36611923Sandreas.sandberg@arm.com    # enabled
36711923Sandreas.sandberg@arm.com    if options.listener_mode == "off":
36811923Sandreas.sandberg@arm.com        m5.disableAllListeners()
36911923Sandreas.sandberg@arm.com    elif options.listener_mode == "auto":
37011923Sandreas.sandberg@arm.com        if not isInteractive():
37111923Sandreas.sandberg@arm.com            inform("Standard input is not a terminal, disabling listeners.")
37211923Sandreas.sandberg@arm.com            m5.disableAllListeners()
37311923Sandreas.sandberg@arm.com    elif options.listener_mode == "on":
37411923Sandreas.sandberg@arm.com        pass
37511923Sandreas.sandberg@arm.com    else:
37611923Sandreas.sandberg@arm.com        panic("Unhandled listener mode: %s" % options.listener_mode)
37711923Sandreas.sandberg@arm.com
37812012Sgabeblack@google.com    if options.listener_loopback_only:
37912012Sgabeblack@google.com        m5.listenersLoopbackOnly()
38012012Sgabeblack@google.com
3812889Sbinkertn@umich.edu    # set debugging options
3825801Snate@binkert.org    debug.setRemoteGDBPort(options.remote_gdb_port)
3833645Sbinkertn@umich.edu    for when in options.debug_break:
3849960Sandreas.hansson@arm.com        debug.schedBreak(int(when))
3852889Sbinkertn@umich.edu
3868232Snate@binkert.org    if options.debug_flags:
38713671Sandreas.sandberg@arm.com        _check_tracing()
3884053Sbinkertn@umich.edu
3895586Snate@binkert.org        on_flags = []
3905586Snate@binkert.org        off_flags = []
3918232Snate@binkert.org        for flag in options.debug_flags:
3925586Snate@binkert.org            off = False
3935586Snate@binkert.org            if flag.startswith('-'):
3945586Snate@binkert.org                flag = flag[1:]
3955586Snate@binkert.org                off = True
3968232Snate@binkert.org
3978232Snate@binkert.org            if flag not in debug.flags:
39812563Sgabeblack@google.com                print("invalid debug flag '%s'" % flag, file=sys.stderr)
3995586Snate@binkert.org                sys.exit(1)
4004053Sbinkertn@umich.edu
4015586Snate@binkert.org            if off:
4028232Snate@binkert.org                debug.flags[flag].disable()
4035586Snate@binkert.org            else:
4048232Snate@binkert.org                debug.flags[flag].enable()
4054053Sbinkertn@umich.edu
4069960Sandreas.hansson@arm.com    if options.debug_start:
40713671Sandreas.sandberg@arm.com        _check_tracing()
4089960Sandreas.hansson@arm.com        e = event.create(trace.enable, event.Event.Debug_Enable_Pri)
4099960Sandreas.hansson@arm.com        event.mainq.schedule(e, options.debug_start)
4104074Sbinkertn@umich.edu    else:
4115799Snate@binkert.org        trace.enable()
4124042Sbinkertn@umich.edu
41311338SMichael.Lebeane@amd.com    if options.debug_end:
41413671Sandreas.sandberg@arm.com        _check_tracing()
41511338SMichael.Lebeane@amd.com        e = event.create(trace.disable, event.Event.Debug_Enable_Pri)
41611338SMichael.Lebeane@amd.com        event.mainq.schedule(e, options.debug_end)
41711338SMichael.Lebeane@amd.com
4189960Sandreas.hansson@arm.com    trace.output(options.debug_file)
4194042Sbinkertn@umich.edu
4209960Sandreas.hansson@arm.com    for ignore in options.debug_ignore:
42113671Sandreas.sandberg@arm.com        _check_tracing()
4225799Snate@binkert.org        trace.ignore(ignore)
4232889Sbinkertn@umich.edu
4242889Sbinkertn@umich.edu    sys.argv = arguments
4252889Sbinkertn@umich.edu    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
4262891Sbinkertn@umich.edu
4275604Snate@binkert.org    filename = sys.argv[0]
42813670Sandreas.sandberg@arm.com    filedata = open(filename, 'r').read()
4295604Snate@binkert.org    filecode = compile(filedata, filename, 'exec')
4305604Snate@binkert.org    scope = { '__file__' : filename,
4313887Sbinkertn@umich.edu              '__name__' : '__m5_main__' }
4322899Sbinkertn@umich.edu
4332899Sbinkertn@umich.edu    # if pdb was requested, execfile the thing under pdb, otherwise,
4342899Sbinkertn@umich.edu    # just do the execfile normally
4352899Sbinkertn@umich.edu    if options.pdb:
4365604Snate@binkert.org        import pdb
4375604Snate@binkert.org        import traceback
4385604Snate@binkert.org
4395604Snate@binkert.org        pdb = pdb.Pdb()
4405604Snate@binkert.org        try:
4415604Snate@binkert.org            pdb.run(filecode, scope)
4425604Snate@binkert.org        except SystemExit:
44312563Sgabeblack@google.com            print("The program exited via sys.exit(). Exit status: ", end=' ')
44412563Sgabeblack@google.com            print(sys.exc_info()[1])
4455604Snate@binkert.org        except:
4465604Snate@binkert.org            traceback.print_exc()
44712563Sgabeblack@google.com            print("Uncaught exception. Entering post mortem debugging")
4485604Snate@binkert.org            t = sys.exc_info()[2]
4495604Snate@binkert.org            while t.tb_next is not None:
4505604Snate@binkert.org                t = t.tb_next
4515604Snate@binkert.org                pdb.interaction(t.tb_frame,t)
4522899Sbinkertn@umich.edu    else:
45313671Sandreas.sandberg@arm.com        exec(filecode, scope)
4542889Sbinkertn@umich.edu
4552889Sbinkertn@umich.edu    # once the script is done
4562889Sbinkertn@umich.edu    if options.interactive:
4578219Snate@binkert.org        interact(scope)
4582889Sbinkertn@umich.edu
4592889Sbinkertn@umich.eduif __name__ == '__main__':
4602889Sbinkertn@umich.edu    from pprint import pprint
4612889Sbinkertn@umich.edu
4628234Snate@binkert.org    options, arguments = parse_options()
4638234Snate@binkert.org
46412563Sgabeblack@google.com    print('opts:')
4652889Sbinkertn@umich.edu    pprint(options, indent=4)
46612563Sgabeblack@google.com    print()
4672889Sbinkertn@umich.edu
46812563Sgabeblack@google.com    print('args:')
4692889Sbinkertn@umich.edu    pprint(arguments, indent=4)
470