Add copyright notice
[forms.git] / src / F_App.C
blob78435061303dd6f3ee083ed5a7c15f2d5941c8bd
2  /*
3   *   Copyright (C) 2007, Harbour, All rights reserved.
4   */
6 #include <iostream>
7 #include <F_App.H>
9 using namespace F;
10 bool F::sigsegv_ = false;
12 int F_App::argc_;
13 char **F_App::argv_;
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);
25 #include <cxxabi.h>
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)
38 #ifdef OLD_BINUTILS
39  static bool init_;
40  if (!init) {
41    init_demangler(NULL, NULL, NULL);
42    init_ = true;
43  }
44 #endif
45  return cplus_demangle(mngl, DMGL_AUTO);
48 #else
50 const char *F::demangled_(const char *mngl)
52  static std::string mangled;
53  char *ptr = NULL;
54  size_t len;
55  int status;
56  mangled = mngl;
57  // do we need to free ptr ?
58  const char *dm = abi::__cxa_demangle(mangled.c_str(), ptr, &len, &status);
59  if (dm)
60    mangled = dm;
61  return mangled.c_str();
64 #endif
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)
73  // ÂÙ×ÁÀÔ ×ÁÒÉÁÎÔÙ :
74  //
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 '('
82    return mangled;
83  size_t fname_end = mangled.rfind('+');
84  if (fname_end == std::string::npos) // no '+'
85    return mangled;
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());
89  if (!dm)
90    dm = mfunc.c_str();
91  demangled.append(dm);
92  if (demangled.find("()") == std::string::npos)
93    demangled.append("()");
94  demangled.append(mngl + fname_end);
95  return demangled;
98 #else
99 #define demangle_(x) std::string(x)
100 #endif
102 #include <sys/utsname.h>
103 #include <gnu/libc-version.h>
105 // ËÒÁÔËÉÅ Ó×ÅÄÅÎÉÑ Ï ÓÉÓÔÅÍÅ
106 static inline void print_sysinfo()
108  struct utsname buf;
109  int err = uname(&buf);
110  time_t t = time(0);
112  std::clog << "Occured  : " << ctime(&t);
113  std::clog << "Compiled : " << __DATE__ << " " << __TIME__ << std::endl;
114  std::clog << "Node     : " << (err ? "Unknown" : buf.nodename) << std::endl;
115  std::clog << "Machine  : " << (err ? "Unknown" : buf.machine) << std::endl;
116  std::clog << "OS Name  : " << (err ? "Unknown" : buf.sysname) << std::endl;
117  std::clog << "Release  : " << (err ? "Unknown" : buf.release) << std::endl;
118  std::clog << "Version  : " << (err ? "Unknown" : buf.version) << std::endl;
119  std::clog << "GCC      : " << __VERSION__ << std::endl;
120  std::clog << "GLIBC    : " << gnu_get_libc_version() << " " << gnu_get_libc_release() << std::endl;
121  std::clog << std::endl;
124 #include <execinfo.h>
126 static inline void print_backtrace(siginfo_t* siginfo, signal_regs_t *regs)
128  // may be implement smth. like show_regs ...?
129  void *ba[128];
130  size_t bas = backtrace(ba, 128);
131  char **bts = backtrace_symbols(ba, bas);
132  if (bts) {
133    std::clog << "*** Siginfo: pid "<< getpid() << ", tid " << pthread_self()
134     << ", fault eip 0x" << std::hex << regs->eip << ", fault addr " <<
135     siginfo->si_addr <<  std::endl << std::dec;
136    std::clog << "*** Current thread backtrace (" << (bas - 3) << " funcs) :" <<
137     std::endl << std::endl;
138    for (size_t i = 3; i < bas; i++) // ÐÅÒ×ÙÅ ÔÒÉ ÎÅ ÎÕÖÎÙ
139     std::clog << "[" << (i - 3) << "] " << demangle_(bts[i]) << std::endl;
140    std::clog << std::endl;
141    free(bts);
145 // ÔÁËÖÅ ÄÌÑ ÚÁ×ÉÓÛÉÈ ×ÁÒÉÁÎÔÏ× ÍÏÖÎÏ ÚÁÄÁÔØ ÓÉÇÎÁÌ (F_TRACE_SIGNAL) É ÐÏ ÎÅÍÕ
146 // ÐÅÞÁÔÁÔØ backtrace(), ÔÏËÍÁ ÓÌÅÄÕÅÔØ ÕÞÅÓÔØ ÛÏ ÜÎÔÏÔ ÓÉÇÎÁÌ ÐÏÌÕÞÁÀÔØ ÕÓÅ
147 // ÔÒÅÄÙ, ÔÏ ÂÉÛØ ÐÏÌÕÞÉÍ ËÉÐÕ backtrac'Ï× ÄÌÑ multithread app ...
150 void F_App::signal_handler(int signo, siginfo_t* siginfo, void* ptr)
152   struct sigaction dfl_handler;
153   sigemptyset(&dfl_handler.sa_mask);
154   dfl_handler.sa_flags = SA_SIGINFO | SA_RESTART;
155   dfl_handler.sa_handler = 0;
156   dfl_handler.sa_restorer = NULL;
157   switch (signo) {
158     case SIGCONT:
159     case SIGCHLD:
160       dfl_handler.sa_sigaction = F_App::signal_handler;
161       sigaction(SIGTSTP, &dfl_handler, NULL);
162       sigaction(SIGSTOP, &dfl_handler, NULL);
163       if (this_->stopped) {
164         this_->sigcont();
165         if (this_->ui_)
166           this_->ui_->sigcont();
167         this_->stopped = false;
168       }
169       return;
170     case SIGINT:
171     case SIGQUIT:
172       // temporary disable signals
173       dfl_handler.sa_handler = SIG_IGN;
174       sigaction(SIGINT, &dfl_handler, NULL);
175       sigaction(SIGQUIT, &dfl_handler, NULL);
176       // ask user if he/she want to leave app
177       if (this_->exit_confirm(1))
178         break;
179       else { // reenable sigs
180         dfl_handler.sa_sigaction = F_App::signal_handler;
181         sigaction(SIGINT, &dfl_handler, NULL);
182         sigaction(SIGQUIT, &dfl_handler, NULL);
183       }             
184       return;
185     case SIGTSTP:
186     case SIGSTOP:
187       if (!this_->sigstop()) // app doesn't accept sigstops
188         return;
189       this_->stopped = true;
190       if (this_->ui_)
191         this_->ui_->sigstop();
192       dfl_handler.sa_handler = SIG_DFL;
193       sigaction(SIGTSTP, &dfl_handler, NULL);
194       raise(SIGTSTP);
195       return;
196     case SIGWINCH:
197       if (this_->ui_ && (getpid() == F_UI::pid)) // deliver only to ui thread
198         this_->ui_->sigwinch();
199       return;
200 #ifdef DEBUG_VERSION
201     case F_TRACE_SIGNAL:
202       std::clog << "\n*** Got backtrace signal \'" << strsignal(signo) <<
203         "\'.\n";
204       print_backtrace(siginfo, (signal_regs_t *)ptr);
205       return;
206     case SIGSEGV:
207     case SIGILL:
208     case SIGFPE:
209     case SIGBUS:
210      if (this_->ui_)
211        this_->ui_->sigsegv();
212      std::clog << "\n*** Got " << strsignal(signo) << " signal, exiting ...\n";
213      print_backtrace(siginfo, (signal_regs_t *)ptr);
214      print_sysinfo();
215      std::clog << "*** You may send this backtrace to \'" << this_->author_ <<
216       "\'\n\n";
217      break;      
218 #endif
219     default:
220       break;
221    }
222  if (this_->ui_)
223    this_->ui_->shutdown();
224  log("signal_handler", ALERT_LEVEL, "ðÒÅËÒÁÝÅÎÉÅ ÒÁÂÏÔÙ (%s signal)",
225    strsignal(signo));
226  ::exit(0);
229 static void out_of_memory()
231  std::cerr << "Out of memory, bye ..." << std::endl;
232  // if we can free some memory for backtrace it will be wonderfull ...
233  // kill(getpid(), F_TRACE_SIGNAL);
234  ::exit(-1);
237 void F_App::signals_setup(void)
239  struct sigaction new_action;
240  sigemptyset(&new_action.sa_mask);
241  new_action.sa_handler = 0;
242  new_action.sa_restorer = NULL;
243  new_action.sa_sigaction = F_App::signal_handler;
244  new_action.sa_flags = SA_SIGINFO | SA_ONESHOT;
245  // system signals - default action is to terminate app
246  sigaction (SIGHUP, &new_action, NULL);
247  sigaction (SIGSEGV, &new_action, NULL);
248  sigaction (SIGILL, &new_action, NULL);
249  sigaction (SIGFPE, &new_action, NULL);
250  sigaction (SIGBUS, &new_action, NULL);
251  // user trapped sigs
252  new_action.sa_flags = SA_SIGINFO | SA_RESTART;
253  sigaction (SIGCHLD, &new_action, NULL);
254  sigaction (SIGSTOP, &new_action, NULL); // sigstop is no reacheable for app
255  sigaction (SIGCONT, &new_action, NULL);
256  sigaction (SIGTSTP, &new_action, NULL);
257  sigaction (SIGQUIT, &new_action, NULL);
258  sigaction (SIGINT, &new_action, NULL);
259  sigaction (SIGWINCH, &new_action, NULL);
260  sigaction (F_TRACE_SIGNAL, &new_action, NULL);
263 void F_App::default_idle(void)
265  ost::Thread::sleep(10);
268 #include <exception>
270 F_App::F_App(int argc, char **argv, const char *name, const char *version,
271   const char *author, const char *license, ost::CommandOption *user_cmd_opts,
272   const char *comment, const char *l_prefix)
274  if (this_) {
275    std::cerr << "Only one F_App instance is allowed, bye ..." << std::endl;
276    ::abort();
278  this_ = this;
279  ui_ = 0;
280  stopped = false;
281  old_new_handler = std::set_new_handler(out_of_memory);
282  // GNU extension
283  std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
284  signals_setup();
285  idle(default_idle);
286  name_ = name ? name : "Unknown App,";
287  version_ = version ? version : "version unknown,";
288  author_ = author ? author : "Mr. Unknown <unknown@nowhere.world>";
289  license_ = license ? license : "unknown";
290  // parse internal command options
291  if (user_cmd_opts) {
292   ost::CommandOption *tt = user_cmd_opts;
293    while (tt->next)
294     tt = tt->next;
295    // this is strange - options is parsed from end ?!
296    tt->next = f_app_cmd_options;
298  if (!comment)
299    comment = "program usage :";
300  this->argc_ = argc;
301  this->argv_ = argv; 
302  cmd_opts_ = ost::makeCommandOptionParse(argc, argv, (char *)comment,
303    user_cmd_opts ? user_cmd_opts : f_app_cmd_options);
304  if (help_option.numSet) {
305    std::clog << cmd_opts_->printUsage();
306    ::exit(0);
308  if (version_option.numSet) {
309    std::clog << name_ << " " << version_ << " written by " << author_ <<
310      " under " << license_ << " license" << std::endl;
311    ::exit(0);
313  if (cmd_opts_->argsHaveError()) {
314    std::cerr << cmd_opts_->printErrors();
315    std::cerr << "Did you try " << argv[0] << " --help ?\n";
316    ::exit(-1);
318  // create main log channel
319  log_init();
320  if (l_prefix)
321    log_prefix(l_prefix);
322  log("F_App", DEBUG_LEVEL, "F_App() v%0d.%0d.%0d created.", F_MAJOR_VERSION,
323    F_MINOR_VERSION, F_PATCH_VERSION);
326 void F::shutdown(int retval)
328  F_App::shutdown();
329  ::exit(retval);
332 // do this as atexit handler ?
333 F_App::~F_App()
335  shutdown();
336  if (this_)
337    delete this_;
338  this_ = 0;
341 void F_App::init(void)
343  // ÅÓÌÉ ÐÒÏÇÒÁÍÍÁ ×ÙÈÏÄÉÔ ÔÕÔÁ ÐÏ Ctrl-C, ÔÏ ÔÁË ËÁË ui ËÌÁÓÓ ÍÏÖÅÔ ÂÙÔØ
344  // ÅÝÅ ÎÅ ÓÏÚÄÁΠ- ÓÌÅÄÕÅÔ ÜÔÏ ÕÞÅÓÔØ
345  parse_command_options();
346  // create ui
347  if (!F_UI::ui_count()) {
348   log("ui_count", FATAL_LEVEL, "Oops, your app doesn't define any UI !");
349   ::exit(-1);
351  ui_ = F_UI::make_ui();