3 * Copyright (C) 2007, Harbour, All rights reserved.
10 bool F::sigsegv_ = false;
15 void (*F_App::idle_)(void);
16 F_App *F_App::this_ = 0;
17 std::string F_App::name_;
19 ost::CommandOption *f_app_cmd_options = 0;
20 ost::CommandOptionNoArg help_option("help", "h", "\t\t\tprints short usage info",
21 false, &f_app_cmd_options);
22 ost::CommandOptionNoArg version_option("version", "v",
23 "\t\tprints program name & version", false, &f_app_cmd_options);
27 #define F_BINUTILS_DEMANGLER
29 #ifdef F_BINUTILS_DEMANGLER
31 extern "C" int init_demangler(const char *style, const char *options,
32 const char *demangler);
33 extern "C" char *cplus_demangle(const char *mangled, int options);
35 #define DMGL_AUTO (1 << 8)
36 const char *F::demangled_(const char *mngl)
41 init_demangler(NULL, NULL, NULL);
45 return cplus_demangle(mngl, DMGL_AUTO);
50 const char *F::demangled_(const char *mngl)
52 static std::string mangled;
57 // do we need to free ptr ?
58 const char *dm = abi::__cxa_demangle(mangled.c_str(), ptr, &len, &status);
61 return mangled.c_str();
66 // for this pleasure you need libiberty from binutils or cxxabi.h
67 #define F_DEMANGLED_BACKTRACE
69 #ifdef F_DEMANGLED_BACKTRACE
71 static std::string demangle_(const char *mngl)
75 // [X] [0xffffe440] - ÏÔÂÒÁÓÙ×ÁÅÍ
76 // [X] /lib/libpthread.so.0 [0xb7d23167] - ÏÔÂÒÁÓÙ×ÁÅÍ
77 // [X] ./test/demo(_ZN1F12F_Console_UID2Ev+0x35) [0x804fd25] - ÄÅÍÁÎÇÌÉÍ
78 // ×ÙÒÅÚÁÅÍ ÏÔÓÅÄÁ|......................| ÄÏ ÓÅÄÁ ×ËÌÀÞÉÔÅÌØÎÏ
79 std::string mangled = mngl;
80 size_t fname_start = mangled.find('(');
81 if (fname_start == std::string::npos) // no '('
83 size_t fname_end = mangled.rfind('+');
84 if (fname_end == std::string::npos) // no '+'
86 std::string mfunc(mangled, fname_start + 1, fname_end - fname_start - 1);
87 std::string demangled(mangled, 0, fname_start + 1);
88 const char *dm = demangled_(mfunc.c_str());
92 if (demangled.find("()") == std::string::npos)
93 demangled.append("()");
94 demangled.append(mngl + fname_end);
99 #define demangle_(x) std::string(x)
102 #include <sys/utsname.h>
103 #include <gnu/libc-version.h>
105 // ËÒÁÔËÉÅ Ó×ÅÄÅÎÉÑ Ï ÓÉÓÔÅÍÅ
106 static inline void print_sysinfo()
109 int err = uname(&buf);
113 setlocale(LC_ALL, "C");
114 strftime(occured, sizeof(occured), "%b %d %Y %H:%M:%S", localtime(&t));
115 setlocale(LC_ALL, "");
117 std::clog << "Occured : " << occured << std::endl;
118 std::clog << "Compiled : " << __DATE__ << " " << __TIME__ << std::endl;
119 std::clog << "Node : " << (err ? "Unknown" : buf.nodename) << std::endl;
120 std::clog << "Machine : " << (err ? "Unknown" : buf.machine) << std::endl;
121 std::clog << "OS Name : " << (err ? "Unknown" : buf.sysname) << std::endl;
122 std::clog << "Release : " << (err ? "Unknown" : buf.release) << std::endl;
123 std::clog << "Version : " << (err ? "Unknown" : buf.version) << std::endl;
124 std::clog << "GCC : " << __VERSION__ << std::endl;
125 std::clog << "GLIBC : " << gnu_get_libc_version() << " " << gnu_get_libc_release() << std::endl;
126 std::clog << std::endl;
129 #include <execinfo.h>
131 static inline void print_backtrace(siginfo_t* siginfo, signal_regs_t *regs)
133 // may be implement smth. like show_regs ...?
135 size_t bas = backtrace(ba, 128);
136 char **bts = backtrace_symbols(ba, bas);
138 std::clog << "*** Siginfo: pid "<< getpid() << ", tid " << pthread_self()
139 << ", fault eip 0x" << std::hex << regs->eip << ", fault addr " <<
140 siginfo->si_addr << std::endl << std::dec;
141 std::clog << "*** Current thread backtrace (" << (bas - 3) << " funcs) :" <<
142 std::endl << std::endl;
143 for (size_t i = 3; i < bas; i++) // ÐÅÒ×ÙÅ ÔÒÉ ÎÅ ÎÕÖÎÙ
144 std::clog << "[" << (i - 3) << "] " << demangle_(bts[i]) << std::endl;
145 std::clog << std::endl;
150 // ÔÁËÖÅ ÄÌÑ ÚÁ×ÉÓÛÉÈ ×ÁÒÉÁÎÔÏ× ÍÏÖÎÏ ÚÁÄÁÔØ ÓÉÇÎÁÌ (F_TRACE_SIGNAL) É ÐÏ ÎÅÍÕ
151 // ÐÅÞÁÔÁÔØ backtrace(), ÔÏËÍÁ ÓÌÅÄÕÅÔØ ÕÞÅÓÔØ ÛÏ ÜÎÔÏÔ ÓÉÇÎÁÌ ÐÏÌÕÞÁÀÔØ ÕÓÅ
152 // ÔÒÅÄÙ, ÔÏ ÂÉÛØ ÐÏÌÕÞÉÍ ËÉÐÕ backtrac'Ï× ÄÌÑ multithread app ...
155 void F_App::signal_handler(int signo, siginfo_t* siginfo, void* ptr)
157 struct sigaction dfl_handler;
158 sigemptyset(&dfl_handler.sa_mask);
159 dfl_handler.sa_flags = SA_SIGINFO | SA_RESTART;
160 dfl_handler.sa_handler = 0;
161 dfl_handler.sa_restorer = NULL;
165 dfl_handler.sa_sigaction = F_App::signal_handler;
166 sigaction(SIGTSTP, &dfl_handler, NULL);
167 sigaction(SIGSTOP, &dfl_handler, NULL);
168 if (this_->stopped) {
171 this_->ui_->sigcont();
172 this_->stopped = false;
177 // temporary disable signals
178 dfl_handler.sa_handler = SIG_IGN;
179 sigaction(SIGINT, &dfl_handler, NULL);
180 sigaction(SIGQUIT, &dfl_handler, NULL);
181 // ask user if he/she want to leave app
182 if (this_->exit_confirm(1))
184 else { // reenable sigs
185 dfl_handler.sa_sigaction = F_App::signal_handler;
186 sigaction(SIGINT, &dfl_handler, NULL);
187 sigaction(SIGQUIT, &dfl_handler, NULL);
192 if (!this_->sigstop()) // app doesn't accept sigstops
194 this_->stopped = true;
196 this_->ui_->sigstop();
197 dfl_handler.sa_handler = SIG_DFL;
198 sigaction(SIGTSTP, &dfl_handler, NULL);
202 if (this_->ui_ && (getpid() == F_UI::pid)) // deliver only to ui thread
203 this_->ui_->sigwinch();
207 std::clog << "\n*** Got backtrace signal \'" << strsignal(signo) <<
209 print_backtrace(siginfo, (signal_regs_t *)ptr);
216 this_->ui_->sigsegv();
217 std::clog << "\n*** Got " << strsignal(signo) << " signal, exiting ...\n";
218 print_backtrace(siginfo, (signal_regs_t *)ptr);
220 std::clog << "*** You may send this backtrace to \'" << this_->author_ <<
228 this_->ui_->shutdown();
229 log("signal_handler", ALERT_LEVEL, "ðÒÅËÒÁÝÅÎÉÅ ÒÁÂÏÔÙ (%s signal)",
234 static void out_of_memory()
236 std::cerr << "Out of memory, bye ..." << std::endl;
237 // if we can free some memory for backtrace it will be wonderfull ...
238 // kill(getpid(), F_TRACE_SIGNAL);
242 void F_App::signals_setup(void)
244 struct sigaction new_action;
245 sigemptyset(&new_action.sa_mask);
246 new_action.sa_handler = 0;
247 new_action.sa_restorer = NULL;
248 new_action.sa_sigaction = F_App::signal_handler;
249 new_action.sa_flags = SA_SIGINFO | SA_ONESHOT;
250 // system signals - default action is to terminate app
251 sigaction (SIGHUP, &new_action, NULL);
252 sigaction (SIGSEGV, &new_action, NULL);
253 sigaction (SIGILL, &new_action, NULL);
254 sigaction (SIGFPE, &new_action, NULL);
255 sigaction (SIGBUS, &new_action, NULL);
257 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
258 sigaction (SIGCHLD, &new_action, NULL);
259 sigaction (SIGSTOP, &new_action, NULL); // sigstop is no reacheable for app
260 sigaction (SIGCONT, &new_action, NULL);
261 sigaction (SIGTSTP, &new_action, NULL);
262 sigaction (SIGQUIT, &new_action, NULL);
263 sigaction (SIGINT, &new_action, NULL);
264 sigaction (SIGWINCH, &new_action, NULL);
265 sigaction (F_TRACE_SIGNAL, &new_action, NULL);
268 void F_App::default_idle(void)
270 ost::Thread::sleep(10);
275 F_App::F_App(int argc, char **argv, const char *name, const char *version,
276 const char *author, const char *license, ost::CommandOption *user_cmd_opts,
277 const char *comment, const char *l_prefix)
280 std::cerr << "Only one F_App instance is allowed, bye ..." << std::endl;
286 old_new_handler = std::set_new_handler(out_of_memory);
288 std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
291 name_ = name ? name : "Unknown App,";
292 version_ = version ? version : "version unknown,";
293 author_ = author ? author : "Mr. Unknown <unknown@nowhere.world>";
294 license_ = license ? license : "unknown";
295 // parse internal command options
297 ost::CommandOption *tt = user_cmd_opts;
300 // this is strange - options is parsed from end ?!
301 tt->next = f_app_cmd_options;
304 comment = "program usage :";
307 cmd_opts_ = ost::makeCommandOptionParse(argc, argv, (char *)comment,
308 user_cmd_opts ? user_cmd_opts : f_app_cmd_options);
309 if (help_option.numSet) {
310 std::clog << cmd_opts_->printUsage();
313 if (version_option.numSet) {
314 std::clog << name_ << " " << version_ << " written by " << author_ <<
315 " under " << license_ << " license" << std::endl;
318 if (cmd_opts_->argsHaveError()) {
319 std::cerr << cmd_opts_->printErrors();
320 std::cerr << "Did you try " << argv[0] << " --help ?\n";
323 // create main log channel
326 log_prefix(l_prefix);
327 log("F_App", DEBUG_LEVEL, "F_App() v%0d.%0d.%0d created.", F_MAJOR_VERSION,
328 F_MINOR_VERSION, F_PATCH_VERSION);
331 void F::shutdown(int retval)
337 // do this as atexit handler ?
346 void F_App::init(void)
348 // ÅÓÌÉ ÐÒÏÇÒÁÍÍÁ ×ÙÈÏÄÉÔ ÔÕÔÁ ÐÏ Ctrl-C, ÔÏ ÔÁË ËÁË ui ËÌÁÓÓ ÍÏÖÅÔ ÂÙÔØ
349 // ÅÝÅ ÎÅ ÓÏÚÄÁÎ - ÓÌÅÄÕÅÔ ÜÔÏ ÕÞÅÓÔØ
350 parse_command_options();
352 if (!F_UI::ui_count()) {
353 log("ui_count", FATAL_LEVEL, "Oops, your app doesn't define any UI !");
356 ui_ = F_UI::make_ui();