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 from util
import attrdict
37 from options
import OptionParser
39 __all__
= [ 'options', 'arguments', 'main' ]
41 def print_list(items
, indent
=4):
43 for i
,item
in enumerate(items
):
44 if len(line
) + len(item
) > 76:
48 if i
< len(items
) - 1:
54 usage
="%prog [m5 options] script.py [script options]"
57 Copyright (c) 2001-2008
58 The Regents of The University of Michigan
62 options
= OptionParser(usage
=usage
, version
=version
,
63 description
=brief_copyright
)
64 add_option
= options
.add_option
65 set_group
= options
.set_group
69 add_option('-A', "--authors", action
="store_true", default
=False,
70 help="Show author information")
71 add_option('-B', "--build-info", action
="store_true", default
=False,
72 help="Show build information")
73 add_option('-C', "--copyright", action
="store_true", default
=False,
74 help="Show full copyright information")
75 add_option('-R', "--readme", action
="store_true", default
=False,
76 help="Show the readme")
77 add_option('-N', "--release-notes", action
="store_true", default
=False,
78 help="Show the release notes")
80 # Options for configuring the base simulator
81 add_option('-d', "--outdir", metavar
="DIR", default
=".",
82 help="Set the output directory to DIR [Default: %default]")
83 add_option('-r', "--redirect-stdout", action
="store_true", default
=False,
84 help="Redirect stdout (& stderr, without -e) to file")
85 add_option('-e', "--redirect-stderr", action
="store_true", default
=False,
86 help="Redirect stderr to file")
87 add_option("--stdout-file", metavar
="FILE", default
="simout",
88 help="Filename for -r redirection [Default: %default]")
89 add_option("--stderr-file", metavar
="FILE", default
="simerr",
90 help="Filename for -e redirection [Default: %default]")
91 add_option('-i', "--interactive", action
="store_true", default
=False,
92 help="Invoke the interactive interpreter after running the script")
93 add_option("--pdb", action
="store_true", default
=False,
94 help="Invoke the python debugger before running the script")
95 add_option('-p', "--path", metavar
="PATH[:PATH]", action
='append', split
=':',
96 help="Prepend PATH to the system path when invoking the script")
97 add_option('-q', "--quiet", action
="count", default
=0,
98 help="Reduce verbosity")
99 add_option('-v', "--verbose", action
="count", default
=0,
100 help="Increase verbosity")
103 set_group("Statistics Options")
104 add_option("--stats-file", metavar
="FILE", default
="m5stats.txt",
105 help="Sets the output file for statistics [Default: %default]")
108 set_group("Debugging Options")
109 add_option("--debug-break", metavar
="TIME[,TIME]", action
='append', split
=',',
110 help="Cycle to create a breakpoint")
111 add_option("--remote-gdb-port", type='int', default
=7000,
112 help="Remote gdb base port")
115 set_group("Trace Options")
116 add_option("--trace-help", action
='store_true',
117 help="Print help on trace flags")
118 add_option("--trace-flags", metavar
="FLAG[,FLAG]", action
='append', split
=',',
119 help="Sets the flags for tracing (-FLAG disables a flag)")
120 add_option("--trace-start", metavar
="TIME", type='int',
121 help="Start tracing at TIME (must be in ticks)")
122 add_option("--trace-file", metavar
="FILE", default
="cout",
123 help="Sets the output file for tracing [Default: %default]")
124 add_option("--trace-ignore", metavar
="EXPR", action
='append', split
=':',
125 help="Ignore EXPR sim objects")
128 set_group("Help Options")
129 add_option("--list-sim-objects", action
='store_true', default
=False,
130 help="List all built-in SimObjects, their parameters and default values")
137 # load the options.py config file to allow people to set their own
139 options_file
= config
.get('options.py')
141 scope
= { 'options' : options
}
142 execfile(options_file
, scope
)
144 arguments
= options
.parse_args()
146 if not os
.path
.isdir(options
.outdir
):
147 os
.makedirs(options
.outdir
)
149 # These filenames are used only if the redirect_std* options are set
150 stdout_file
= os
.path
.join(options
.outdir
, options
.stdout_file
)
151 stderr_file
= os
.path
.join(options
.outdir
, options
.stderr_file
)
153 # Print redirection notices here before doing any redirection
154 if options
.redirect_stdout
and not options
.redirect_stderr
:
155 print "Redirecting stdout and stderr to", stdout_file
157 if options
.redirect_stdout
:
158 print "Redirecting stdout to", stdout_file
159 if options
.redirect_stderr
:
160 print "Redirecting stderr to", stderr_file
162 # Now redirect stdout/stderr as desired
163 if options
.redirect_stdout
:
164 redir_fd
= os
.open(stdout_file
, os
. O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
165 os
.dup2(redir_fd
, sys
.stdout
.fileno())
166 if not options
.redirect_stderr
:
167 os
.dup2(redir_fd
, sys
.stderr
.fileno())
169 if options
.redirect_stderr
:
170 redir_fd
= os
.open(stderr_file
, os
. O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
171 os
.dup2(redir_fd
, sys
.stderr
.fileno())
175 if options
.build_info
:
179 print 'Build information:'
181 print 'compiled %s' % internal
.core
.cvar
.compileDate
;
182 print "revision %s" % internal
.core
.cvar
.hgRev
183 print "commit date %s" % internal
.core
.cvar
.hgDate
184 print 'build options:'
185 keys
= defines
.m5_build_env
.keys()
188 val
= defines
.m5_build_env
[key
]
189 print ' %s = %s' % (key
, val
)
192 if options
.copyright
:
199 print 'Author information:'
211 if options
.release_notes
:
213 print 'Release Notes:'
215 print info
.RELEASE_NOTES
218 if options
.trace_help
:
223 print_list(traceflags
.baseFlags
, indent
=4)
225 print "Compound Flags:"
226 for flag
in traceflags
.compoundFlags
:
230 print_list(traceflags
.compoundFlagMap
[flag
], indent
=8)
233 if options
.list_sim_objects
:
237 objects
= SimObject
.allClasses
.keys()
240 obj
= SimObject
.allClasses
[name
]
242 params
= obj
._params
.keys()
245 param
= obj
._params
[pname
]
246 default
= getattr(param
, 'default', '')
249 print " default: %s" % default
250 print " desc: %s" % param
.desc
257 # setting verbose and quiet at the same time doesn't make sense
258 if options
.verbose
> 0 and options
.quiet
> 0:
261 verbose
= options
.verbose
- options
.quiet
262 if options
.verbose
>= 0:
263 print "M5 Simulator System"
264 print brief_copyright
266 print "M5 compiled %s" % internal
.core
.cvar
.compileDate
;
267 print "M5 revision %s" % internal
.core
.cvar
.hgRev
268 print "M5 commit date %s" % internal
.core
.cvar
.hgDate
270 print "M5 started %s" % datetime
.datetime
.now().strftime("%b %e %Y %X")
271 print "M5 executing on %s" % socket
.gethostname()
273 print "command line:",
274 for argv
in sys
.argv
:
278 # check to make sure we can find the listed script
279 if not arguments
or not os
.path
.isfile(arguments
[0]):
280 if arguments
and not os
.path
.isfile(arguments
[0]):
281 print "Script %s not found" % arguments
[0]
285 # tell C++ about output directory
286 internal
.core
.setOutputDir(options
.outdir
)
288 # update the system path with elements from the -p option
289 sys
.path
[0:0] = options
.path
294 internal
.stats
.initText(options
.stats_file
)
296 # set debugging options
297 internal
.debug
.setRemoteGDBPort(options
.remote_gdb_port
)
298 for when
in options
.debug_break
:
299 internal
.debug
.schedBreakCycle(int(when
))
301 if options
.trace_flags
:
306 for flag
in options
.trace_flags
:
308 if flag
.startswith('-'):
311 if flag
not in traceflags
.allFlags
:
312 print >>sys
.stderr
, "invalid trace flag '%s'" % flag
316 off_flags
.append(flag
)
318 on_flags
.append(flag
)
320 for flag
in on_flags
:
321 internal
.trace
.set(flag
)
323 for flag
in off_flags
:
324 internal
.trace
.clear(flag
)
326 if options
.trace_start
:
328 internal
.trace
.cvar
.enabled
= True
330 e
= event
.create(enable_trace
)
331 event
.mainq
.schedule(e
, options
.trace_start
)
333 internal
.trace
.cvar
.enabled
= True
335 internal
.trace
.output(options
.trace_file
)
337 for ignore
in options
.trace_ignore
:
338 internal
.trace
.ignore(ignore
)
341 sys
.path
= [ os
.path
.dirname(sys
.argv
[0]) ] + sys
.path
343 filename
= sys
.argv
[0]
344 filedata
= file(filename
, 'r').read()
345 filecode
= compile(filedata
, filename
, 'exec')
346 scope
= { '__file__' : filename
,
347 '__name__' : '__m5_main__' }
349 # we want readline if we're doing anything interactive
350 if options
.interactive
or options
.pdb
:
351 exec "import readline" in scope
353 # if pdb was requested, execfile the thing under pdb, otherwise,
354 # just do the execfile normally
361 pdb
.run(filecode
, scope
)
363 print "The program exited via sys.exit(). Exit status: ",
364 print sys
.exc_info()[1]
366 traceback
.print_exc()
367 print "Uncaught exception. Entering post mortem debugging"
368 t
= sys
.exc_info()[2]
369 while t
.tb_next
is not None:
371 pdb
.interaction(t
.tb_frame
,t
)
373 exec filecode
in scope
375 # once the script is done
376 if options
.interactive
:
377 interact
= code
.InteractiveConsole(scope
)
378 interact
.interact("M5 Interactive Console")
380 if __name__
== '__main__':
381 from pprint
import pprint
383 # load the options.py config file to allow people to set their own
385 options_file
= config
.get('options.py')
387 scope
= { 'options' : options
}
388 execfile(options_file
, scope
)
390 arguments
= options
.parse_args()
393 pprint(options
, indent
=4)
397 pprint(arguments
, indent
=4)