main.cc revision 4941
12SN/A/* 21762SN/A * Copyright (c) 2000-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 284123Sbinkertn@umich.edu * Authors: Nathan Binkert 292SN/A */ 302SN/A 314123Sbinkertn@umich.edu#include <Python.h> 324128Sbinkertn@umich.edu#include <signal.h> 332655Sstever@eecs.umich.edu 344123Sbinkertn@umich.edu#include <iostream> 354123Sbinkertn@umich.edu#include <string> 362SN/A 374123Sbinkertn@umich.edu#include "base/cprintf.hh" 38146SN/A#include "base/misc.hh" 393356Sbinkertn@umich.edu#include "config/pythonhome.hh" 403868Sbinkertn@umich.edu#include "python/swig/init.hh" 41146SN/A#include "sim/async.hh" 42146SN/A#include "sim/host.hh" 434167Sbinkertn@umich.edu#include "sim/core.hh" 442SN/A 452SN/Ausing namespace std; 462SN/A 472SN/A/// Stats signal handler. 482SN/Avoid 492SN/AdumpStatsHandler(int sigtype) 502SN/A{ 512SN/A async_event = true; 524123Sbinkertn@umich.edu async_statdump = true; 532SN/A} 542SN/A 55329SN/Avoid 56329SN/AdumprstStatsHandler(int sigtype) 57329SN/A{ 58329SN/A async_event = true; 594123Sbinkertn@umich.edu async_statdump = true; 604123Sbinkertn@umich.edu async_statreset = true; 61329SN/A} 62329SN/A 632SN/A/// Exit signal handler. 642SN/Avoid 652SN/AexitNowHandler(int sigtype) 662SN/A{ 672SN/A async_event = true; 682SN/A async_exit = true; 692SN/A} 702SN/A 71764SN/A/// Abort signal handler. 72764SN/Avoid 73764SN/AabortHandler(int sigtype) 74764SN/A{ 754123Sbinkertn@umich.edu ccprintf(cerr, "Program aborted at cycle %d\n", curTick); 76764SN/A} 77764SN/A 782SN/Aint 794941Snate@binkert.orgpython_main() 804941Snate@binkert.org{ 814941Snate@binkert.org PyObject *module; 824941Snate@binkert.org PyObject *dict; 834941Snate@binkert.org PyObject *result; 844941Snate@binkert.org 854941Snate@binkert.org module = PyImport_AddModule("__main__"); 864941Snate@binkert.org if (module == NULL) 874941Snate@binkert.org fatal("Could not import __main__"); 884941Snate@binkert.org 894941Snate@binkert.org dict = PyModule_GetDict(module); 904941Snate@binkert.org 914941Snate@binkert.org result = PyRun_String("import m5.main", Py_file_input, dict, dict); 924941Snate@binkert.org if (!result) { 934941Snate@binkert.org PyErr_Print(); 944941Snate@binkert.org return 1; 954941Snate@binkert.org } 964941Snate@binkert.org Py_DECREF(result); 974941Snate@binkert.org 984941Snate@binkert.org result = PyRun_String("m5.main.main()", Py_file_input, dict, dict); 994941Snate@binkert.org if (!result) { 1004941Snate@binkert.org PyErr_Print(); 1014941Snate@binkert.org return 1; 1024941Snate@binkert.org } 1034941Snate@binkert.org Py_DECREF(result); 1044941Snate@binkert.org 1054941Snate@binkert.org if (Py_FlushLine()) 1064941Snate@binkert.org PyErr_Clear(); 1074941Snate@binkert.org 1084941Snate@binkert.org return 0; 1094941Snate@binkert.org} 1104941Snate@binkert.org 1114941Snate@binkert.orgint 1122SN/Amain(int argc, char **argv) 1132SN/A{ 1142SN/A signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 1152SN/A signal(SIGTRAP, SIG_IGN); 116329SN/A signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 117329SN/A signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 118329SN/A signal(SIGINT, exitNowHandler); // dump final stats and exit 119764SN/A signal(SIGABRT, abortHandler); 1202SN/A 1212655Sstever@eecs.umich.edu Py_SetProgramName(argv[0]); 1222667Sstever@eecs.umich.edu 1232667Sstever@eecs.umich.edu // default path to m5 python code is the currently executing 1242889Sbinkertn@umich.edu // file... Python ZipImporter will find embedded zip archive. 1252889Sbinkertn@umich.edu // The M5_ARCHIVE environment variable can be used to override this. 1262889Sbinkertn@umich.edu char *m5_archive = getenv("M5_ARCHIVE"); 1272889Sbinkertn@umich.edu string pythonpath = m5_archive ? m5_archive : argv[0]; 1282667Sstever@eecs.umich.edu 1292667Sstever@eecs.umich.edu char *oldpath = getenv("PYTHONPATH"); 1302667Sstever@eecs.umich.edu if (oldpath != NULL) { 1312889Sbinkertn@umich.edu pythonpath += ":"; 1322889Sbinkertn@umich.edu pythonpath += oldpath; 1332667Sstever@eecs.umich.edu } 1342667Sstever@eecs.umich.edu 1352889Sbinkertn@umich.edu if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1) 1362667Sstever@eecs.umich.edu fatal("setenv: %s\n", strerror(errno)); 1372667Sstever@eecs.umich.edu 1383356Sbinkertn@umich.edu char *python_home = getenv("PYTHONHOME"); 1393356Sbinkertn@umich.edu if (!python_home) 1403356Sbinkertn@umich.edu python_home = PYTHONHOME; 1413356Sbinkertn@umich.edu Py_SetPythonHome(python_home); 1423356Sbinkertn@umich.edu 1432667Sstever@eecs.umich.edu // initialize embedded Python interpreter 1442655Sstever@eecs.umich.edu Py_Initialize(); 1452655Sstever@eecs.umich.edu PySys_SetArgv(argc, argv); 1461311SN/A 1473645Sbinkertn@umich.edu // initialize SWIG modules 1483868Sbinkertn@umich.edu init_swig(); 1491703SN/A 1504941Snate@binkert.org int ret = python_main(); 1512667Sstever@eecs.umich.edu 1522667Sstever@eecs.umich.edu // clean up Python intepreter. 1532655Sstever@eecs.umich.edu Py_Finalize(); 1544941Snate@binkert.org 1554941Snate@binkert.org return ret; 1562667Sstever@eecs.umich.edu} 157