main.py revision 3127:c56885d6dc6d
112641Sgiacomo.travaglini@arm.com# Copyright (c) 2005 The Regents of The University of Michigan 212641Sgiacomo.travaglini@arm.com# All rights reserved. 312641Sgiacomo.travaglini@arm.com# 412641Sgiacomo.travaglini@arm.com# Redistribution and use in source and binary forms, with or without 512641Sgiacomo.travaglini@arm.com# modification, are permitted provided that the following conditions are 612641Sgiacomo.travaglini@arm.com# met: redistributions of source code must retain the above copyright 712641Sgiacomo.travaglini@arm.com# notice, this list of conditions and the following disclaimer; 812641Sgiacomo.travaglini@arm.com# redistributions in binary form must reproduce the above copyright 912641Sgiacomo.travaglini@arm.com# notice, this list of conditions and the following disclaimer in the 1012641Sgiacomo.travaglini@arm.com# documentation and/or other materials provided with the distribution; 1112641Sgiacomo.travaglini@arm.com# neither the name of the copyright holders nor the names of its 1212641Sgiacomo.travaglini@arm.com# contributors may be used to endorse or promote products derived from 1312641Sgiacomo.travaglini@arm.com# this software without specific prior written permission. 1412641Sgiacomo.travaglini@arm.com# 1512641Sgiacomo.travaglini@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612641Sgiacomo.travaglini@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712641Sgiacomo.travaglini@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812641Sgiacomo.travaglini@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912641Sgiacomo.travaglini@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012641Sgiacomo.travaglini@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112641Sgiacomo.travaglini@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212641Sgiacomo.travaglini@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312641Sgiacomo.travaglini@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412641Sgiacomo.travaglini@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512641Sgiacomo.travaglini@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612641Sgiacomo.travaglini@arm.com# 2712641Sgiacomo.travaglini@arm.com# Authors: Nathan Binkert 2812641Sgiacomo.travaglini@arm.com 2912641Sgiacomo.travaglini@arm.comimport code, optparse, os, socket, sys 3012641Sgiacomo.travaglini@arm.comfrom datetime import datetime 3112641Sgiacomo.travaglini@arm.comfrom attrdict import attrdict 3212641Sgiacomo.travaglini@arm.com 3312641Sgiacomo.travaglini@arm.comtry: 3412641Sgiacomo.travaglini@arm.com import info 3512641Sgiacomo.travaglini@arm.comexcept ImportError: 3612641Sgiacomo.travaglini@arm.com info = None 3712641Sgiacomo.travaglini@arm.com 3812641Sgiacomo.travaglini@arm.com__all__ = [ 'options', 'arguments', 'main' ] 3912641Sgiacomo.travaglini@arm.com 4012641Sgiacomo.travaglini@arm.comusage="%prog [m5 options] script.py [script options]" 4113665Sandreas.sandberg@arm.comversion="%prog 2.0" 4212641Sgiacomo.travaglini@arm.combrief_copyright=''' 4312641Sgiacomo.travaglini@arm.comCopyright (c) 2001-2006 4412641Sgiacomo.travaglini@arm.comThe Regents of The University of Michigan 4512641Sgiacomo.travaglini@arm.comAll Rights Reserved 4612641Sgiacomo.travaglini@arm.com''' 4712641Sgiacomo.travaglini@arm.com 4812641Sgiacomo.travaglini@arm.com# there's only one option parsing done, so make it global and add some 4912641Sgiacomo.travaglini@arm.com# helper functions to make it work well. 5012641Sgiacomo.travaglini@arm.comparser = optparse.OptionParser(usage=usage, version=version, 5112641Sgiacomo.travaglini@arm.com description=brief_copyright, 5212641Sgiacomo.travaglini@arm.com formatter=optparse.TitledHelpFormatter()) 5312641Sgiacomo.travaglini@arm.comparser.disable_interspersed_args() 5412641Sgiacomo.travaglini@arm.com 5512641Sgiacomo.travaglini@arm.com# current option group 5612641Sgiacomo.travaglini@arm.comgroup = None 5712641Sgiacomo.travaglini@arm.com 5812641Sgiacomo.travaglini@arm.comdef set_group(*args, **kwargs): 5912641Sgiacomo.travaglini@arm.com '''set the current option group''' 6012641Sgiacomo.travaglini@arm.com global group 6112641Sgiacomo.travaglini@arm.com if not args and not kwargs: 6212641Sgiacomo.travaglini@arm.com group = None 6312641Sgiacomo.travaglini@arm.com else: 6412641Sgiacomo.travaglini@arm.com group = parser.add_option_group(*args, **kwargs) 6512641Sgiacomo.travaglini@arm.com 6612641Sgiacomo.travaglini@arm.comclass splitter(object): 6712642Sgiacomo.travaglini@arm.com def __init__(self, split): 6812642Sgiacomo.travaglini@arm.com self.split = split 6912642Sgiacomo.travaglini@arm.com def __call__(self, option, opt_str, value, parser): 7012642Sgiacomo.travaglini@arm.com getattr(parser.values, option.dest).extend(value.split(self.split)) 7112642Sgiacomo.travaglini@arm.com 7212642Sgiacomo.travaglini@arm.comdef add_option(*args, **kwargs): 7312642Sgiacomo.travaglini@arm.com '''add an option to the current option group, or global none set''' 7412642Sgiacomo.travaglini@arm.com 7512642Sgiacomo.travaglini@arm.com # if action=split, but allows the option arguments 7612642Sgiacomo.travaglini@arm.com # themselves to be lists separated by the split variable''' 7712642Sgiacomo.travaglini@arm.com 78 if kwargs.get('action', None) == 'append' and 'split' in kwargs: 79 split = kwargs.pop('split') 80 kwargs['default'] = [] 81 kwargs['type'] = 'string' 82 kwargs['action'] = 'callback' 83 kwargs['callback'] = splitter(split) 84 85 if group: 86 return group.add_option(*args, **kwargs) 87 88 return parser.add_option(*args, **kwargs) 89 90def bool_option(name, default, help): 91 '''add a boolean option called --name and --no-name. 92 Display help depending on which is the default''' 93 94 tname = '--%s' % name 95 fname = '--no-%s' % name 96 dest = name.replace('-', '_') 97 if default: 98 thelp = optparse.SUPPRESS_HELP 99 fhelp = help 100 else: 101 thelp = help 102 fhelp = optparse.SUPPRESS_HELP 103 104 add_option(tname, action="store_true", default=default, help=thelp) 105 add_option(fname, action="store_false", dest=dest, help=fhelp) 106 107# Help options 108add_option('-A', "--authors", action="store_true", default=False, 109 help="Show author information") 110add_option('-C', "--copyright", action="store_true", default=False, 111 help="Show full copyright information") 112add_option('-R', "--readme", action="store_true", default=False, 113 help="Show the readme") 114add_option('-N', "--release-notes", action="store_true", default=False, 115 help="Show the release notes") 116 117# Options for configuring the base simulator 118add_option('-d', "--outdir", metavar="DIR", default=".", 119 help="Set the output directory to DIR [Default: %default]") 120add_option('-i', "--interactive", action="store_true", default=False, 121 help="Invoke the interactive interpreter after running the script") 122add_option("--pdb", action="store_true", default=False, 123 help="Invoke the python debugger before running the script") 124add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 125 help="Prepend PATH to the system path when invoking the script") 126add_option('-q', "--quiet", action="count", default=0, 127 help="Reduce verbosity") 128add_option('-v', "--verbose", action="count", default=0, 129 help="Increase verbosity") 130 131# Statistics options 132set_group("Statistics Options") 133add_option("--stats-file", metavar="FILE", default="m5stats.txt", 134 help="Sets the output file for statistics [Default: %default]") 135 136# Debugging options 137set_group("Debugging Options") 138add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 139 help="Cycle to create a breakpoint") 140 141# Tracing options 142set_group("Trace Options") 143add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 144 help="Sets the flags for tracing") 145add_option("--trace-start", metavar="TIME", default='0s', 146 help="Start tracing at TIME (must have units)") 147add_option("--trace-cycle", metavar="CYCLE", default='0', 148 help="Start tracing at CYCLE") 149add_option("--trace-file", metavar="FILE", default="cout", 150 help="Sets the output file for tracing [Default: %default]") 151add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0, 152 help="If SIZE is non-zero, turn on the circular buffer with SIZE lines") 153add_option("--no-trace-circlebuf", action="store_const", const=0, 154 dest='trace_circlebuf', help=optparse.SUPPRESS_HELP) 155bool_option("trace-dumponexit", default=False, 156 help="Dump trace buffer on exit") 157add_option("--trace-ignore", metavar="EXPR", action='append', split=':', 158 help="Ignore EXPR sim objects") 159 160# Execution Trace options 161set_group("Execution Trace Options") 162bool_option("speculative", default=True, 163 help="Don't capture speculative instructions") 164bool_option("print-cycle", default=True, 165 help="Don't print cycle numbers in trace output") 166bool_option("print-symbol", default=True, 167 help="Disable PC symbols in trace output") 168bool_option("print-opclass", default=True, 169 help="Don't print op class type in trace output") 170bool_option("print-thread", default=True, 171 help="Don't print thread number in trace output") 172bool_option("print-effaddr", default=True, 173 help="Don't print effective address in trace output") 174bool_option("print-data", default=True, 175 help="Don't print result data in trace output") 176bool_option("print-iregs", default=False, 177 help="Print fetch sequence numbers in trace output") 178bool_option("print-fetch-seq", default=False, 179 help="Print fetch sequence numbers in trace output") 180bool_option("print-cpseq", default=False, 181 help="Print correct path sequence numbers in trace output") 182#bool_option("print-reg-delta", default=False, 183# help="Print which registers changed to what in trace output") 184 185options = attrdict() 186arguments = [] 187 188def usage(exitcode=None): 189 parser.print_help() 190 if exitcode is not None: 191 sys.exit(exitcode) 192 193def parse_args(): 194 _opts,args = parser.parse_args() 195 opts = attrdict(_opts.__dict__) 196 197 # setting verbose and quiet at the same time doesn't make sense 198 if opts.verbose > 0 and opts.quiet > 0: 199 usage(2) 200 201 # store the verbosity in a single variable. 0 is default, 202 # negative numbers represent quiet and positive values indicate verbose 203 opts.verbose -= opts.quiet 204 205 del opts.quiet 206 207 options.update(opts) 208 arguments.extend(args) 209 return opts,args 210 211def main(): 212 import cc_main 213 214 parse_args() 215 216 done = False 217 if options.copyright: 218 done = True 219 print info.LICENSE 220 print 221 222 if options.authors: 223 done = True 224 print 'Author information:' 225 print 226 print info.AUTHORS 227 print 228 229 if options.readme: 230 done = True 231 print 'Readme:' 232 print 233 print info.README 234 print 235 236 if options.release_notes: 237 done = True 238 print 'Release Notes:' 239 print 240 print info.RELEASE_NOTES 241 print 242 243 if done: 244 sys.exit(0) 245 246 if options.verbose >= 0: 247 print "M5 Simulator System" 248 print brief_copyright 249 print 250 print "M5 compiled %s" % cc_main.cvar.compileDate; 251 print "M5 started %s" % datetime.now().ctime() 252 print "M5 executing on %s" % socket.gethostname() 253 print "command line:", 254 for argv in sys.argv: 255 print argv, 256 print 257 258 # check to make sure we can find the listed script 259 if not arguments or not os.path.isfile(arguments[0]): 260 if arguments and not os.path.isfile(arguments[0]): 261 print "Script %s not found" % arguments[0] 262 usage(2) 263 264 # tell C++ about output directory 265 cc_main.setOutputDir(options.outdir) 266 267 # update the system path with elements from the -p option 268 sys.path[0:0] = options.path 269 270 import objects 271 272 # set stats options 273 objects.Statistics.text_file = options.stats_file 274 275 # set debugging options 276 objects.Debug.break_cycles = options.debug_break 277 278 # set tracing options 279 objects.Trace.flags = options.trace_flags 280 objects.Trace.start = options.trace_start 281 objects.Trace.file = options.trace_file 282 objects.Trace.bufsize = options.trace_circlebuf 283 objects.Trace.dump_on_exit = options.trace_dumponexit 284 objects.Trace.ignore = options.trace_ignore 285 286 # set execution trace options 287 objects.ExecutionTrace.speculative = options.speculative 288 objects.ExecutionTrace.print_cycle = options.print_cycle 289 objects.ExecutionTrace.pc_symbol = options.print_symbol 290 objects.ExecutionTrace.print_opclass = options.print_opclass 291 objects.ExecutionTrace.print_thread = options.print_thread 292 objects.ExecutionTrace.print_effaddr = options.print_effaddr 293 objects.ExecutionTrace.print_data = options.print_data 294 objects.ExecutionTrace.print_iregs = options.print_iregs 295 objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq 296 objects.ExecutionTrace.print_cpseq = options.print_cpseq 297 #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta 298 299 sys.argv = arguments 300 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 301 302 scope = { '__file__' : sys.argv[0] } 303 304 # we want readline if we're doing anything interactive 305 if options.interactive or options.pdb: 306 exec("import readline", scope) 307 308 # if pdb was requested, execfile the thing under pdb, otherwise, 309 # just do the execfile normally 310 if options.pdb: 311 from pdb import Pdb 312 debugger = Pdb() 313 debugger.run('execfile("%s")' % sys.argv[0], scope) 314 else: 315 execfile(sys.argv[0], scope) 316 317 # once the script is done 318 if options.interactive: 319 interact = code.InteractiveConsole(scope) 320 interact.interact("M5 Interactive Console") 321 322if __name__ == '__main__': 323 from pprint import pprint 324 325 parse_args() 326 327 print 'opts:' 328 pprint(options, indent=4) 329 print 330 331 print 'args:' 332 pprint(arguments, indent=4) 333