Back to index

texmacs  1.0.7.15
tm_debug.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_debug.cpp
00004 * DESCRIPTION: Debugging facilities
00005 * COPYRIGHT  : (C) 2011  Joris van der Hoeven
00006 *              (C) 2008  Timo Bingmann from http://idlebox.net
00007 *******************************************************************************
00008 * This software falls under the GNU general public license version 3 or later.
00009 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00010 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00011 ******************************************************************************/
00012 
00013 #include "tm_server.hpp"
00014 #include "file.hpp"
00015 #include "tm_link.hpp"
00016 #include "sys_utils.hpp"
00017 
00018 bool rescue_mode= false;
00019 
00020 /******************************************************************************
00021 * Status reports
00022 ******************************************************************************/
00023 
00024 string
00025 get_system_information () {
00026   string r;
00027   r << "System information:\n";
00028   r << "  TeXmacs version  : "
00029     << TEXMACS_VERSION << "\n";
00030   r << "  Built by         : "
00031     << BUILD_USER << "\n";
00032   r << "  Building date    : "
00033     << BUILD_DATE << "\n";
00034   r << "  Operating system : "
00035     << HOST_OS << "\n";
00036   r << "  Vendor           : "
00037     << HOST_VENDOR << "\n";
00038   r << "  Processor        : "
00039     << HOST_CPU << "\n";
00040   r << "  Crash date       : "
00041     << var_eval_system ("date") << "\n";
00042   return r;
00043 }
00044 
00045 string
00046 path_as_string (path p) {
00047   if (is_nil (p)) return "[]";
00048   string r= "[ ";
00049   r << as_string (p->item);
00050   p= p->next;
00051   while (!is_nil (p)) {
00052     r << ", " << as_string (p->item);
00053     p= p->next;
00054   }
00055   r << " ]";
00056   return r;
00057 }
00058 
00059 string
00060 get_editor_status_report () {
00061   string r;
00062   r << "Editor status:\n";
00063   server sv= get_server ();
00064   editor ed= sv -> get_editor ();
00065   path start_p, end_p;
00066   ed->get_selection (start_p, end_p);
00067   selection sel;
00068   ed->selection_get (sel);
00069   r << "  Root path          : "
00070     << path_as_string (ed->rp) << "\n"
00071     << "  Current path       : "
00072     << path_as_string (ed->the_path ()) << "\n"
00073     << "  Shifted path       : "
00074     << path_as_string (ed->the_shifted_path ()) << "\n"
00075     << "  Physical selection : "
00076     << path_as_string (start_p) << " -- "
00077     << path_as_string (end_p) << "\n"
00078     << "  Logical selection  : "
00079     << path_as_string (sel->start) << " -- "
00080     << path_as_string (sel->end) << "\n";
00081   return r;
00082 }
00083 
00084 void
00085 tree_report (string& s, tree t, path p, int indent) {
00086   for (int i=0; i<indent; i++) s << " ";
00087   if (is_atomic (t)) {
00088     s << raw_quote (t->label);
00089     s << " -- " << path_as_string (p) << "\n";
00090   }
00091   else {
00092     s << as_string (L(t));
00093     s << " -- " << path_as_string (p) << "\n";
00094     for (int i=0; i<N(t); i++)
00095       tree_report (s, t[i], p * i, indent+2);
00096   }
00097 }
00098 
00099 string
00100   tree_report (tree t, path p) {
00101   string s;
00102   tree_report (s, t, p, 0);
00103   return s;
00104 }
00105 
00106 /******************************************************************************
00107 * Crash management
00108 ******************************************************************************/
00109 
00110 string
00111 get_crash_report (const char* msg) {
00112   string r;
00113   r << "Error message:\n  " << msg << "\n"
00114     << "\n" << get_system_information ()
00115     << "\n" << get_editor_status_report ()
00116     << "\n" << get_stacktrace ();
00117   return r;
00118 }
00119 
00120 void
00121 tm_failure (const char* msg) {
00122   if (rescue_mode) {
00123     fprintf (stderr, "\nTeXmacs] Fatal unrecoverable error, %s\n", msg);
00124     exit (1);
00125   }
00126   rescue_mode= true;
00127   cerr << "\nTeXmacs] Fatal error, " << msg << "\n";
00128 
00129   string report= get_crash_report (msg);
00130   url dir ("$TEXMACS_HOME_PATH/system/crash");
00131   url err= url_numbered (dir, "crash_report_", "");
00132   if (!save_string (err, report))
00133     cerr << "TeXmacs] Crash report saved in " << err << "\n";
00134   else
00135     cerr << "TeXmacs] Crash report could not be saved in "
00136          << err << "\n"
00137          << "TeXmacs] Dumping report below\n\n"
00138          << report << "\n";
00139 
00140   server sv= get_server ();
00141   editor ed= sv -> get_editor ();
00142   string buf= tree_report (subtree (the_et, ed->rp), ed->rp);
00143   url buf_err= glue (err, "_tree");
00144   if (!save_string (buf_err, buf))
00145     cerr << "TeXmacs] Current buffer report saved in " << buf_err << "\n";
00146   else
00147     cerr << "TeXmacs] Current buffer report could not be saved in "
00148          << buf_err << "\n"
00149          << "TeXmacs] Dumping report below\n\n"
00150          << buf << "\n";
00151 
00152   auto_save ();
00153   close_all_pipes ();
00154   call ("quit-TeXmacs-scheme");
00155   clear_pending_commands ();
00156   //exit (1);
00157 }