main.cc (5202:ff56fa8c2091) main.cc (5522:e56c3d89be79)
1/*
1/*
2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
2 * Copyright (c) 2008 The Hewlett-Packard Development Company
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

--- 13 unchanged lines hidden (view full) ---

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>
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

--- 13 unchanged lines hidden (view full) ---

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
32
34#include <iostream>
35#include <string>
33#include "sim/init.hh"
36
34
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
35// main() is now pretty stripped down and just sets up python and then
36// calls initM5Python which loads the various embedded python modules
37// into the python environment and then starts things running by
38// calling m5Main.
78int
39int
79python_main()
80{
81 PyObject *module;
82 PyObject *dict;
83 PyObject *result;
84
85 module = PyImport_AddModule(const_cast<char*>("__main__"));
86 if (module == NULL)
87 fatal("Could not import __main__");
88
89 dict = PyModule_GetDict(module);
90
91 result = PyRun_String("import m5.main", Py_file_input, dict, dict);
92 if (!result) {
93 PyErr_Print();
94 return 1;
95 }
96 Py_DECREF(result);
97
98 result = PyRun_String("m5.main.main()", Py_file_input, dict, dict);
99 if (!result) {
100 PyErr_Print();
101 return 1;
102 }
103 Py_DECREF(result);
104
105 if (Py_FlushLine())
106 PyErr_Clear();
107
108 return 0;
109}
110
111int
112main(int argc, char **argv)
113{
40main(int argc, char **argv)
41{
114 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths
115 signal(SIGTRAP, SIG_IGN);
116 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats
117 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats
118 signal(SIGINT, exitNowHandler); // dump final stats and exit
119 signal(SIGABRT, abortHandler);
42 int ret;
120
43
44 // Initialize m5 special signal handling.
45 initSignals();
46
121 Py_SetProgramName(argv[0]);
122
47 Py_SetProgramName(argv[0]);
48
123 // default path to m5 python code is the currently executing
124 // file... Python ZipImporter will find embedded zip archive.
125 // The M5_ARCHIVE environment variable can be used to override this.
126 char *m5_archive = getenv("M5_ARCHIVE");
127 string pythonpath = m5_archive ? m5_archive : argv[0];
128
129 char *oldpath = getenv("PYTHONPATH");
130 if (oldpath != NULL) {
131 pythonpath += ":";
132 pythonpath += oldpath;
133 }
134
135 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
136 fatal("setenv: %s\n", strerror(errno));
137
138 const char *python_home = getenv("PYTHONHOME");
139 if (!python_home)
140 python_home = PYTHONHOME;
141 Py_SetPythonHome(const_cast<char*>(python_home));
142
143 // initialize embedded Python interpreter
144 Py_Initialize();
49 // initialize embedded Python interpreter
50 Py_Initialize();
145 PySys_SetArgv(argc, argv);
146
51
147 // initialize SWIG modules
148 init_swig();
52 // Initialize the embedded m5 python library
53 ret = initM5Python();
149
54
150 int ret = python_main();
55 if (ret == 0) {
56 // start m5
57 ret = m5Main(argc, argv);
58 }
151
152 // clean up Python intepreter.
153 Py_Finalize();
154
155 return ret;
156}
59
60 // clean up Python intepreter.
61 Py_Finalize();
62
63 return ret;
64}