GNU TeXmacsUser manualReferenceAboutEnglish
The main program and crash handling

This page describes

Texmacs/Texmacs/texmacs.cpp

, which contains the main program, and

Texmacs/Server/tm_debug.cpp

, which handles fatal errors. The platform specific main functions (

Plugins/Unix/unix_entrypoint.cpp

,

Plugins/Windows/windows32_entrypoint.cpp

,

Plugins/Windows64/windows64_entrypoint.cpp

and, for Android,

src/packages/android/launcher/main.cpp

) prepare the environment and the arguments (the TeXmacs path, the AppImage search path and the Qt platform on Unix, conversion of the arguments to UTF-8 on Windows) and then call texmacs_entrypoint.

1.The startup sequence

Startup happens in two stages. The first stage, texmacs_entrypoint (argc, argv), runs before the Scheme interpreter exists:

  1. immediate_options sets TEXMACS_HOME_PATH if it is not set (~/.TeXmacs, or the platform equivalent) and handles the options which must act before anything is loaded: the cache and setup removal options, -headless, -open and -log-file (see below). It also sets TEXMACS_SERVER_CERT_DIR.

  2. Resource limits are raised (the stack size if STACK_SIZE is defined; on macOS, the number of open files in boot_hacks), automatic widget refreshes are disabled until further notice (windows_delayed_refresh), the paths are initialized (TeXmacs_init_paths, which also fixes the environment of application bundles on macOS, Windows and Haiku) and the user preferences are loaded (load_user_preferences).

  3. The Qt application object is created: a QTMApplication normally, a QTMCoreApplication without any GUI in headless mode. The gui scaling preference is applied first, through QT_SCALE_FACTOR.

  4. In -open mode (not on macOS; the option also sets headless mode), the file is sent to an already running instance if there is one (send_to_single_instance, Qt only), otherwise a new TeXmacs is started on it with execl; in both cases the current process exits.

  5. The fonts are initialized, the global edit tree is created with its root ip_observer, the caches are initialized (cache_initialize), and init_texmacs (

    System/Boot/init_texmacs.cpp

    ) performs the remaining system initialization: the main paths, the user directories, the boot lock (if the lock file of a previous run is still there, that run crashed during startup, and the settings and caches are reset; the lock file itself stays until release_boot_lock), the succession status table, the standard DRD, the preferences again, the paths of the Scheme interpreter, the environment variables used by plug-ins and some miscellaneous and deprecated settings.

  6. start_scheme (argc, argv, TeXmacs_main) calls TeXmacs_main. With Guile it does so through scm_boot_guile (or gh_enter), so that TeXmacs_main runs inside the Guile runtime; with TinyScheme it just calls it. The interpreter state itself (bootstrap code, types and glue) is set up later, by initialize_scheme in the constructor of the server.

The second stage, TeXmacs_main (argc, argv), runs inside the interpreter:

  1. set_global_options parses the remaining command line options and reads a few preferences which must be known before the first window is built (native menu bar, mini bars).

  2. init_plugins installs the plug-ins, gui_open opens the display and the default font is set.

  3. The server is constructed (server sv), which boots the Scheme side; see the server classes.

  4. On the first run after an installation or an upgrade (install_status), a command which loads the welcome message or the list of recent changes is added.

  5. If no buffer has been opened yet, an empty window is opened (open_window). The comment in the code notes that this test is always true at this point, since files given on the command line are only loaded later.

  6. A delayed command which gives the keyboard focus to the canvas is added, texmacs_started is set, the signal handlers are installed (segmentation faults go to the crash handler unless -disable-error-recovery was given; SIGTERM exits at once; SIGPIPE is ignored, so that a client closing a socket does not kill the process), the TeXmacs server for remote clients is started if requested (-server, and only if server_can_start ()), and the boot lock is released.

  7. The collected startup commands are scheduled with exec_delayed and the GUI event loop is entered (gui_start_loop). Normally the program leaves the loop only through quit, which exits the process directly.

2.Startup commands

Most command line options do not act directly but produce Scheme commands, collected in two strings:

my_init_cmds

Filled by -x, -q, -c/-C, -W and -U; in headless mode without -server, (quit-TeXmacs) is appended (unless the option -X was given, which sets exec_exit to false). These commands are wrapped in a single begin and scheduled by the constructor of tm_server_rep, right after

init-texmacs.scm

and my-init-texmacs.scm have been loaded.

extra_init_cmd

Filled with a load-buffer for each file named on the command line (the first one in the current window, the next ones with :new-window), the welcome or upgrade message, -build-manual, -reference-suite and -test-suite. These commands are scheduled by TeXmacs_main just before entering the event loop.

The delayed commands are executed by the event loop in the order in which they were scheduled (exec_pending in

Plugins/Qt/qt_gui.cpp

processes a FIFO queue). This has two consequences:

3.Command line options

The options are recognized with one or two leading dashes. The main ones are:

Information

-h (help, also any unknown option), -v (version), -p, -hp, -bp (print the TeXmacs path, home path and binary path).

Messages and debugging

-s (silent), -V (verbose), -d (debug), the -debug-kind family (events, io, sockets, gnutls, bench, history, qt, qt-widgets, keyboard, packrat, flatten, parser, correct, convert, remote, live, and all, which only turns on events, std, io, history, bench, qt and qt-widgets), -disable-error-recovery, -log-file file.

Initialization

-i file (replaces

init-texmacs.scm

), -b file (replaces

init-buffer.scm

), -x cmd (execute a Scheme command), -q (append (quit-TeXmacs) at this point of the command line), -X (do not quit automatically in headless mode).

Display

-g wxh+x+y (geometry of the first window), -fn font (default font), -r (reverse video), -Oc/+Oc (clipping of TeX bitmap characters) and, for Qt 5 and earlier, the -retina options.

Batch processing

-headless or -H (no GUI), -c or -C in out (convert a file, through load-buffer and export-buffer), -W and -U in out (build or update a web site), -build-manual, -reference-suite, -test-suite. In the Qt version, -C, -W/-build-website and -U/-update-website imply headless mode (but not the lower case -c).

Maintenance

-S or -setup (forget the settings and caches), -delete-cache and the more specific -delete-kind-cache options, -delete-server-data, -delete-databases.

Server and instances

-server, -port n, -reset-server-preferences, -reset-admin-password, -tls-no-verify (see collaboration), and -open file (open in a running instance).

Several of these options are seen twice: by immediate_options and by set_global_options. For most of them (the cache options, -headless, -log-file) the first does the work and the second skips them; for -C, -W and -U the first sets headless mode and the second builds the command. On macOS, set_global_options skips -open but not its argument, which is then loaded as an ordinary file. A new option which must act before the preferences or the GUI exist belongs in immediate_options; all others belong in set_global_options and should, when possible, produce a Scheme command rather than act directly.

4.Global flags

texmacs.cpp

defines a few global flags: headless_mode (read through is_headless () or directly as an extern, for instance by the interpose handler, which skips all screen updates in this mode), tls_no_verify (is_tls_no_verify ()) and disable_error_recovery (used only in

texmacs.cpp

).

tm_server.cpp

defines texmacs_started, which is set just before the event loop starts and is used by the wait handler to decide whether a window can be used.

5.Fatal errors and crash handling

Internal errors are signalled with the macros ASSERT (cond, msg) and FAILED (msg) of

Kernel/Abstractions/basic.hpp

. What they do depends on USE_EXCEPTIONS, which that header currently always defines.

With exceptions (the default).
Both macros call tm_throw (msg) (
Kernel/Abstractions/basic.cpp

). It stores the message in the_exception, builds a crash report with get_crash_report (see below), prints it on the console and throws the message as a string. The exception is caught at the following places:

The editor and protected_call sites call handle_exceptions () right after catching; it prints the message and the report as an error (and so in the error console of TeXmacs) and clears the_exception. The Qt sites only store the message in the_exception, which is reported by the next call of handle_exceptions. In all cases the program goes on with the next event and nothing is saved.

Without exceptions.
If USE_EXCEPTIONS is not defined, the macros call tm_failure (msg) (
Texmacs/Server/tm_debug.cpp

), followed by assert when DEBUG_ASSERT is defined (which the CMake build does). tm_failure tries to save as much as possible:

  1. It sets the global rescue_mode (tested with in_rescue_mode ()). A second fatal error while in rescue mode exits at once (or returns, with DEBUG_ASSERT).

  2. It writes the crash report to $TEXMACS_HOME_PATH/system/crash/crash_report_n, or prints it if this fails.

  3. It writes the tree of the current buffer, with the path of each node, to a file with the same name followed by _tree (tree_report).

  4. It autosaves all buffers (autosave-all), closes all plug-in pipes, runs the Scheme exit hook and clears the pending commands.

tm_failure does not exit by itself; it returns to the assert (which aborts) or to the code which raised the error. Note that it calls get_server and get_current_editor without checking that they exist, so an error before the first view exists fails a second time; rescue_mode stops the recursion.

The crash report.
get_crash_report (msg) concatenates the message, the system information (get_system_information: version, build user and date, host, current date), the editor status (get_editor_status_report: root path, cursor path, shifted path and selection of the current editor) and a stack trace. get_editor_status_report guards against being called before the server or the first view exist, since an error at that stage would otherwise cause a second failure while the report is built.
Signals.
A segmentation fault is caught by clean_exit_on_segfault (unless -disable-error-recovery was given), which calls FAILED ("segmentation fault"). With exceptions, this throws from inside a signal handler, which is not guaranteed to work and should not be relied on: after a segmentation fault the state of the program is unknown anyway. SIGTERM exits immediately without saving.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".