ide-debugger-breakpoints.c 10.9 KB
Newer Older
1 2
/* ide-debugger-breakpoints.c
 *
3
 * Copyright 2017-2019 Christian Hergert <chergert@redhat.com>
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
19 20 21 22
 */

#define G_LOG_DOMAIN "ide-debugger-breakpoints"

23 24
#include "config.h"

25 26
#include <stdlib.h>

27 28
#include "ide-debugger-breakpoints.h"
#include "ide-debugger-private.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * SECTION:ide-debugger-breakpoints
 * @title: IdeDebuggerBreakpoints
 * @short_title: A collection of breakpoints for a file
 *
 * The #IdeDebuggerBreakpoints provides a convenient container for breakpoints
 * about a single file. This is useful for situations like the document gutter
 * where we need very fast access to whether or not a line has a breakpoint set
 * during the rendering process.
 *
 * At it's core, this is a sparse array as rarely do we have more than one
 * cacheline of information about breakpoints in a file.
 *
 * This object is controled by the IdeDebuggerManager and will modify the
 * breakpoints as necessary by the current debugger. If no debugger is
 * active, the breakpoints are queued until the debugger has started, and
 * then synchronized to the debugger process.
47 48
 *
 * Since: 3.32
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 */

typedef struct
{
  guint                  line;
  IdeDebuggerBreakMode   mode;
  IdeDebuggerBreakpoint *breakpoint;
} LineInfo;

struct _IdeDebuggerBreakpoints
{
  GObject parent_instance;
  GArray *lines;
  GFile *file;
};

enum {
  PROP_0,
  PROP_FILE,
  N_PROPS
};

enum {
  CHANGED,
  N_SIGNALS
};

G_DEFINE_TYPE (IdeDebuggerBreakpoints, ide_debugger_breakpoints, G_TYPE_OBJECT)

static GParamSpec *properties [N_PROPS];
static guint signals [N_SIGNALS];

static void
line_info_clear (gpointer data)
{
  LineInfo *info = data;

  info->line = 0;
  info->mode = 0;
  g_clear_object (&info->breakpoint);
}

static gint
line_info_compare (gconstpointer a,
                   gconstpointer b)
{
  const LineInfo *lia = a;
  const LineInfo *lib = b;

  return (gint)lia->line - (gint)lib->line;
}

static void
ide_debugger_breakpoints_dispose (GObject *object)
{
  IdeDebuggerBreakpoints *self = (IdeDebuggerBreakpoints *)object;

106
  g_clear_pointer (&self->lines, g_array_unref);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  G_OBJECT_CLASS (ide_debugger_breakpoints_parent_class)->dispose (object);
}

static void
ide_debugger_breakpoints_finalize (GObject *object)
{
  IdeDebuggerBreakpoints *self = (IdeDebuggerBreakpoints *)object;

  g_clear_object (&self->file);

  G_OBJECT_CLASS (ide_debugger_breakpoints_parent_class)->finalize (object);
}

static void
ide_debugger_breakpoints_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  IdeDebuggerBreakpoints *self = IDE_DEBUGGER_BREAKPOINTS (object);

  switch (prop_id)
    {
    case PROP_FILE:
      g_value_set_object (value, self->file);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
ide_debugger_breakpoints_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
  IdeDebuggerBreakpoints *self = IDE_DEBUGGER_BREAKPOINTS (object);

  switch (prop_id)
    {
    case PROP_FILE:
      self->file = g_value_dup_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
ide_debugger_breakpoints_class_init (IdeDebuggerBreakpointsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = ide_debugger_breakpoints_dispose;
  object_class->finalize = ide_debugger_breakpoints_finalize;
  object_class->get_property = ide_debugger_breakpoints_get_property;
  object_class->set_property = ide_debugger_breakpoints_set_property;

  properties [PROP_FILE] =
    g_param_spec_object ("file",
                         "File",
                         "The file for the breakpoints",
                         G_TYPE_FILE,
                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, N_PROPS, properties);

  signals [CHANGED] =
    g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
}

static void
ide_debugger_breakpoints_init (IdeDebuggerBreakpoints *self)
{
}

/**
 * ide_debugger_breakpoints_get_line:
 * @self: An #IdeDebuggerBreakpoints
 * @line: The line number
 *
 * Gets the breakpoint that has been registered at a given line, or %NULL
 * if no breakpoint is registered there.
 *
 * Returns: (nullable) (transfer none): An #IdeDebuggerBreakpoint or %NULL
 *
202
 * Since: 3.32
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
 */
IdeDebuggerBreakpoint *
ide_debugger_breakpoints_get_line (IdeDebuggerBreakpoints *self,
                                   guint                   line)
{
  g_return_val_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self), NULL);

  if (self->lines != NULL)
    {
      LineInfo info = { line, 0 };
      LineInfo *ret;

      ret = bsearch (&info, (gpointer)self->lines->data,
                     self->lines->len, sizeof (LineInfo),
                     line_info_compare);

      if (ret)
        return ret->breakpoint;
    }

  return NULL;
}

IdeDebuggerBreakMode
ide_debugger_breakpoints_get_line_mode (IdeDebuggerBreakpoints *self,
                                        guint                   line)
{
  g_return_val_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self), 0);

  if (self->lines != NULL)
    {
      LineInfo info = { line, 0 };
      LineInfo *ret;

      ret = bsearch (&info, (gpointer)self->lines->data,
                     self->lines->len, sizeof (LineInfo),
                     line_info_compare);

      if (ret)
        return ret->mode;
    }

  return 0;
}

static void
ide_debugger_breakpoints_set_line (IdeDebuggerBreakpoints *self,
                                   guint                   line,
                                   IdeDebuggerBreakMode    mode,
                                   IdeDebuggerBreakpoint  *breakpoint)
{
  LineInfo info;

  g_assert (IDE_IS_DEBUGGER_BREAKPOINTS (self));
  g_assert (IDE_IS_DEBUGGER_BREAK_MODE (mode));
  g_assert (!breakpoint || IDE_IS_DEBUGGER_BREAKPOINT (breakpoint));
  g_assert (mode == IDE_DEBUGGER_BREAK_NONE || breakpoint != NULL);

  if (self->lines != NULL)
    {
      for (guint i = 0; i < self->lines->len; i++)
        {
          LineInfo *ele = &g_array_index (self->lines, LineInfo, i);

          if (ele->line == line)
            {
              if (mode != IDE_DEBUGGER_BREAK_NONE)
                {
                  ele->mode = mode;
                  g_set_object (&ele->breakpoint, breakpoint);
                }
              else
                g_array_remove_index (self->lines, i);

              goto emit_signal;
            }
        }
    }

  /* Nothing to do here */
  if (mode == IDE_DEBUGGER_BREAK_NONE)
    return;

  if (self->lines == NULL)
    {
      self->lines = g_array_new (FALSE, FALSE, sizeof (LineInfo));
      g_array_set_clear_func (self->lines, line_info_clear);
    }

  info.line = line;
  info.mode = mode;
  info.breakpoint = g_object_ref (breakpoint);

  g_array_append_val (self->lines, info);
  g_array_sort (self->lines, line_info_compare);

emit_signal:
  g_signal_emit (self, signals [CHANGED], 0);
}

void
_ide_debugger_breakpoints_add (IdeDebuggerBreakpoints *self,
                               IdeDebuggerBreakpoint  *breakpoint)
{
  IdeDebuggerBreakMode mode;
  guint line;

  IDE_ENTRY;

  g_return_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self));
  g_return_if_fail (IDE_IS_DEBUGGER_BREAKPOINT (breakpoint));

  line = ide_debugger_breakpoint_get_line (breakpoint);
  mode = ide_debugger_breakpoint_get_mode (breakpoint);

  IDE_TRACE_MSG ("tracking breakpoint at line %d [breakpoints=%p]",
                 line, self);

  ide_debugger_breakpoints_set_line (self, line, mode, breakpoint);

  IDE_EXIT;
}

void
_ide_debugger_breakpoints_remove (IdeDebuggerBreakpoints *self,
                                  IdeDebuggerBreakpoint  *breakpoint)
{
  guint line;

  IDE_ENTRY;

  g_return_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self));
  g_return_if_fail (IDE_IS_DEBUGGER_BREAKPOINT (breakpoint));

  line = ide_debugger_breakpoint_get_line (breakpoint);

  IDE_TRACE_MSG ("removing breakpoint at line %d [breakpoints=%p]",
                 line, self);

  if (self->lines != NULL)
    {
      /* First try to get things by pointer address to reduce chances
       * of removing the wrong breakpoint from the collection.
       */
      for (guint i = 0; i < self->lines->len; i++)
        {
          const LineInfo *info = &g_array_index (self->lines, LineInfo, i);

          if (ide_debugger_breakpoint_compare (breakpoint, info->breakpoint) == 0)
            {
              g_array_remove_index (self->lines, i);
              g_signal_emit (self, signals [CHANGED], 0);
              IDE_EXIT;
            }
        }

      ide_debugger_breakpoints_set_line (self, line, IDE_DEBUGGER_BREAK_NONE, NULL);
    }

  IDE_EXIT;
}

/**
 * ide_debugger_breakpoints_get_file:
 * @self: An #IdeDebuggerBreakpoints
 *
 * Gets the "file" property, which is the file that breakpoints within
 * this container belong to.
 *
372
 * Returns: (transfer none): a #GFile
373 374
 *
 * Since: 3.32
375 376 377 378 379 380 381 382 383 384 385 386
 */
GFile *
ide_debugger_breakpoints_get_file (IdeDebuggerBreakpoints *self)
{
  g_return_val_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self), NULL);

  return self->file;
}

/**
 * ide_debugger_breakpoints_foreach:
 * @self: a #IdeDebuggerBreakpoints
387
 * @func: (scope call) (closure user_data): a #GFunc to call
388 389 390 391
 * @user_data: user data for @func
 *
 * Call @func for every #IdeDebuggerBreakpoint in @self.
 *
392
 * Since: 3.32
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
 */
void
ide_debugger_breakpoints_foreach (IdeDebuggerBreakpoints *self,
                                  GFunc                   func,
                                  gpointer                user_data)
{
  g_return_if_fail (IDE_IS_DEBUGGER_BREAKPOINTS (self));
  g_return_if_fail (func != NULL);

  if (self->lines != NULL)
    {
      for (guint i = 0; i < self->lines->len; i++)
        {
          const LineInfo *info = &g_array_index (self->lines, LineInfo, i);

          if (info->breakpoint)
            func (info->breakpoint, user_data);
        }
    }
}