main.c 8.5 KB
Newer Older
Christian Hergert's avatar
Christian Hergert committed
1 2
/* main.c
 *
3
 * Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
Christian Hergert's avatar
Christian Hergert committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
Christian Hergert's avatar
Christian Hergert committed
19 20
 */

21
#define G_LOG_DOMAIN "main"
22

23 24 25
#include "config.h"

#include <girepository.h>
26
#include <glib/gi18n.h>
27
#include <gtksourceview/gtksource.h>
28 29 30 31 32 33
#include <libide-core.h>
#include <libide-code.h>
#include <libide-editor.h>
#include <libide-greeter.h>
#include <libide-gui.h>
#include <libide-threading.h>
34
#include <locale.h>
35 36 37 38 39
#ifdef ENABLE_TRACING_SYSCAP
# include <sysprof-capture.h>
#endif
#include <sched.h>
#include <unistd.h>
40

41 42 43
#include "ide-application-private.h"
#include "ide-thread-private.h"
#include "ide-terminal-private.h"
44
#include "ide-private.h"
45

46
#include "bug-buddy.h"
Christian Hergert's avatar
Christian Hergert committed
47

48 49
#ifdef ENABLE_TRACING_SYSCAP
static SysprofCaptureWriter *trace_writer;
50
static G_LOCK_DEFINE (tracer);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

static void
trace_load (void)
{
  sysprof_clock_init ();
  trace_writer = sysprof_capture_writer_new_from_env (0);
}

static void
trace_unload (void)
{
  if (trace_writer)
    {
      sysprof_capture_writer_flush (trace_writer);
      g_clear_pointer (&trace_writer, sysprof_capture_writer_unref);
    }
}

static void
trace_function (const gchar    *func,
                gint64          begin_time_usec,
                gint64          end_time_usec)
{
  if (trace_writer != NULL)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    {
      G_LOCK (tracer);
      sysprof_capture_writer_add_mark (trace_writer,
                                       begin_time_usec * 1000L,
                                       sched_getcpu (),
                                       getpid (),
                                       (end_time_usec - begin_time_usec) * 1000L,
                                       "tracing",
                                       "function",
                                       func);
      G_UNLOCK (tracer);
    }
}

static void
90 91 92
trace_log (GLogLevelFlags  log_level,
           const gchar    *domain,
           const gchar    *message)
93 94 95 96
{
  if (trace_writer != NULL)
    {
      G_LOCK (tracer);
97 98 99 100 101 102 103
      sysprof_capture_writer_add_log (trace_writer,
                                      SYSPROF_CAPTURE_CURRENT_TIME,
                                      sched_getcpu (),
                                      getpid (),
                                      log_level,
                                      domain,
                                      message);
104 105
      G_UNLOCK (tracer);
    }
106 107 108 109 110 111
}

static IdeTraceVTable trace_vtable = {
  trace_load,
  trace_unload,
  trace_function,
112
  trace_log,
113 114 115
};
#endif

116 117 118 119 120 121 122 123 124 125 126
static gboolean
verbose_cb (const gchar  *option_name,
            const gchar  *value,
            gpointer      data,
            GError      **error)
{
  ide_log_increase_verbosity ();
  return TRUE;
}

static void
127 128 129 130 131 132
early_params_check (gint       *argc,
                    gchar    ***argv,
                    gboolean   *standalone,
                    gchar     **type,
                    gchar     **plugin,
                    gchar     **dbus_address)
133
{
134
  g_autoptr(GOptionContext) context = NULL;
135
  g_autoptr(GOptionGroup) gir_group = NULL;
136
  GOptionEntry entries[] = {
137
    { "standalone", 's', 0, G_OPTION_ARG_NONE, standalone, N_("Run a new instance of Builder") },
138
    { "verbose", 'v', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, verbose_cb },
139 140 141
    { "plugin", 0, 0, G_OPTION_ARG_STRING, plugin },
    { "type", 0, 0, G_OPTION_ARG_STRING, type },
    { "dbus-address", 0, 0, G_OPTION_ARG_STRING, dbus_address },
142 143 144
    { NULL }
  };

145 146
  gir_group = g_irepository_get_option_group ();

147 148 149 150
  context = g_option_context_new (NULL);
  g_option_context_set_ignore_unknown_options (context, TRUE);
  g_option_context_set_help_enabled (context, FALSE);
  g_option_context_add_main_entries (context, entries, NULL);
151
  g_option_context_add_group (context, g_steal_pointer (&gir_group));
152 153 154
  g_option_context_parse (context, argc, argv, NULL);
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static gboolean
_home_contains_symlink (const gchar *path)
{
  g_autofree gchar *parent = NULL;

  if (g_file_test (path, G_FILE_TEST_IS_SYMLINK))
    return TRUE;

  if ((parent = g_path_get_dirname (path)) && !g_str_equal (parent, "/"))
    return _home_contains_symlink (parent);

  return FALSE;
}

static gboolean
home_contains_symlink (void)
{
  return _home_contains_symlink (g_get_home_dir ());
}

175 176 177
static gboolean
is_running_in_shell (void)
{
178 179 180 181 182 183 184 185 186
  const gchar *shlvl = g_getenv ("SHLVL");

  /* GNOME Shell, among other desktop shells may set SHLVL=0 to indicate
   * that we are not running within a shell. Use that before checking any
   * file-descriptors since it is more reliable.
   */
  if (ide_str_equal0 (shlvl, "0"))
    return FALSE;

187 188 189 190 191 192 193 194 195 196
  /* If stdin is not a TTY, then assume we have no access to communicate
   * with the user via console. We use stdin instead of stdout as a logging
   * system may have a PTY for stdout to get colorized output.
   */
  if (!isatty (STDIN_FILENO))
    return FALSE;

  return TRUE;
}

197 198 199
gint
main (gint   argc,
      gchar *argv[])
Christian Hergert's avatar
Christian Hergert committed
200
{
201 202 203
  g_autofree gchar *plugin = NULL;
  g_autofree gchar *type = NULL;
  g_autofree gchar *dbus_address = NULL;
204
  IdeApplication *app;
205
  const gchar *desktop;
206
  gboolean standalone = FALSE;
Christian Hergert's avatar
Christian Hergert committed
207 208
  int ret;

209
  /* Setup our gdb fork()/exec() helper if we're in a terminal */
210
  if (is_running_in_shell ())
211
    bug_buddy_init ();
212

Christian Hergert's avatar
Christian Hergert committed
213 214 215
  /* Always ignore SIGPIPE */
  signal (SIGPIPE, SIG_IGN);

216 217 218 219 220 221
  /* Set up gettext translations */
  setlocale (LC_ALL, "");
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);

222 223 224 225 226 227 228 229 230 231
  /* Setup various application name/id defaults. */
  g_set_prgname (ide_get_program_name ());
  g_set_application_name (_("Builder"));

#if 0
  /* TODO: allow support for parallel nightly install */
#ifdef DEVELOPMENT_BUILD
  ide_set_application_id ("org.gnome.Builder-Devel");
#endif
#endif
232

233 234 235 236
  /* Early init of logging so that we get messages in a consistent
   * format. If we deferred this to GApplication, we'd get them in
   * multiple formats.
   */
Christian Hergert's avatar
wip  
Christian Hergert committed
237
  ide_log_init (TRUE, NULL);
238

239 240
  /* Extract options like -vvvv */
  early_params_check (&argc, &argv, &standalone, &type, &plugin, &dbus_address);
241

242 243 244 245
  /* Log some info so it shows up in logs */
  g_message ("GNOME Builder %s starting with ABI %s",
             PACKAGE_VERSION, PACKAGE_ABI_S);

246 247 248 249 250 251 252 253 254
  /* Make sure $HOME is not a symlink, as that can cause issues with
   * various subsystems. Just warn super loud so that users find it
   * when trying to debug issues.
   *
   * Silverblue did this, but has since stopped (and some users will
   * lag behind until their systems are fixed).
   *
   * https://gitlab.gnome.org/GNOME/gnome-builder/issues/859
   */
255
  if (home_contains_symlink ())
256 257 258
    g_critical ("User home directory uses a symlink. "
                "This is not supported and may result in unforseen issues.");

259 260 261 262 263 264 265
  /* Log what desktop is being used to simplify tracking down
   * quirks in the future.
   */
  desktop = g_getenv ("XDG_CURRENT_DESKTOP");
  if (desktop == NULL)
    desktop = "unknown";

266 267 268 269
#ifdef ENABLE_TRACING_SYSCAP
  _ide_trace_init (&trace_vtable);
#endif

270
  g_message ("Initializing with %s desktop and GTK+ %d.%d.%d.",
271
             desktop,
272 273 274 275
             gtk_get_major_version (),
             gtk_get_minor_version (),
             gtk_get_micro_version ());

276 277
  gtk_source_init ();

278 279
  /* Initialize thread pools */
  _ide_thread_pool_init (FALSE);
280

281 282
  /* Guess the user shell early */
  _ide_guess_shell ();
283

284 285
  app = _ide_application_new (standalone, type, plugin, dbus_address);
  g_application_add_option_group (G_APPLICATION (app), g_irepository_get_option_group ());
286
  ret = g_application_run (G_APPLICATION (app), argc, argv);
287 288 289 290
  /* Force disposal of the application (to help catch cleanup
   * issues at shutdown) and then (hopefully) finalize the app.
   */
  g_object_run_dispose (G_OBJECT (app));
Christian Hergert's avatar
Christian Hergert committed
291 292
  g_clear_object (&app);

293
  /* Flush any outstanding logs */
Christian Hergert's avatar
wip  
Christian Hergert committed
294
  ide_log_shutdown ();
Christian Hergert's avatar
Christian Hergert committed
295

296
  /* Cleanup GtkSourceView singletons to improve valgrind output */
297
  gtk_source_finalize ();
298

299 300 301 302
#ifdef ENABLE_TRACING_SYSCAP
  _ide_trace_shutdown ();
#endif

Christian Hergert's avatar
Christian Hergert committed
303 304
  return ret;
}