sim: Move CPU-specific methods from SimObject to the BaseCPU class
[gem5.git] / src / python / m5 / main.py
index ccd6c5807f09fd33c61ce0b7645a15b8063db316..ce2979cd218d617fa1626aacb20aa83815af77e4 100644 (file)
 #
 # Authors: Nathan Binkert
 
-import code, optparse, os, socket, sys
-from datetime import datetime
-from attrdict import attrdict
-
-try:
-    import info
-except ImportError:
-    info = None
+import code
+import datetime
+import os
+import socket
+import sys
 
 __all__ = [ 'options', 'arguments', 'main' ]
 
-usage="%prog [m5 options] script.py [script options]"
+usage="%prog [gem5 options] script.py [script options]"
 version="%prog 2.0"
-brief_copyright='''
-Copyright (c) 2001-2006
-The Regents of The University of Michigan
-All Rights Reserved
-'''
-
-# there's only one option parsing done, so make it global and add some
-# helper functions to make it work well.
-parser = optparse.OptionParser(usage=usage, version=version,
-                               description=brief_copyright,
-                               formatter=optparse.TitledHelpFormatter())
-parser.disable_interspersed_args()
-
-# current option group
-group = None
-
-def set_group(*args, **kwargs):
-    '''set the current option group'''
-    global group
-    if not args and not kwargs:
-        group = None
-    else:
-        group = parser.add_option_group(*args, **kwargs)
-
-class splitter(object):
-    def __init__(self, split):
-        self.split = split
-    def __call__(self, option, opt_str, value, parser):
-        getattr(parser.values, option.dest).extend(value.split(self.split))
-
-def add_option(*args, **kwargs):
-    '''add an option to the current option group, or global none set'''
-
-    # if action=split, but allows the option arguments
-    # themselves to be lists separated by the split variable'''
-
-    if kwargs.get('action', None) == 'append' and 'split' in kwargs:
-        split = kwargs.pop('split')
-        kwargs['default'] = []
-        kwargs['type'] = 'string'
-        kwargs['action'] = 'callback'
-        kwargs['callback'] = splitter(split)
-
-    if group:
-        return group.add_option(*args, **kwargs)
-
-    return parser.add_option(*args, **kwargs)
-
-def bool_option(name, default, help):
-    '''add a boolean option called --name and --no-name.
-    Display help depending on which is the default'''
-
-    tname = '--%s' % name
-    fname = '--no-%s' % name
-    dest = name.replace('-', '_')
-    if default:
-        thelp = optparse.SUPPRESS_HELP
-        fhelp = help
+brief_copyright=\
+    "gem5 is copyrighted software; use the --copyright option for details."
+
+def parse_options():
+    import config
+    from options import OptionParser
+
+    options = OptionParser(usage=usage, version=version,
+                           description=brief_copyright)
+    option = options.add_option
+    group = options.set_group
+
+    # Help options
+    option('-B', "--build-info", action="store_true", default=False,
+        help="Show build information")
+    option('-C', "--copyright", action="store_true", default=False,
+        help="Show full copyright information")
+    option('-R', "--readme", action="store_true", default=False,
+        help="Show the readme")
+
+    # Options for configuring the base simulator
+    option('-d', "--outdir", metavar="DIR", default="m5out",
+        help="Set the output directory to DIR [Default: %default]")
+    option('-r', "--redirect-stdout", action="store_true", default=False,
+        help="Redirect stdout (& stderr, without -e) to file")
+    option('-e', "--redirect-stderr", action="store_true", default=False,
+        help="Redirect stderr to file")
+    option("--stdout-file", metavar="FILE", default="simout",
+        help="Filename for -r redirection [Default: %default]")
+    option("--stderr-file", metavar="FILE", default="simerr",
+        help="Filename for -e redirection [Default: %default]")
+    option('-i', "--interactive", action="store_true", default=False,
+        help="Invoke the interactive interpreter after running the script")
+    option("--pdb", action="store_true", default=False,
+        help="Invoke the python debugger before running the script")
+    option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
+        help="Prepend PATH to the system path when invoking the script")
+    option('-q', "--quiet", action="count", default=0,
+        help="Reduce verbosity")
+    option('-v', "--verbose", action="count", default=0,
+        help="Increase verbosity")
+
+    # Statistics options
+    group("Statistics Options")
+    option("--stats-file", metavar="FILE", default="stats.txt",
+        help="Sets the output file for statistics [Default: %default]")
+
+    # Configuration Options
+    group("Configuration Options")
+    option("--dump-config", metavar="FILE", default="config.ini",
+        help="Dump configuration output file [Default: %default]")
+    option("--json-config", metavar="FILE", default="config.json",
+        help="Create JSON output of the configuration [Default: %default]")
+    option("--dot-config", metavar="FILE", default="config.dot",
+        help="Create DOT & pdf outputs of the configuration [Default: %default]")
+
+    # Debugging options
+    group("Debugging Options")
+    option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
+        help="Cycle to create a breakpoint")
+    option("--debug-help", action='store_true',
+        help="Print help on trace flags")
+    option("--debug-flags", metavar="FLAG[,FLAG]", action='append', split=',',
+        help="Sets the flags for tracing (-FLAG disables a flag)")
+    option("--remote-gdb-port", type='int', default=7000,
+        help="Remote gdb base port (set to 0 to disable listening)")
+
+    # Tracing options
+    group("Trace Options")
+    option("--trace-start", metavar="TIME", type='int',
+        help="Start tracing at TIME (must be in ticks)")
+    option("--trace-file", metavar="FILE", default="cout",
+        help="Sets the output file for tracing [Default: %default]")
+    option("--trace-ignore", metavar="EXPR", action='append', split=':',
+        help="Ignore EXPR sim objects")
+
+    # Help options
+    group("Help Options")
+    option("--list-sim-objects", action='store_true', default=False,
+        help="List all built-in SimObjects, their params and default values")
+
+    # load the options.py config file to allow people to set their own
+    # default options
+    options_file = config.get('options.py')
+    if options_file:
+        scope = { 'options' : options }
+        execfile(options_file, scope)
+
+    arguments = options.parse_args()
+    return options,arguments
+
+def interact(scope):
+    banner = "gem5 Interactive Console"
+    sys.argv = []
+    try:
+        from IPython.Shell import IPShellEmbed
+        ipshell = IPShellEmbed(banner=banner,user_ns=scope)
+        ipshell()
+    except ImportError:
+        code.InteractiveConsole(scope).interact(banner)
+
+def main(*args):
+    import m5
+
+    import core
+    import debug
+    import defines
+    import event
+    import info
+    import stats
+    import trace
+
+    from util import fatal
+
+    if len(args) == 0:
+        options, arguments = parse_options()
+    elif len(args) == 2:
+        options, arguments = args
     else:
-        thelp = help
-        fhelp = optparse.SUPPRESS_HELP
-
-    add_option(tname, action="store_true", default=default, help=thelp)
-    add_option(fname, action="store_false", dest=dest, help=fhelp)
-
-# Help options
-add_option('-A', "--authors", action="store_true", default=False,
-    help="Show author information")
-add_option('-C', "--copyright", action="store_true", default=False,
-    help="Show full copyright information")
-add_option('-R', "--readme", action="store_true", default=False,
-    help="Show the readme")
-add_option('-N', "--release-notes", action="store_true", default=False,
-    help="Show the release notes")
-
-# Options for configuring the base simulator
-add_option('-d', "--outdir", metavar="DIR", default=".",
-    help="Set the output directory to DIR [Default: %default]")
-add_option('-i', "--interactive", action="store_true", default=False,
-    help="Invoke the interactive interpreter after running the script")
-add_option("--pdb", action="store_true", default=False,
-    help="Invoke the python debugger before running the script")
-add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
-    help="Prepend PATH to the system path when invoking the script")
-add_option('-q', "--quiet", action="count", default=0,
-    help="Reduce verbosity")
-add_option('-v', "--verbose", action="count", default=0,
-    help="Increase verbosity")
-
-# Statistics options
-set_group("Statistics Options")
-add_option("--stats-file", metavar="FILE", default="m5stats.txt",
-    help="Sets the output file for statistics [Default: %default]")
-
-# Debugging options
-set_group("Debugging Options")
-add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
-    help="Cycle to create a breakpoint")
-
-# Tracing options
-set_group("Trace Options")
-add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
-    help="Sets the flags for tracing")
-add_option("--trace-start", metavar="TIME", default='0s',
-    help="Start tracing at TIME (must have units)")
-add_option("--trace-cycle", metavar="CYCLE", default='0',
-    help="Start tracing at CYCLE")
-add_option("--trace-file", metavar="FILE", default="cout",
-    help="Sets the output file for tracing [Default: %default]")
-add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
-    help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
-add_option("--no-trace-circlebuf", action="store_const", const=0,
-    dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
-bool_option("trace-dumponexit", default=False,
-    help="Dump trace buffer on exit")
-add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
-    help="Ignore EXPR sim objects")
-
-# Execution Trace options
-set_group("Execution Trace Options")
-bool_option("speculative", default=True,
-    help="Don't capture speculative instructions")
-bool_option("print-cycle", default=True,
-    help="Don't print cycle numbers in trace output")
-bool_option("print-symbol", default=True,
-    help="Disable PC symbols in trace output")
-bool_option("print-opclass", default=True,
-    help="Don't print op class type in trace output")
-bool_option("print-thread", default=True,
-    help="Don't print thread number in trace output")
-bool_option("print-effaddr", default=True,
-    help="Don't print effective address in trace output")
-bool_option("print-data", default=True,
-    help="Don't print result data in trace output")
-bool_option("print-iregs", default=False,
-    help="Print fetch sequence numbers in trace output")
-bool_option("print-fetch-seq", default=False,
-    help="Print fetch sequence numbers in trace output")
-bool_option("print-cpseq", default=False,
-    help="Print correct path sequence numbers in trace output")
-#bool_option("print-reg-delta", default=False,
-#    help="Print which registers changed to what in trace output")
-
-options = attrdict()
-arguments = []
-
-def usage(exitcode=None):
-    parser.print_help()
-    if exitcode is not None:
-        sys.exit(exitcode)
-
-def parse_args():
-    _opts,args = parser.parse_args()
-    opts = attrdict(_opts.__dict__)
+        raise TypeError, "main() takes 0 or 2 arguments (%d given)" % len(args)
 
-    # setting verbose and quiet at the same time doesn't make sense
-    if opts.verbose > 0 and opts.quiet > 0:
-        usage(2)
+    m5.options = options
 
-    # store the verbosity in a single variable.  0 is default,
-    # negative numbers represent quiet and positive values indicate verbose
-    opts.verbose -= opts.quiet
+    def check_tracing():
+        if defines.TRACING_ON:
+            return
 
-    del opts.quiet
+        fatal("Tracing is not enabled.  Compile with TRACING_ON")
 
-    options.update(opts)
-    arguments.extend(args)
-    return opts,args
+    if not os.path.isdir(options.outdir):
+        os.makedirs(options.outdir)
 
-def main():
-    import cc_main
+    # These filenames are used only if the redirect_std* options are set
+    stdout_file = os.path.join(options.outdir, options.stdout_file)
+    stderr_file = os.path.join(options.outdir, options.stderr_file)
 
-    parse_args()
+    # Print redirection notices here before doing any redirection
+    if options.redirect_stdout and not options.redirect_stderr:
+        print "Redirecting stdout and stderr to", stdout_file
+    else:
+        if options.redirect_stdout:
+            print "Redirecting stdout to", stdout_file
+        if options.redirect_stderr:
+            print "Redirecting stderr to", stderr_file
+
+    # Now redirect stdout/stderr as desired
+    if options.redirect_stdout:
+        redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
+        os.dup2(redir_fd, sys.stdout.fileno())
+        if not options.redirect_stderr:
+            os.dup2(redir_fd, sys.stderr.fileno())
+
+    if options.redirect_stderr:
+        redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
+        os.dup2(redir_fd, sys.stderr.fileno())
 
     done = False
-    if options.copyright:
+
+    if options.build_info:
         done = True
-        print info.LICENSE
+        print 'Build information:'
+        print
+        print 'compiled %s' % defines.compileDate;
+        print 'build options:'
+        keys = defines.buildEnv.keys()
+        keys.sort()
+        for key in keys:
+            val = defines.buildEnv[key]
+            print '    %s = %s' % (key, val)
         print
 
-    if options.authors:
+    if options.copyright:
         done = True
-        print 'Author information:'
-        print
-        print info.AUTHORS
+        print info.COPYING
         print
 
     if options.readme:
@@ -233,23 +219,51 @@ def main():
         print info.README
         print
 
-    if options.release_notes:
+    if options.debug_help:
         done = True
-        print 'Release Notes:'
-        print
-        print info.RELEASE_NOTES
-        print
+        check_tracing()
+        debug.help()
+
+    if options.list_sim_objects:
+        import SimObject
+        done = True
+        print "SimObjects:"
+        objects = SimObject.allClasses.keys()
+        objects.sort()
+        for name in objects:
+            obj = SimObject.allClasses[name]
+            print "    %s" % obj
+            params = obj._params.keys()
+            params.sort()
+            for pname in params:
+                param = obj._params[pname]
+                default = getattr(param, 'default', '')
+                print "        %s" % pname
+                if default:
+                    print "            default: %s" % default
+                print "            desc: %s" % param.desc
+                print
+            print
 
     if done:
         sys.exit(0)
 
+    # setting verbose and quiet at the same time doesn't make sense
+    if options.verbose > 0 and options.quiet > 0:
+        options.usage(2)
+
+    verbose = options.verbose - options.quiet
     if options.verbose >= 0:
-        print "M5 Simulator System"
+        print "gem5 Simulator System.  http://gem5.org"
         print brief_copyright
         print
-        print "M5 compiled %s" % cc_main.cvar.compileDate;
-        print "M5 started %s" % datetime.now().ctime()
-        print "M5 executing on %s" % socket.gethostname()
+
+        print "gem5 compiled %s" % defines.compileDate;
+
+        print "gem5 started %s" % \
+            datetime.datetime.now().strftime("%b %e %Y %X")
+        print "gem5 executing on %s" % socket.gethostname()
+
         print "command line:",
         for argv in sys.argv:
             print argv,
@@ -259,71 +273,99 @@ def main():
     if not arguments or not os.path.isfile(arguments[0]):
         if arguments and not os.path.isfile(arguments[0]):
             print "Script %s not found" % arguments[0]
-        usage(2)
+
+        options.usage(2)
 
     # tell C++ about output directory
-    cc_main.setOutputDir(options.outdir)
+    core.setOutputDir(options.outdir)
 
     # update the system path with elements from the -p option
     sys.path[0:0] = options.path
 
-    import objects
-
     # set stats options
-    objects.Statistics.text_file = options.stats_file
+    stats.initText(options.stats_file)
 
     # set debugging options
-    objects.Debug.break_cycles = options.debug_break
-
-    # set tracing options
-    objects.Trace.flags = options.trace_flags
-    objects.Trace.start = options.trace_start
-    objects.Trace.cycle = options.trace_cycle
-    objects.Trace.file = options.trace_file
-    objects.Trace.bufsize = options.trace_circlebuf
-    objects.Trace.dump_on_exit = options.trace_dumponexit
-    objects.Trace.ignore = options.trace_ignore
-
-    # set execution trace options
-    objects.ExecutionTrace.speculative = options.speculative
-    objects.ExecutionTrace.print_cycle = options.print_cycle
-    objects.ExecutionTrace.pc_symbol = options.print_symbol
-    objects.ExecutionTrace.print_opclass = options.print_opclass
-    objects.ExecutionTrace.print_thread = options.print_thread
-    objects.ExecutionTrace.print_effaddr = options.print_effaddr
-    objects.ExecutionTrace.print_data = options.print_data
-    objects.ExecutionTrace.print_iregs = options.print_iregs
-    objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
-    objects.ExecutionTrace.print_cpseq = options.print_cpseq
-    #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta
+    debug.setRemoteGDBPort(options.remote_gdb_port)
+    for when in options.debug_break:
+        debug.schedBreakCycle(int(when))
+
+    if options.debug_flags:
+        check_tracing()
+
+        on_flags = []
+        off_flags = []
+        for flag in options.debug_flags:
+            off = False
+            if flag.startswith('-'):
+                flag = flag[1:]
+                off = True
+
+            if flag not in debug.flags:
+                print >>sys.stderr, "invalid debug flag '%s'" % flag
+                sys.exit(1)
+
+            if off:
+                debug.flags[flag].disable()
+            else:
+                debug.flags[flag].enable()
+
+    if options.trace_start:
+        check_tracing()
+        e = event.create(trace.enable, event.Event.Trace_Enable_Pri)
+        event.mainq.schedule(e, options.trace_start)
+    else:
+        trace.enable()
+
+    trace.output(options.trace_file)
+
+    for ignore in options.trace_ignore:
+        check_tracing()
+        trace.ignore(ignore)
 
     sys.argv = arguments
     sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
 
-    scope = { '__file__' : sys.argv[0] }
+    filename = sys.argv[0]
+    filedata = file(filename, 'r').read()
+    filecode = compile(filedata, filename, 'exec')
+    scope = { '__file__' : filename,
+              '__name__' : '__m5_main__' }
 
     # we want readline if we're doing anything interactive
     if options.interactive or options.pdb:
-        exec("import readline", scope)
+        exec "import readline" in scope
 
     # if pdb was requested, execfile the thing under pdb, otherwise,
     # just do the execfile normally
     if options.pdb:
-        from pdb import Pdb
-        debugger = Pdb()
-        debugger.run('execfile("%s")' % sys.argv[0], scope)
+        import pdb
+        import traceback
+
+        pdb = pdb.Pdb()
+        try:
+            pdb.run(filecode, scope)
+        except SystemExit:
+            print "The program exited via sys.exit(). Exit status: ",
+            print sys.exc_info()[1]
+        except:
+            traceback.print_exc()
+            print "Uncaught exception. Entering post mortem debugging"
+            t = sys.exc_info()[2]
+            while t.tb_next is not None:
+                t = t.tb_next
+                pdb.interaction(t.tb_frame,t)
     else:
-        execfile(sys.argv[0], scope)
+        exec filecode in scope
 
     # once the script is done
     if options.interactive:
-        interact = code.InteractiveConsole(scope)
-        interact.interact("M5 Interactive Console")
+        interact(scope)
 
 if __name__ == '__main__':
     from pprint import pprint
 
-    parse_args()
+    options, arguments = parse_options()
 
     print 'opts:'
     pprint(options, indent=4)