Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that sthread_inside_simgrid is initially 1 to protect all lib constructors
[simgrid.git] / src / xbt / xbt_main.cpp
1 /* module handling                                                          */
2
3 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #define XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL /* MSVC don't want it to be declared extern in headers and local here */
9
10 #include "simgrid/config.h"
11 #include "simgrid/sg_config.hpp"
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h" // sthread_inside_simgrid
14 #include "xbt/config.hpp"
15 #include "xbt/coverage.h"
16 #include "xbt/dynar.h"
17 #include "xbt/log.h"
18 #include "xbt/log.hpp"
19 #include "xbt/misc.h"
20 #include "xbt/module.h" /* this module */
21 #include "xbt/sysdep.h"
22 #include "xbt/xbt_modinter.h" /* prototype of other module's init/exit in XBT */
23
24 #include <cmath>
25 #include <cstdio>
26 #ifdef _WIN32
27 # include <csignal> /* To silence MSVC on abort() */
28 #endif
29 #if HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string>
33 #include <vector>
34
35 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module, xbt, "module handling");
36
37 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories"); /* lives here even if that's a bit odd to solve linking issues: this is used in xbt_log_file_appender to detect whether SMPI is used (and thus whether we should unbench the writing to disk) */
38
39 namespace simgrid::xbt {
40 std::string binary_name;          /* Name of the system process containing us (mandatory to retrieve neat backtraces) */
41 std::vector<std::string> cmdline; /* all we got in argv */
42 } // namespace simgrid::xbt
43
44 int xbt_initialized = 0;
45 simgrid::config::Flag<bool> cfg_dbg_clean_atexit{
46     "debug/clean-atexit",
47     "Whether to cleanup SimGrid at exit. Disable it if your code segfaults after its end.",
48     true};
49
50 int xbt_pagesize;
51 int xbt_pagebits = 0;
52
53 /* Declare xbt_preinit and xbt_postexit as constructor/destructor of the library.
54  * This is crude and rather compiler-specific, unfortunately.
55  */
56 static void xbt_preinit() XBT_ATTRIB_CONSTRUCTOR(200);
57 static void xbt_postexit();
58
59 #ifdef _WIN32
60 #include <windows.h>
61
62 #ifndef __GNUC__
63 /* Should not be necessary but for some reason, DllMain is called twice at attachment and at detachment.*/
64 /* see also http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx */
65 /* and http://www.microsoft.com/whdc/driver/kernel/DLL_bestprac.mspx */
66 static BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 {
68   static bool xbt_dll_process_is_attached = false;
69   if (fdwReason == DLL_PROCESS_ATTACH && not xbt_dll_process_is_attached) {
70     xbt_dll_process_is_attached = true;
71     xbt_preinit();
72   } else if (fdwReason == DLL_PROCESS_DETACH && xbt_dll_process_is_attached) {
73     xbt_dll_process_is_attached = false;
74   }
75   return 1;
76 }
77 #endif
78
79 #endif
80
81 static void xbt_preinit()
82 {
83 #ifdef _WIN32
84   SYSTEM_INFO si;
85   GetSystemInfo(&si);
86   xbt_pagesize = si.dwPageSize;
87 #elif HAVE_SYSCONF
88   xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
89 #else
90 # error Cannot get page size.
91 #endif
92
93   xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
94
95 #ifdef _TWO_DIGIT_EXPONENT
96   /* Even printf behaves differently on Windows... */
97   _set_output_format(_TWO_DIGIT_EXPONENT);
98 #endif
99   xbt_log_preinit();
100   xbt_dict_preinit();
101   atexit(xbt_postexit);
102 }
103
104 static void xbt_postexit()
105 {
106   if (not cfg_dbg_clean_atexit)
107     return;
108   xbt_initialized--;
109   xbt_dict_postexit();
110   xbt_log_postexit();
111 #if SIMGRID_HAVE_MC
112   mmalloc_postexit();
113 #endif
114 }
115
116 /** @brief Initialize the xbt mechanisms. */
117 void xbt_init(int *argc, char **argv)
118 {
119   xbt_initialized++;
120   if (xbt_initialized > 1) {
121     XBT_DEBUG("XBT has been initialized %d times.", xbt_initialized);
122     return;
123   }
124
125   simgrid::xbt::install_exception_handler();
126
127   if (*argc > 0)
128     simgrid::xbt::binary_name = argv[0];
129   for (int i = 0; i < *argc; i++)
130     simgrid::xbt::cmdline.emplace_back(argv[i]);
131
132   xbt_log_init(argc, argv);
133 }
134
135 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
136 /** @brief like xbt_free, but you can be sure that it is a function  */
137 void xbt_free_f(void* p) noexcept(noexcept(::free))
138 {
139   xbt_free(p);
140 }
141
142 /** @brief should be given a pointer to pointer, and frees the second one */
143 void xbt_free_ref(void* d) noexcept(noexcept(::free))
144 {
145   xbt_free(*(void**)d);
146 }
147
148 /** @brief Kill the program in silence */
149 void xbt_abort()
150 {
151   /* Call __gcov_flush on abort when compiling with coverage options. */
152   coverage_checkpoint();
153 #ifdef _WIN32
154   /* We said *in silence*. We don't want to see the error message printed by Microsoft's implementation of abort(). */
155   raise(SIGABRT);
156   signal(SIGABRT, SIG_DFL);
157   raise(SIGABRT);
158 #endif
159   abort();
160 }
161
162 #ifndef HAVE_SMPI
163 int SMPI_is_inited()
164 {
165   return false;
166 }
167 #endif