main.cc revision 4123
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
33#include <iostream>
34#include <string>
35
36#include "base/cprintf.hh"
37#include "base/misc.hh"
38#include "config/pythonhome.hh"
39#include "python/swig/init.hh"
40#include "sim/async.hh"
41#include "sim/host.hh"
42#include "sim/root.hh"
43
44using namespace std;
45
46/// Stats signal handler.
47void
48dumpStatsHandler(int sigtype)
49{
50    async_event = true;
51    async_statdump = true;
52}
53
54void
55dumprstStatsHandler(int sigtype)
56{
57    async_event = true;
58    async_statdump = true;
59    async_statreset = true;
60}
61
62/// Exit signal handler.
63void
64exitNowHandler(int sigtype)
65{
66    async_event = true;
67    async_exit = true;
68}
69
70/// Abort signal handler.
71void
72abortHandler(int sigtype)
73{
74    ccprintf(cerr, "Program aborted at cycle %d\n", curTick);
75}
76
77int
78main(int argc, char **argv)
79{
80    signal(SIGFPE, SIG_IGN);		// may occur on misspeculated paths
81    signal(SIGTRAP, SIG_IGN);
82    signal(SIGUSR1, dumpStatsHandler);		// dump intermediate stats
83    signal(SIGUSR2, dumprstStatsHandler);	// dump and reset stats
84    signal(SIGINT, exitNowHandler);		// dump final stats and exit
85    signal(SIGABRT, abortHandler);
86
87    Py_SetProgramName(argv[0]);
88
89    // default path to m5 python code is the currently executing
90    // file... Python ZipImporter will find embedded zip archive.
91    // The M5_ARCHIVE environment variable can be used to override this.
92    char *m5_archive = getenv("M5_ARCHIVE");
93    string pythonpath = m5_archive ? m5_archive : argv[0];
94
95    char *oldpath = getenv("PYTHONPATH");
96    if (oldpath != NULL) {
97        pythonpath += ":";
98        pythonpath += oldpath;
99    }
100
101    if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
102        fatal("setenv: %s\n", strerror(errno));
103
104    char *python_home = getenv("PYTHONHOME");
105    if (!python_home)
106        python_home = PYTHONHOME;
107    Py_SetPythonHome(python_home);
108
109    // initialize embedded Python interpreter
110    Py_Initialize();
111    PySys_SetArgv(argc, argv);
112
113    // initialize SWIG modules
114    init_swig();
115
116    PyRun_SimpleString("import m5.main");
117    PyRun_SimpleString("m5.main.main()");
118
119    // clean up Python intepreter.
120    Py_Finalize();
121}
122