File: foundry-debugger-actions.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (451 lines) | stat: -rw-r--r-- 14,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
106
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
202
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* foundry-debugger-actions.c
 *
 * Copyright 2025 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library 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
 * Lesser 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/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "foundry-debugger.h"
#include "foundry-debugger-actions.h"
#include "foundry-debugger-thread.h"
#include "foundry-util.h"

#include "eggactiongroup.h"

/**
 * FoundryDebuggerActions:
 *
 * Simplified interface for performing debugger actions.
 *
 * This [iface@Gio.ActionGroup] provides actions and mechanics for writing
 * a debugger application. It does a light amount of state management to
 * ensure actions are enabled/disabled properly.
 */

struct _FoundryDebuggerActions
{
  GObject                parent_instance;
  FoundryDebugger       *debugger;
  FoundryDebuggerThread *thread;
  gulong                 thread_changed_id;
  gulong                 notify_terminated_id;
};

enum {
  PROP_0,
  PROP_DEBUGGER,
  PROP_THREAD,
  N_PROPS
};

static GParamSpec *properties[N_PROPS];

static void
do_move (FoundryDebuggerActions  *self,
         FoundryDebuggerMovement  movement)
{
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

  if (self->thread != NULL)
    dex_future_disown (foundry_debugger_thread_move (self->thread, movement));
  else if (self->debugger != NULL)
    dex_future_disown (foundry_debugger_move (self->debugger, movement));

  G_GNUC_END_IGNORE_DEPRECATIONS
}

static void
foundry_debugger_actions_continue_action (FoundryDebuggerActions *self,
                                          GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  do_move (self, FOUNDRY_DEBUGGER_MOVEMENT_CONTINUE);
}

static void
foundry_debugger_actions_step_in_action (FoundryDebuggerActions *self,
                                         GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  do_move (self, FOUNDRY_DEBUGGER_MOVEMENT_STEP_IN);
}

static void
foundry_debugger_actions_step_out_action (FoundryDebuggerActions *self,
                                          GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  do_move (self, FOUNDRY_DEBUGGER_MOVEMENT_STEP_OUT);
}

static void
foundry_debugger_actions_step_over_action (FoundryDebuggerActions *self,
                                           GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  do_move (self, FOUNDRY_DEBUGGER_MOVEMENT_STEP_OVER);
}

static void
foundry_debugger_actions_interrupt_action (FoundryDebuggerActions *self,
                                           GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

  if (self->thread != NULL)
    dex_future_disown (foundry_debugger_thread_interrupt (self->thread));
  else if (self->debugger != NULL)
    dex_future_disown (foundry_debugger_interrupt (self->debugger));

  G_GNUC_END_IGNORE_DEPRECATIONS
}

static void
foundry_debugger_actions_stop (FoundryDebuggerActions *self,
                               GVariant               *param)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  dex_future_disown (foundry_debugger_stop (self->debugger));
}

EGG_DEFINE_ACTION_GROUP (FoundryDebuggerActions, foundry_debugger_actions, {
  { "continue", foundry_debugger_actions_continue_action, NULL, NULL },
  { "step-in", foundry_debugger_actions_step_in_action, NULL, NULL },
  { "step-out", foundry_debugger_actions_step_out_action, NULL, NULL },
  { "step-over", foundry_debugger_actions_step_over_action, NULL, NULL },
  { "interrupt", foundry_debugger_actions_interrupt_action, NULL, NULL },
  { "stop", foundry_debugger_actions_stop, NULL, NULL },
})

G_DEFINE_FINAL_TYPE_WITH_CODE (FoundryDebuggerActions, foundry_debugger_actions, G_TYPE_OBJECT,
                                EGG_IMPLEMENT_ACTION_GROUP (foundry_debugger_actions))

static void
foundry_debugger_actions_update (FoundryDebuggerActions *self)
{
  gboolean is_stopped = TRUE;
  gboolean has_terminated = FALSE;
  gboolean can_continue = FALSE;
  gboolean can_step_in = FALSE;
  gboolean can_step_out = FALSE;
  gboolean can_step_over = FALSE;

  g_assert (!self->thread || FOUNDRY_IS_DEBUGGER_THREAD (self->thread));
  g_assert (!self->debugger || FOUNDRY_IS_DEBUGGER (self->debugger));
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));

  if (self->debugger != NULL && self->thread != NULL)
    is_stopped = foundry_debugger_thread_is_stopped (self->thread);

  if (self->debugger != NULL)
    has_terminated = foundry_debugger_has_terminated (self->debugger);

  if (self->thread != NULL && !has_terminated && is_stopped)
    {
      can_continue = foundry_debugger_thread_can_move (self->thread, FOUNDRY_DEBUGGER_MOVEMENT_CONTINUE);
      can_step_in = foundry_debugger_thread_can_move (self->thread, FOUNDRY_DEBUGGER_MOVEMENT_STEP_IN);
      can_step_out = foundry_debugger_thread_can_move (self->thread, FOUNDRY_DEBUGGER_MOVEMENT_STEP_OUT);
      can_step_over = foundry_debugger_thread_can_move (self->thread, FOUNDRY_DEBUGGER_MOVEMENT_STEP_OVER);
    }

  foundry_debugger_actions_set_action_enabled (self, "continue", can_continue);
  foundry_debugger_actions_set_action_enabled (self, "step-in", can_step_in);
  foundry_debugger_actions_set_action_enabled (self, "step-out", can_step_out);
  foundry_debugger_actions_set_action_enabled (self, "step-over", can_step_over);
  foundry_debugger_actions_set_action_enabled (self, "interrupt", !has_terminated && !is_stopped);
  foundry_debugger_actions_set_action_enabled (self, "stop", !has_terminated);
}

static void
foundry_debugger_actions_thread_changed_cb (FoundryDebuggerActions *self,
                                            GParamSpec             *pspec,
                                            FoundryDebuggerThread  *thread)
{
  g_assert (FOUNDRY_IS_DEBUGGER_ACTIONS (self));
  g_assert (FOUNDRY_IS_DEBUGGER_THREAD (thread));

  foundry_debugger_actions_update (self);
}

static void
foundry_debugger_actions_constructed (GObject *object)
{
  FoundryDebuggerActions *self = (FoundryDebuggerActions *)object;

  G_OBJECT_CLASS (foundry_debugger_actions_parent_class)->constructed (object);

  if (self->debugger != NULL)
    g_signal_connect_object (self->debugger,
                             "notify::terminated",
                             G_CALLBACK (foundry_debugger_actions_update),
                             self,
                             G_CONNECT_SWAPPED);
}

static void
foundry_debugger_actions_dispose (GObject *object)
{
  FoundryDebuggerActions *self = FOUNDRY_DEBUGGER_ACTIONS (object);

  g_clear_signal_handler (&self->thread_changed_id, self->thread);
  g_clear_signal_handler (&self->notify_terminated_id, self->debugger);

  g_clear_object (&self->thread);
  g_clear_object (&self->debugger);

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

static void
foundry_debugger_actions_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  FoundryDebuggerActions *self = FOUNDRY_DEBUGGER_ACTIONS (object);

  switch (prop_id)
    {
    case PROP_DEBUGGER:
      g_value_take_object (value, foundry_debugger_actions_dup_debugger (self));
      break;

    case PROP_THREAD:
      g_value_take_object (value, foundry_debugger_actions_dup_thread (self));
      break;

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

static void
foundry_debugger_actions_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
  FoundryDebuggerActions *self = FOUNDRY_DEBUGGER_ACTIONS (object);

  switch (prop_id)
    {
    case PROP_DEBUGGER:
      foundry_debugger_actions_set_debugger (self, g_value_get_object (value));
      break;

    case PROP_THREAD:
      foundry_debugger_actions_set_thread (self, g_value_get_object (value));
      break;

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

static void
foundry_debugger_actions_class_init (FoundryDebuggerActionsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = foundry_debugger_actions_constructed;
  object_class->get_property = foundry_debugger_actions_get_property;
  object_class->set_property = foundry_debugger_actions_set_property;
  object_class->dispose = foundry_debugger_actions_dispose;

  /**
   * FoundryDebuggerActions:debugger:
   *
   * The debugger instance.
   *
   * Since: 1.1
   */
  properties[PROP_DEBUGGER] =
    g_param_spec_object ("debugger", NULL, NULL,
                         FOUNDRY_TYPE_DEBUGGER,
                         (G_PARAM_READWRITE |
                          G_PARAM_EXPLICIT_NOTIFY |
                          G_PARAM_STATIC_STRINGS));

  /**
   * FoundryDebuggerActions:thread:
   *
   * The debugger thread.
   *
   * Since: 1.1
   */
  properties[PROP_THREAD] =
    g_param_spec_object ("thread", NULL, NULL,
                         FOUNDRY_TYPE_DEBUGGER_THREAD,
                         (G_PARAM_READWRITE |
                          G_PARAM_EXPLICIT_NOTIFY |
                          G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
foundry_debugger_actions_init (FoundryDebuggerActions *self)
{
}

/**
 * foundry_debugger_actions_new:
 * @debugger: (nullable): a [class@Foundry.Debugger]
 * @thread: (nullable): a [class@Foundry.DebuggerThread]
 *
 * Creates a new [class@Foundry.DebuggerActions] instance.
 *
 * Returns: (transfer full): a new [class@Foundry.DebuggerActions]
 *
 * Since: 1.1
 */
FoundryDebuggerActions *
foundry_debugger_actions_new (FoundryDebugger       *debugger,
                              FoundryDebuggerThread *thread)
{
  g_return_val_if_fail (!debugger || FOUNDRY_IS_DEBUGGER (debugger), NULL);
  g_return_val_if_fail (!thread || FOUNDRY_IS_DEBUGGER_THREAD (thread), NULL);

  return g_object_new (FOUNDRY_TYPE_DEBUGGER_ACTIONS,
                       "debugger", debugger,
                       "thread", thread,
                       NULL);
}

/**
 * foundry_debugger_actions_dup_debugger:
 * @self: a [class@Foundry.DebuggerActions]
 *
 * Gets the debugger instance.
 *
 * Returns: (transfer full) (nullable): the debugger instance
 *
 * Since: 1.1
 */
FoundryDebugger *
foundry_debugger_actions_dup_debugger (FoundryDebuggerActions *self)
{
  g_return_val_if_fail (FOUNDRY_IS_DEBUGGER_ACTIONS (self), NULL);

  return self->debugger ? g_object_ref (self->debugger) : NULL;
}

/**
 * foundry_debugger_actions_set_debugger:
 * @self: a [class@Foundry.DebuggerActions]
 * @debugger: (nullable): the debugger instance
 *
 * Sets the debugger instance.
 *
 * Since: 1.1
 */
void
foundry_debugger_actions_set_debugger (FoundryDebuggerActions *self,
                                       FoundryDebugger        *debugger)
{
  g_return_if_fail (FOUNDRY_IS_DEBUGGER_ACTIONS (self));
  g_return_if_fail (debugger == NULL || FOUNDRY_IS_DEBUGGER (debugger));

  if (self->debugger == debugger)
    return;

  if (debugger != NULL)
    g_object_ref (debugger);

  g_clear_signal_handler (&self->notify_terminated_id, self->debugger);

  g_clear_object (&self->debugger);

  self->debugger = debugger;

  if (debugger != NULL)
    self->notify_terminated_id =
      g_signal_connect_object (debugger,
                               "notify::terminated",
                               G_CALLBACK (foundry_debugger_actions_update),
                               self,
                               G_CONNECT_SWAPPED);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DEBUGGER]);

  foundry_debugger_actions_update (self);
}

/**
 * foundry_debugger_actions_dup_thread:
 * @self: a [class@Foundry.DebuggerActions]
 *
 * Gets the debugger thread.
 *
 * Returns: (transfer full) (nullable): the debugger thread
 *
 * Since: 1.1
 */
FoundryDebuggerThread *
foundry_debugger_actions_dup_thread (FoundryDebuggerActions *self)
{
  g_return_val_if_fail (FOUNDRY_IS_DEBUGGER_ACTIONS (self), NULL);

  return self->thread ? g_object_ref (self->thread) : NULL;
}

/**
 * foundry_debugger_actions_set_thread:
 * @self: a [class@Foundry.DebuggerActions]
 * @thread: (nullable): the debugger thread
 *
 * Sets the debugger thread and connects to its "changed" signal
 * to update action states.
 *
 * Since: 1.1
 */
void
foundry_debugger_actions_set_thread (FoundryDebuggerActions *self,
                                     FoundryDebuggerThread  *thread)
{
  g_return_if_fail (FOUNDRY_IS_DEBUGGER_ACTIONS (self));
  g_return_if_fail (thread == NULL || FOUNDRY_IS_DEBUGGER_THREAD (thread));

  if (self->thread == thread)
    return;

  if (thread)
    g_object_ref (thread);

  g_clear_signal_handler (&self->thread_changed_id, self->thread);
  g_clear_object (&self->thread);
  self->thread = thread;

  if (self->thread != NULL)
    self->thread_changed_id = g_signal_connect_object (self->thread,
                                                       "notify::stopped",
                                                       G_CALLBACK (foundry_debugger_actions_thread_changed_cb),
                                                       self,
                                                       G_CONNECT_SWAPPED);

  foundry_debugger_actions_update (self);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_THREAD]);
}