main.cc revision 4123
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>
322655Sstever@eecs.umich.edu
334123Sbinkertn@umich.edu#include <iostream>
344123Sbinkertn@umich.edu#include <string>
352SN/A
364123Sbinkertn@umich.edu#include "base/cprintf.hh"
37146SN/A#include "base/misc.hh"
383356Sbinkertn@umich.edu#include "config/pythonhome.hh"
393868Sbinkertn@umich.edu#include "python/swig/init.hh"
40146SN/A#include "sim/async.hh"
41146SN/A#include "sim/host.hh"
421696SN/A#include "sim/root.hh"
432SN/A
442SN/Ausing namespace std;
452SN/A
462SN/A/// Stats signal handler.
472SN/Avoid
482SN/AdumpStatsHandler(int sigtype)
492SN/A{
502SN/A    async_event = true;
514123Sbinkertn@umich.edu    async_statdump = true;
522SN/A}
532SN/A
54329SN/Avoid
55329SN/AdumprstStatsHandler(int sigtype)
56329SN/A{
57329SN/A    async_event = true;
584123Sbinkertn@umich.edu    async_statdump = true;
594123Sbinkertn@umich.edu    async_statreset = true;
60329SN/A}
61329SN/A
622SN/A/// Exit signal handler.
632SN/Avoid
642SN/AexitNowHandler(int sigtype)
652SN/A{
662SN/A    async_event = true;
672SN/A    async_exit = true;
682SN/A}
692SN/A
70764SN/A/// Abort signal handler.
71764SN/Avoid
72764SN/AabortHandler(int sigtype)
73764SN/A{
744123Sbinkertn@umich.edu    ccprintf(cerr, "Program aborted at cycle %d\n", curTick);
75764SN/A}
76764SN/A
772SN/Aint
782SN/Amain(int argc, char **argv)
792SN/A{
802SN/A    signal(SIGFPE, SIG_IGN);		// may occur on misspeculated paths
812SN/A    signal(SIGTRAP, SIG_IGN);
82329SN/A    signal(SIGUSR1, dumpStatsHandler);		// dump intermediate stats
83329SN/A    signal(SIGUSR2, dumprstStatsHandler);	// dump and reset stats
84329SN/A    signal(SIGINT, exitNowHandler);		// dump final stats and exit
85764SN/A    signal(SIGABRT, abortHandler);
862SN/A
872655Sstever@eecs.umich.edu    Py_SetProgramName(argv[0]);
882667Sstever@eecs.umich.edu
892667Sstever@eecs.umich.edu    // default path to m5 python code is the currently executing
902889Sbinkertn@umich.edu    // file... Python ZipImporter will find embedded zip archive.
912889Sbinkertn@umich.edu    // The M5_ARCHIVE environment variable can be used to override this.
922889Sbinkertn@umich.edu    char *m5_archive = getenv("M5_ARCHIVE");
932889Sbinkertn@umich.edu    string pythonpath = m5_archive ? m5_archive : argv[0];
942667Sstever@eecs.umich.edu
952667Sstever@eecs.umich.edu    char *oldpath = getenv("PYTHONPATH");
962667Sstever@eecs.umich.edu    if (oldpath != NULL) {
972889Sbinkertn@umich.edu        pythonpath += ":";
982889Sbinkertn@umich.edu        pythonpath += oldpath;
992667Sstever@eecs.umich.edu    }
1002667Sstever@eecs.umich.edu
1012889Sbinkertn@umich.edu    if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
1022667Sstever@eecs.umich.edu        fatal("setenv: %s\n", strerror(errno));
1032667Sstever@eecs.umich.edu
1043356Sbinkertn@umich.edu    char *python_home = getenv("PYTHONHOME");
1053356Sbinkertn@umich.edu    if (!python_home)
1063356Sbinkertn@umich.edu        python_home = PYTHONHOME;
1073356Sbinkertn@umich.edu    Py_SetPythonHome(python_home);
1083356Sbinkertn@umich.edu
1092667Sstever@eecs.umich.edu    // initialize embedded Python interpreter
1102655Sstever@eecs.umich.edu    Py_Initialize();
1112655Sstever@eecs.umich.edu    PySys_SetArgv(argc, argv);
1121311SN/A
1133645Sbinkertn@umich.edu    // initialize SWIG modules
1143868Sbinkertn@umich.edu    init_swig();
1151703SN/A
1163102Sstever@eecs.umich.edu    PyRun_SimpleString("import m5.main");
1173102Sstever@eecs.umich.edu    PyRun_SimpleString("m5.main.main()");
1182667Sstever@eecs.umich.edu
1192667Sstever@eecs.umich.edu    // clean up Python intepreter.
1202655Sstever@eecs.umich.edu    Py_Finalize();
1212667Sstever@eecs.umich.edu}
122