main.py revision 5470
14981SN/A# Copyright (c) 2005 The Regents of The University of Michigan
24981SN/A# All rights reserved.
34981SN/A#
44981SN/A# Redistribution and use in source and binary forms, with or without
54981SN/A# modification, are permitted provided that the following conditions are
64981SN/A# met: redistributions of source code must retain the above copyright
74981SN/A# notice, this list of conditions and the following disclaimer;
84981SN/A# redistributions in binary form must reproduce the above copyright
94981SN/A# notice, this list of conditions and the following disclaimer in the
104981SN/A# documentation and/or other materials provided with the distribution;
114981SN/A# neither the name of the copyright holders nor the names of its
124981SN/A# contributors may be used to endorse or promote products derived from
134981SN/A# this software without specific prior written permission.
144981SN/A#
154981SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164981SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174981SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
184981SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
194981SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
204981SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
214981SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224981SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234981SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244981SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254981SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264981SN/A#
274981SN/A# Authors: Nathan Binkert
284981SN/A
294981SN/Aimport code
304981SN/Aimport datetime
314981SN/Aimport os
324981SN/Aimport socket
334981SN/Aimport sys
344981SN/A
354981SN/Afrom util import attrdict
3611263Sandreas.sandberg@arm.comimport defines
3711263Sandreas.sandberg@arm.comfrom options import OptionParser
384981SN/Aimport traceflags
395485SN/A
4011260SN/A__all__ = [ 'options', 'arguments', 'main' ]
4111263Sandreas.sandberg@arm.com
424981SN/Adef print_list(items, indent=4):
434981SN/A    line = ' ' * indent
444981SN/A    for i,item in enumerate(items):
454981SN/A        if len(line) + len(item) > 76:
464981SN/A            print line
4713784Sgabeblack@google.com            line = ' ' * indent
484981SN/A
494981SN/A        if i < len(items) - 1:
504981SN/A            line += '%s, ' % item
514981SN/A        else:
529807SN/A            line += item
534981SN/A            print line
544981SN/A
554981SN/Ausage="%prog [m5 options] script.py [script options]"
564981SN/Aversion="%prog 2.0"
574981SN/Abrief_copyright='''
584981SN/ACopyright (c) 2001-2008
594981SN/AThe Regents of The University of Michigan
604981SN/AAll Rights Reserved
614981SN/A'''
625485SN/A
635485SN/Aoptions = OptionParser(usage=usage, version=version,
645485SN/A                       description=brief_copyright)
655999SN/Aadd_option = options.add_option
665999SN/Aset_group = options.set_group
675999SN/Ausage = options.usage
685999SN/A
695999SN/A# Help options
705999SN/Aadd_option('-A', "--authors", action="store_true", default=False,
715999SN/A    help="Show author information")
725999SN/Aadd_option('-B', "--build-info", action="store_true", default=False,
735999SN/A    help="Show build information")
745999SN/Aadd_option('-C', "--copyright", action="store_true", default=False,
755999SN/A    help="Show full copyright information")
765999SN/Aadd_option('-R', "--readme", action="store_true", default=False,
775999SN/A    help="Show the readme")
785999SN/Aadd_option('-N', "--release-notes", action="store_true", default=False,
795485SN/A    help="Show the release notes")
805485SN/A
815485SN/A# Options for configuring the base simulator
825485SN/Aadd_option('-d', "--outdir", metavar="DIR", default=".",
835485SN/A    help="Set the output directory to DIR [Default: %default]")
845485SN/Aadd_option('-i', "--interactive", action="store_true", default=False,
855485SN/A    help="Invoke the interactive interpreter after running the script")
865485SN/Aadd_option("--pdb", action="store_true", default=False,
875999SN/A    help="Invoke the python debugger before running the script")
885485SN/Aadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
895999SN/A    help="Prepend PATH to the system path when invoking the script")
905999SN/Aadd_option('-q', "--quiet", action="count", default=0,
915485SN/A    help="Reduce verbosity")
925999SN/Aadd_option('-v', "--verbose", action="count", default=0,
935999SN/A    help="Increase verbosity")
945485SN/A
955999SN/A# Statistics options
965999SN/Aset_group("Statistics Options")
975485SN/Aadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
985999SN/A    help="Sets the output file for statistics [Default: %default]")
995999SN/A
1005485SN/A# Debugging options
1015999SN/Aset_group("Debugging Options")
1025999SN/Aadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1035485SN/A    help="Cycle to create a breakpoint")
1045999SN/A
1055999SN/A# Tracing options
1065485SN/Aset_group("Trace Options")
1075999SN/Aadd_option("--trace-help", action='store_true',
1085999SN/A    help="Print help on trace flags")
1095485SN/Aadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1105999SN/A    help="Sets the flags for tracing (-FLAG disables a flag)")
1115485SN/Aadd_option("--trace-start", metavar="TIME", type='int',
1125999SN/A    help="Start tracing at TIME (must be in ticks)")
1135999SN/Aadd_option("--trace-file", metavar="FILE", default="cout",
1144981SN/A    help="Sets the output file for tracing [Default: %default]")
1154981SN/Aadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1169339SN/A    help="Ignore EXPR sim objects")
1179339SN/A
1189339SN/Adef main():
1199339SN/A    import defines
1209339SN/A    import event
1219339SN/A    import info
1229339SN/A    import internal
12311990Sandreas.sandberg@arm.com
12411990Sandreas.sandberg@arm.com    arguments = options.parse_args()
1259339SN/A
1269339SN/A    done = False
1279339SN/A
1289339SN/A    if options.build_info:
1299339SN/A        done = True
1309339SN/A        print 'Build information:'
1319339SN/A        print
1329339SN/A        print 'compiled %s' % internal.core.cvar.compileDate;
1339339SN/A        print 'started %s' % datetime.datetime.now().ctime()
1349339SN/A        print 'executing on %s' % socket.gethostname()
1359339SN/A        print 'build options:'
1369339SN/A        keys = defines.m5_build_env.keys()
1379339SN/A        keys.sort()
1389339SN/A        for key in keys:
1399339SN/A            val = defines.m5_build_env[key]
1409339SN/A            print '    %s = %s' % (key, val)
14111263Sandreas.sandberg@arm.com        print
14211263Sandreas.sandberg@arm.com
143    if options.copyright:
144        done = True
145        print info.LICENSE
146        print
147
148    if options.authors:
149        done = True
150        print 'Author information:'
151        print
152        print info.AUTHORS
153        print
154
155    if options.readme:
156        done = True
157        print 'Readme:'
158        print
159        print info.README
160        print
161
162    if options.release_notes:
163        done = True
164        print 'Release Notes:'
165        print
166        print info.RELEASE_NOTES
167        print
168
169    if options.trace_help:
170        done = True
171        print "Base Flags:"
172        print_list(traceflags.baseFlags, indent=4)
173        print
174        print "Compound Flags:"
175        for flag in traceflags.compoundFlags:
176            if flag == 'All':
177                continue
178            print "    %s:" % flag
179            print_list(traceflags.compoundFlagMap[flag], indent=8)
180            print
181
182    if done:
183        sys.exit(0)
184
185    # setting verbose and quiet at the same time doesn't make sense
186    if options.verbose > 0 and options.quiet > 0:
187        options.usage(2)
188
189    verbose = options.verbose - options.quiet
190    if options.verbose >= 0:
191        print "M5 Simulator System"
192        print brief_copyright
193        print
194        print "M5 compiled %s" % internal.core.cvar.compileDate;
195        print "M5 started %s" % datetime.datetime.now().ctime()
196        print "M5 executing on %s" % socket.gethostname()
197
198        print "M5 revision %s" % internal.core.cvar.hgRev
199        print "M5 commit date %s" % internal.core.cvar.hgDate
200
201        print "command line:",
202        for argv in sys.argv:
203            print argv,
204        print
205
206    # check to make sure we can find the listed script
207    if not arguments or not os.path.isfile(arguments[0]):
208        if arguments and not os.path.isfile(arguments[0]):
209            print "Script %s not found" % arguments[0]
210
211        options.usage(2)
212
213    # tell C++ about output directory
214    internal.core.setOutputDir(options.outdir)
215
216    # update the system path with elements from the -p option
217    sys.path[0:0] = options.path
218
219    import objects
220
221    # set stats options
222    internal.stats.initText(options.stats_file)
223
224    # set debugging options
225    for when in options.debug_break:
226        internal.debug.schedBreakCycle(int(when))
227
228    on_flags = []
229    off_flags = []
230    for flag in options.trace_flags:
231        off = False
232        if flag.startswith('-'):
233            flag = flag[1:]
234            off = True
235        if flag not in traceflags.allFlags:
236            print >>sys.stderr, "invalid trace flag '%s'" % flag
237            sys.exit(1)
238
239        if off:
240            off_flags.append(flag)
241        else:
242            on_flags.append(flag)
243
244    for flag in on_flags:
245        internal.trace.set(flag)
246
247    for flag in off_flags:
248        internal.trace.clear(flag)
249
250    if options.trace_start:
251        def enable_trace():
252            internal.trace.cvar.enabled = True
253        event.create(enable_trace, int(options.trace_start))
254    else:
255        internal.trace.cvar.enabled = True
256
257    internal.trace.output(options.trace_file)
258
259    for ignore in options.trace_ignore:
260        internal.trace.ignore(ignore)
261
262    sys.argv = arguments
263    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
264
265    scope = { '__file__' : sys.argv[0],
266              '__name__' : '__m5_main__' }
267
268    # we want readline if we're doing anything interactive
269    if options.interactive or options.pdb:
270        exec "import readline" in scope
271
272    # if pdb was requested, execfile the thing under pdb, otherwise,
273    # just do the execfile normally
274    if options.pdb:
275        from pdb import Pdb
276        debugger = Pdb()
277        debugger.run('execfile("%s")' % sys.argv[0], scope)
278    else:
279        execfile(sys.argv[0], scope)
280
281    # once the script is done
282    if options.interactive:
283        interact = code.InteractiveConsole(scope)
284        interact.interact("M5 Interactive Console")
285
286if __name__ == '__main__':
287    from pprint import pprint
288
289    parse_args()
290
291    print 'opts:'
292    pprint(options, indent=4)
293    print
294
295    print 'args:'
296    pprint(arguments, indent=4)
297