main.cc revision 4128
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"
431696SN/A#include "sim/root.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
792SN/Amain(int argc, char **argv)
802SN/A{
812SN/A    signal(SIGFPE, SIG_IGN);		// may occur on misspeculated paths
822SN/A    signal(SIGTRAP, SIG_IGN);
83329SN/A    signal(SIGUSR1, dumpStatsHandler);		// dump intermediate stats
84329SN/A    signal(SIGUSR2, dumprstStatsHandler);	// dump and reset stats
85329SN/A    signal(SIGINT, exitNowHandler);		// dump final stats and exit
86764SN/A    signal(SIGABRT, abortHandler);
872SN/A
882655Sstever@eecs.umich.edu    Py_SetProgramName(argv[0]);
892667Sstever@eecs.umich.edu
902667Sstever@eecs.umich.edu    // default path to m5 python code is the currently executing
912889Sbinkertn@umich.edu    // file... Python ZipImporter will find embedded zip archive.
922889Sbinkertn@umich.edu    // The M5_ARCHIVE environment variable can be used to override this.
932889Sbinkertn@umich.edu    char *m5_archive = getenv("M5_ARCHIVE");
942889Sbinkertn@umich.edu    string pythonpath = m5_archive ? m5_archive : argv[0];
952667Sstever@eecs.umich.edu
962667Sstever@eecs.umich.edu    char *oldpath = getenv("PYTHONPATH");
972667Sstever@eecs.umich.edu    if (oldpath != NULL) {
982889Sbinkertn@umich.edu        pythonpath += ":";
992889Sbinkertn@umich.edu        pythonpath += oldpath;
1002667Sstever@eecs.umich.edu    }
1012667Sstever@eecs.umich.edu
1022889Sbinkertn@umich.edu    if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
1032667Sstever@eecs.umich.edu        fatal("setenv: %s\n", strerror(errno));
1042667Sstever@eecs.umich.edu
1053356Sbinkertn@umich.edu    char *python_home = getenv("PYTHONHOME");
1063356Sbinkertn@umich.edu    if (!python_home)
1073356Sbinkertn@umich.edu        python_home = PYTHONHOME;
1083356Sbinkertn@umich.edu    Py_SetPythonHome(python_home);
1093356Sbinkertn@umich.edu
1102667Sstever@eecs.umich.edu    // initialize embedded Python interpreter
1112655Sstever@eecs.umich.edu    Py_Initialize();
1122655Sstever@eecs.umich.edu    PySys_SetArgv(argc, argv);
1131311SN/A
1143645Sbinkertn@umich.edu    // initialize SWIG modules
1153868Sbinkertn@umich.edu    init_swig();
1161703SN/A
1173102Sstever@eecs.umich.edu    PyRun_SimpleString("import m5.main");
1183102Sstever@eecs.umich.edu    PyRun_SimpleString("m5.main.main()");
1192667Sstever@eecs.umich.edu
1202667Sstever@eecs.umich.edu    // clean up Python intepreter.
1212655Sstever@eecs.umich.edu    Py_Finalize();
1222667Sstever@eecs.umich.edu}
123