1 # Copyright (c) 2005 The Regents of The University of Michigan
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # Authors: Nathan Binkert
35 __all__
= [ 'options', 'arguments', 'main' ]
37 usage
="%prog [gem5 options] script.py [script options]"
40 "gem5 is copyrighted software; use the --copyright option for details."
44 from options
import OptionParser
46 options
= OptionParser(usage
=usage
, version
=version
,
47 description
=brief_copyright
)
48 option
= options
.add_option
49 group
= options
.set_group
52 option('-B', "--build-info", action
="store_true", default
=False,
53 help="Show build information")
54 option('-C', "--copyright", action
="store_true", default
=False,
55 help="Show full copyright information")
56 option('-R', "--readme", action
="store_true", default
=False,
57 help="Show the readme")
59 # Options for configuring the base simulator
60 option('-d', "--outdir", metavar
="DIR", default
="m5out",
61 help="Set the output directory to DIR [Default: %default]")
62 option('-r', "--redirect-stdout", action
="store_true", default
=False,
63 help="Redirect stdout (& stderr, without -e) to file")
64 option('-e', "--redirect-stderr", action
="store_true", default
=False,
65 help="Redirect stderr to file")
66 option("--stdout-file", metavar
="FILE", default
="simout",
67 help="Filename for -r redirection [Default: %default]")
68 option("--stderr-file", metavar
="FILE", default
="simerr",
69 help="Filename for -e redirection [Default: %default]")
70 option('-i', "--interactive", action
="store_true", default
=False,
71 help="Invoke the interactive interpreter after running the script")
72 option("--pdb", action
="store_true", default
=False,
73 help="Invoke the python debugger before running the script")
74 option('-p', "--path", metavar
="PATH[:PATH]", action
='append', split
=':',
75 help="Prepend PATH to the system path when invoking the script")
76 option('-q', "--quiet", action
="count", default
=0,
77 help="Reduce verbosity")
78 option('-v', "--verbose", action
="count", default
=0,
79 help="Increase verbosity")
82 group("Statistics Options")
83 option("--stats-file", metavar
="FILE", default
="stats.txt",
84 help="Sets the output file for statistics [Default: %default]")
86 # Configuration Options
87 group("Configuration Options")
88 option("--dump-config", metavar
="FILE", default
="config.ini",
89 help="Dump configuration output file [Default: %default]")
90 option("--json-config", metavar
="FILE", default
="config.json",
91 help="Create JSON output of the configuration [Default: %default]")
92 option("--dot-config", metavar
="FILE", default
="config.dot",
93 help="Create DOT & pdf outputs of the configuration [Default: %default]")
94 option("--dot-dvfs-config", metavar
="FILE", default
=None,
95 help="Create DOT & pdf outputs of the DVFS configuration" + \
96 " [Default: %default]")
99 group("Debugging Options")
100 option("--debug-break", metavar
="TICK[,TICK]", action
='append', split
=',',
101 help="Create breakpoint(s) at TICK(s) " \
102 "(kills process if no debugger attached)")
103 option("--debug-help", action
='store_true',
104 help="Print help on debug flags")
105 option("--debug-flags", metavar
="FLAG[,FLAG]", action
='append', split
=',',
106 help="Sets the flags for debug output (-FLAG disables a flag)")
107 option("--debug-start", metavar
="TICK", type='int',
108 help="Start debug output at TICK")
109 option("--debug-end", metavar
="TICK", type='int',
110 help="End debug output at TICK")
111 option("--debug-file", metavar
="FILE", default
="cout",
112 help="Sets the output file for debug [Default: %default]")
113 option("--debug-ignore", metavar
="EXPR", action
='append', split
=':',
114 help="Ignore EXPR sim objects")
115 option("--remote-gdb-port", type='int', default
=7000,
116 help="Remote gdb base port (set to 0 to disable listening)")
119 group("Help Options")
120 option("--list-sim-objects", action
='store_true', default
=False,
121 help="List all built-in SimObjects, their params and default values")
123 # load the options.py config file to allow people to set their own
125 options_file
= config
.get('options.py')
127 scope
= { 'options' : options
}
128 execfile(options_file
, scope
)
130 arguments
= options
.parse_args()
131 return options
,arguments
134 banner
= "gem5 Interactive Console"
137 prompt_in1
= "gem5 \\#> "
138 prompt_out
= "gem5 \\#: "
140 # Is IPython version 0.10 or earlier available?
142 from IPython
.Shell
import IPShellEmbed
143 ipshell
= IPShellEmbed(argv
=["-prompt_in1", prompt_in1
,
144 "-prompt_out", prompt_out
],
145 banner
=banner
, user_ns
=scope
)
149 # Is IPython version 0.11 or later available?
153 from IPython
.config
.loader
import Config
154 from IPython
.terminal
.embed
import InteractiveShellEmbed
157 cfg
.PromptManager
.in_template
= prompt_in1
158 cfg
.PromptManager
.out_template
= prompt_out
159 ipshell
= InteractiveShellEmbed(config
=cfg
, user_ns
=scope
,
167 # Use the Python shell in the standard library if IPython
169 code
.InteractiveConsole(scope
).interact(banner
)
182 from util
import fatal
185 options
, arguments
= parse_options()
187 options
, arguments
= args
189 raise TypeError, "main() takes 0 or 2 arguments (%d given)" % len(args
)
194 if defines
.TRACING_ON
:
197 fatal("Tracing is not enabled. Compile with TRACING_ON")
199 # Set the main event queue for the main thread.
200 event
.mainq
= event
.getEventQueue(0)
201 event
.setEventQueue(event
.mainq
)
203 if not os
.path
.isdir(options
.outdir
):
204 os
.makedirs(options
.outdir
)
206 # These filenames are used only if the redirect_std* options are set
207 stdout_file
= os
.path
.join(options
.outdir
, options
.stdout_file
)
208 stderr_file
= os
.path
.join(options
.outdir
, options
.stderr_file
)
210 # Print redirection notices here before doing any redirection
211 if options
.redirect_stdout
and not options
.redirect_stderr
:
212 print "Redirecting stdout and stderr to", stdout_file
214 if options
.redirect_stdout
:
215 print "Redirecting stdout to", stdout_file
216 if options
.redirect_stderr
:
217 print "Redirecting stderr to", stderr_file
219 # Now redirect stdout/stderr as desired
220 if options
.redirect_stdout
:
221 redir_fd
= os
.open(stdout_file
, os
. O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
222 os
.dup2(redir_fd
, sys
.stdout
.fileno())
223 if not options
.redirect_stderr
:
224 os
.dup2(redir_fd
, sys
.stderr
.fileno())
226 if options
.redirect_stderr
:
227 redir_fd
= os
.open(stderr_file
, os
. O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
228 os
.dup2(redir_fd
, sys
.stderr
.fileno())
232 if options
.build_info
:
234 print 'Build information:'
236 print 'compiled %s' % defines
.compileDate
;
237 print 'build options:'
238 keys
= defines
.buildEnv
.keys()
241 val
= defines
.buildEnv
[key
]
242 print ' %s = %s' % (key
, val
)
245 if options
.copyright
:
257 if options
.debug_help
:
262 if options
.list_sim_objects
:
266 objects
= SimObject
.allClasses
.keys()
269 obj
= SimObject
.allClasses
[name
]
271 params
= obj
._params
.keys()
274 param
= obj
._params
[pname
]
275 default
= getattr(param
, 'default', '')
278 print " default: %s" % default
279 print " desc: %s" % param
.desc
286 # setting verbose and quiet at the same time doesn't make sense
287 if options
.verbose
> 0 and options
.quiet
> 0:
290 verbose
= options
.verbose
- options
.quiet
292 print "gem5 Simulator System. http://gem5.org"
293 print brief_copyright
296 print "gem5 compiled %s" % defines
.compileDate
;
298 print "gem5 started %s" % \
299 datetime
.datetime
.now().strftime("%b %e %Y %X")
300 print "gem5 executing on %s, pid %d" % \
301 (socket
.gethostname(), os
.getpid())
303 # in Python 3 pipes.quote() is moved to shlex.quote()
305 print "command line:", " ".join(map(pipes
.quote
, sys
.argv
))
308 # check to make sure we can find the listed script
309 if not arguments
or not os
.path
.isfile(arguments
[0]):
310 if arguments
and not os
.path
.isfile(arguments
[0]):
311 print "Script %s not found" % arguments
[0]
315 # tell C++ about output directory
316 core
.setOutputDir(options
.outdir
)
318 # update the system path with elements from the -p option
319 sys
.path
[0:0] = options
.path
322 stats
.initText(options
.stats_file
)
324 # set debugging options
325 debug
.setRemoteGDBPort(options
.remote_gdb_port
)
326 for when
in options
.debug_break
:
327 debug
.schedBreak(int(when
))
329 if options
.debug_flags
:
334 for flag
in options
.debug_flags
:
336 if flag
.startswith('-'):
340 if flag
not in debug
.flags
:
341 print >>sys
.stderr
, "invalid debug flag '%s'" % flag
345 debug
.flags
[flag
].disable()
347 debug
.flags
[flag
].enable()
349 if options
.debug_start
:
351 e
= event
.create(trace
.enable
, event
.Event
.Debug_Enable_Pri
)
352 event
.mainq
.schedule(e
, options
.debug_start
)
356 if options
.debug_end
:
358 e
= event
.create(trace
.disable
, event
.Event
.Debug_Enable_Pri
)
359 event
.mainq
.schedule(e
, options
.debug_end
)
361 trace
.output(options
.debug_file
)
363 for ignore
in options
.debug_ignore
:
368 sys
.path
= [ os
.path
.dirname(sys
.argv
[0]) ] + sys
.path
370 filename
= sys
.argv
[0]
371 filedata
= file(filename
, 'r').read()
372 filecode
= compile(filedata
, filename
, 'exec')
373 scope
= { '__file__' : filename
,
374 '__name__' : '__m5_main__' }
376 # we want readline if we're doing anything interactive
377 if options
.interactive
or options
.pdb
:
378 exec "import readline" in scope
380 # if pdb was requested, execfile the thing under pdb, otherwise,
381 # just do the execfile normally
388 pdb
.run(filecode
, scope
)
390 print "The program exited via sys.exit(). Exit status: ",
391 print sys
.exc_info()[1]
393 traceback
.print_exc()
394 print "Uncaught exception. Entering post mortem debugging"
395 t
= sys
.exc_info()[2]
396 while t
.tb_next
is not None:
398 pdb
.interaction(t
.tb_frame
,t
)
400 exec filecode
in scope
402 # once the script is done
403 if options
.interactive
:
406 if __name__
== '__main__':
407 from pprint
import pprint
409 options
, arguments
= parse_options()
412 pprint(options
, indent
=4)
416 pprint(arguments
, indent
=4)