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
36 from attrdict
import attrdict
40 __all__
= [ 'options', 'arguments', 'main' ]
42 usage
="%prog [m5 options] script.py [script options]"
45 Copyright (c) 2001-2006
46 The Regents of The University of Michigan
50 def print_list(items
, indent
=4):
52 for i
,item
in enumerate(items
):
53 if len(line
) + len(item
) > 76:
57 if i
< len(items
) - 1:
63 # there's only one option parsing done, so make it global and add some
64 # helper functions to make it work well.
65 parser
= optparse
.OptionParser(usage
=usage
, version
=version
,
66 description
=brief_copyright
,
67 formatter
=optparse
.TitledHelpFormatter())
68 parser
.disable_interspersed_args()
70 # current option group
73 def set_group(*args
, **kwargs
):
74 '''set the current option group'''
76 if not args
and not kwargs
:
79 group
= parser
.add_option_group(*args
, **kwargs
)
81 class splitter(object):
82 def __init__(self
, split
):
84 def __call__(self
, option
, opt_str
, value
, parser
):
85 getattr(parser
.values
, option
.dest
).extend(value
.split(self
.split
))
87 def add_option(*args
, **kwargs
):
88 '''add an option to the current option group, or global none set'''
90 # if action=split, but allows the option arguments
91 # themselves to be lists separated by the split variable'''
93 if kwargs
.get('action', None) == 'append' and 'split' in kwargs
:
94 split
= kwargs
.pop('split')
95 kwargs
['default'] = []
96 kwargs
['type'] = 'string'
97 kwargs
['action'] = 'callback'
98 kwargs
['callback'] = splitter(split
)
101 return group
.add_option(*args
, **kwargs
)
103 return parser
.add_option(*args
, **kwargs
)
105 def bool_option(name
, default
, help):
106 '''add a boolean option called --name and --no-name.
107 Display help depending on which is the default'''
109 tname
= '--%s' % name
110 fname
= '--no-%s' % name
111 dest
= name
.replace('-', '_')
113 thelp
= optparse
.SUPPRESS_HELP
117 fhelp
= optparse
.SUPPRESS_HELP
119 add_option(tname
, action
="store_true", default
=default
, help=thelp
)
120 add_option(fname
, action
="store_false", dest
=dest
, help=fhelp
)
123 add_option('-A', "--authors", action
="store_true", default
=False,
124 help="Show author information")
125 add_option('-B', "--build-info", action
="store_true", default
=False,
126 help="Show build information")
127 add_option('-C', "--copyright", action
="store_true", default
=False,
128 help="Show full copyright information")
129 add_option('-R', "--readme", action
="store_true", default
=False,
130 help="Show the readme")
131 add_option('-N', "--release-notes", action
="store_true", default
=False,
132 help="Show the release notes")
134 # Options for configuring the base simulator
135 add_option('-d', "--outdir", metavar
="DIR", default
=".",
136 help="Set the output directory to DIR [Default: %default]")
137 add_option('-i', "--interactive", action
="store_true", default
=False,
138 help="Invoke the interactive interpreter after running the script")
139 add_option("--pdb", action
="store_true", default
=False,
140 help="Invoke the python debugger before running the script")
141 add_option('-p', "--path", metavar
="PATH[:PATH]", action
='append', split
=':',
142 help="Prepend PATH to the system path when invoking the script")
143 add_option('-q', "--quiet", action
="count", default
=0,
144 help="Reduce verbosity")
145 add_option('-v', "--verbose", action
="count", default
=0,
146 help="Increase verbosity")
149 set_group("Statistics Options")
150 add_option("--stats-file", metavar
="FILE", default
="m5stats.txt",
151 help="Sets the output file for statistics [Default: %default]")
154 set_group("Debugging Options")
155 add_option("--debug-break", metavar
="TIME[,TIME]", action
='append', split
=',',
156 help="Cycle to create a breakpoint")
159 set_group("Trace Options")
160 add_option("--trace-help", action
='store_true',
161 help="Print help on trace flags")
162 add_option("--trace-flags", metavar
="FLAG[,FLAG]", action
='append', split
=',',
163 help="Sets the flags for tracing (-FLAG disables a flag)")
164 add_option("--trace-start", metavar
="TIME", type='int',
165 help="Start tracing at TIME (must be in ticks)")
166 add_option("--trace-file", metavar
="FILE", default
="cout",
167 help="Sets the output file for tracing [Default: %default]")
168 add_option("--trace-ignore", metavar
="EXPR", action
='append', split
=':',
169 help="Ignore EXPR sim objects")
174 def usage(exitcode
=None):
176 if exitcode
is not None:
180 _opts
,args
= parser
.parse_args()
181 opts
= attrdict(_opts
.__dict
__)
183 # setting verbose and quiet at the same time doesn't make sense
184 if opts
.verbose
> 0 and opts
.quiet
> 0:
187 # store the verbosity in a single variable. 0 is default,
188 # negative numbers represent quiet and positive values indicate verbose
189 opts
.verbose
-= opts
.quiet
194 arguments
.extend(args
)
207 if options
.build_info
:
209 print 'Build information:'
211 print 'compiled %s' % internal
.core
.cvar
.compileDate
;
212 print 'started %s' % datetime
.datetime
.now().ctime()
213 print 'executing on %s' % socket
.gethostname()
214 print 'build options:'
215 keys
= defines
.m5_build_env
.keys()
218 val
= defines
.m5_build_env
[key
]
219 print ' %s = %s' % (key
, val
)
222 if options
.copyright
:
229 print 'Author information:'
241 if options
.release_notes
:
243 print 'Release Notes:'
245 print info
.RELEASE_NOTES
248 if options
.trace_help
:
251 print_list(traceflags
.baseFlags
, indent
=4)
253 print "Compound Flags:"
254 for flag
in traceflags
.compoundFlags
:
258 print_list(traceflags
.compoundFlagMap
[flag
], indent
=8)
264 if options
.verbose
>= 0:
265 print "M5 Simulator System"
266 print brief_copyright
268 print "M5 compiled %s" % internal
.core
.cvar
.compileDate
;
269 print "M5 started %s" % datetime
.datetime
.now().ctime()
270 print "M5 executing on %s" % socket
.gethostname()
271 print "command line:",
272 for argv
in sys
.argv
:
276 # check to make sure we can find the listed script
277 if not arguments
or not os
.path
.isfile(arguments
[0]):
278 if arguments
and not os
.path
.isfile(arguments
[0]):
279 print "Script %s not found" % arguments
[0]
283 # tell C++ about output directory
284 internal
.core
.setOutputDir(options
.outdir
)
286 # update the system path with elements from the -p option
287 sys
.path
[0:0] = options
.path
292 internal
.stats
.initText(options
.stats_file
)
294 # set debugging options
295 for when
in options
.debug_break
:
296 internal
.debug
.schedBreakCycle(int(when
))
300 for flag
in options
.trace_flags
:
302 if flag
.startswith('-'):
305 if flag
not in traceflags
.allFlags
:
306 print >>sys
.stderr
, "invalid trace flag '%s'" % flag
310 off_flags
.append(flag
)
312 on_flags
.append(flag
)
314 for flag
in on_flags
:
315 internal
.trace
.set(flag
)
317 for flag
in off_flags
:
318 internal
.trace
.clear(flag
)
320 if options
.trace_start
:
322 internal
.trace
.cvar
.enabled
= True
323 event
.create(enable_trace
, int(options
.trace_start
))
325 internal
.trace
.cvar
.enabled
= True
327 internal
.trace
.output(options
.trace_file
)
329 for ignore
in options
.trace_ignore
:
330 internal
.trace
.ignore(ignore
)
333 sys
.path
= [ os
.path
.dirname(sys
.argv
[0]) ] + sys
.path
335 scope
= { '__file__' : sys
.argv
[0],
336 '__name__' : '__m5_main__' }
338 # we want readline if we're doing anything interactive
339 if options
.interactive
or options
.pdb
:
340 exec "import readline" in scope
342 # if pdb was requested, execfile the thing under pdb, otherwise,
343 # just do the execfile normally
347 debugger
.run('execfile("%s")' % sys
.argv
[0], scope
)
349 execfile(sys
.argv
[0], scope
)
351 # once the script is done
352 if options
.interactive
:
353 interact
= code
.InteractiveConsole(scope
)
354 interact
.interact("M5 Interactive Console")
356 if __name__
== '__main__':
357 from pprint
import pprint
362 pprint(options
, indent
=4)
366 pprint(arguments
, indent
=4)