main.cc revision 4167
1/* 2 * Copyright (c) 2000-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 */ 30 31#include <Python.h> 32#include <signal.h> 33 34#include <iostream> 35#include <string> 36 37#include "base/cprintf.hh" 38#include "base/misc.hh" 39#include "config/pythonhome.hh" 40#include "python/swig/init.hh" 41#include "sim/async.hh" 42#include "sim/host.hh" 43#include "sim/core.hh" 44 45using namespace std; 46 47/// Stats signal handler. 48void 49dumpStatsHandler(int sigtype) 50{ 51 async_event = true; 52 async_statdump = true; 53} 54 55void 56dumprstStatsHandler(int sigtype) 57{ 58 async_event = true; 59 async_statdump = true; 60 async_statreset = true; 61} 62 63/// Exit signal handler. 64void 65exitNowHandler(int sigtype) 66{ 67 async_event = true; 68 async_exit = true; 69} 70 71/// Abort signal handler. 72void 73abortHandler(int sigtype) 74{ 75 ccprintf(cerr, "Program aborted at cycle %d\n", curTick); 76} 77 78int 79main(int argc, char **argv) 80{ 81 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 82 signal(SIGTRAP, SIG_IGN); 83 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 84 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 85 signal(SIGINT, exitNowHandler); // dump final stats and exit 86 signal(SIGABRT, abortHandler); 87 88 Py_SetProgramName(argv[0]); 89 90 // default path to m5 python code is the currently executing 91 // file... Python ZipImporter will find embedded zip archive. 92 // The M5_ARCHIVE environment variable can be used to override this. 93 char *m5_archive = getenv("M5_ARCHIVE"); 94 string pythonpath = m5_archive ? m5_archive : argv[0]; 95 96 char *oldpath = getenv("PYTHONPATH"); 97 if (oldpath != NULL) { 98 pythonpath += ":"; 99 pythonpath += oldpath; 100 } 101 102 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1) 103 fatal("setenv: %s\n", strerror(errno)); 104 105 char *python_home = getenv("PYTHONHOME"); 106 if (!python_home) 107 python_home = PYTHONHOME; 108 Py_SetPythonHome(python_home); 109 110 // initialize embedded Python interpreter 111 Py_Initialize(); 112 PySys_SetArgv(argc, argv); 113 114 // initialize SWIG modules 115 init_swig(); 116 117 PyRun_SimpleString("import m5.main"); 118 PyRun_SimpleString("m5.main.main()"); 119 120 // clean up Python intepreter. 121 Py_Finalize(); 122} 123