File: foundry-dap-debugger-thread.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 (238 lines) | stat: -rw-r--r-- 8,207 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
/* foundry-dap-debugger-thread.c
 *
 * Copyright 2025 Christian Hergert
 *
 * 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-dap-debugger-private.h"
#include "foundry-dap-debugger-thread-private.h"
#include "foundry-dap-debugger-stack-frame-private.h"
#include "foundry-dap-protocol.h"
#include "foundry-json-node.h"
#include "foundry-util.h"

struct _FoundryDapDebuggerThread
{
  FoundryDebuggerThread parent_instance;
  GWeakRef debugger_wr;
  gint64 id;
  guint stopped : 1;
};

G_DEFINE_FINAL_TYPE (FoundryDapDebuggerThread, foundry_dap_debugger_thread, FOUNDRY_TYPE_DEBUGGER_THREAD)

static char *
foundry_dap_debugger_thread_dup_id (FoundryDebuggerThread *thread)
{
  return g_strdup_printf ("%"G_GINT64_FORMAT, FOUNDRY_DAP_DEBUGGER_THREAD (thread)->id);
}

static DexFuture *
foundry_dap_debugger_thread_inflate_frames (DexFuture *future,
                                            gpointer   user_data)
{
  FoundryDapDebuggerThread *self = user_data;
  g_autoptr(FoundryDapDebugger) debugger = NULL;
  g_autoptr(GListStore) store = NULL;
  g_autoptr(JsonNode) node = NULL;
  JsonNode *stack_frames = NULL;
  JsonArray *ar;
  guint length;

  g_assert (DEX_IS_FUTURE (future));
  g_assert (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self));

  if (!(node = dex_await_boxed (dex_ref (future), NULL)))
    return dex_ref (future);

  if (!(debugger = g_weak_ref_get (&self->debugger_wr)))
    return foundry_future_new_disposed ();

  if (!FOUNDRY_JSON_OBJECT_PARSE (node,
                                  "body", "{",
                                    "stackFrames", FOUNDRY_JSON_NODE_GET_NODE (&stack_frames),
                                  "}") ||
      !JSON_NODE_HOLDS_ARRAY (stack_frames))
    return foundry_future_new_not_supported ();

  ar = json_node_get_array (stack_frames);
  length = json_array_get_length (ar);
  store = g_list_store_new (FOUNDRY_TYPE_DEBUGGER_STACK_FRAME);

  for (guint i = 0; i < length; i++)
    {
      JsonNode *stack_frame = json_array_get_element (ar, i);
      g_autoptr(FoundryDebuggerStackFrame) item = NULL;

      if (!JSON_NODE_HOLDS_OBJECT (stack_frame))
        continue;

      if ((item = foundry_dap_debugger_stack_frame_new (debugger, stack_frame)))
        g_list_store_append (store, item);
    }

  return dex_future_new_take_object (g_steal_pointer (&store));
}

static DexFuture *
foundry_dap_debugger_thread_list_frames (FoundryDebuggerThread *thread)
{
  FoundryDapDebuggerThread *self = FOUNDRY_DAP_DEBUGGER_THREAD (thread);
  g_autoptr(FoundryDapDebugger) debugger = g_weak_ref_get (&self->debugger_wr);

  if (debugger == NULL)
    return foundry_future_new_disposed ();

  return dex_future_then (foundry_dap_debugger_call (debugger,
                                                     FOUNDRY_JSON_OBJECT_NEW ("type", "request",
                                                                              "command", "stackTrace",
                                                                              "arguments", "{",
                                                                                "threadId", FOUNDRY_JSON_NODE_PUT_INT (self->id),
                                                                              "}")),
                          foundry_dap_debugger_thread_inflate_frames,
                          g_object_ref (self),
                          g_object_unref);
}

static gboolean
foundry_dap_debugger_thread_is_stopped (FoundryDebuggerThread *thread)
{
  return FOUNDRY_DAP_DEBUGGER_THREAD (thread)->stopped;
}

static gboolean
foundry_dap_debugger_thread_can_move (FoundryDebuggerThread   *thread,
                                      FoundryDebuggerMovement  movement)
{
  FoundryDapDebuggerThread *self = (FoundryDapDebuggerThread *)thread;

  g_assert (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self));

  if (movement == FOUNDRY_DEBUGGER_MOVEMENT_START)
    return FALSE;

  return self->stopped;
}

static DexFuture *
foundry_dap_debugger_thread_move (FoundryDebuggerThread   *thread,
                                  FoundryDebuggerMovement  movement)
{
  FoundryDapDebuggerThread *self = (FoundryDapDebuggerThread *)thread;
  g_autoptr(FoundryDapDebugger) debugger = NULL;

  g_assert (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self));


  if ((debugger = g_weak_ref_get (&self->debugger_wr)))
    return _foundry_dap_debugger_move (debugger, self->id, movement);

  return foundry_future_new_disposed ();
}

static DexFuture *
foundry_dap_debugger_thread_interrupt (FoundryDebuggerThread *thread)
{
  FoundryDapDebuggerThread *self = (FoundryDapDebuggerThread *)thread;
  g_autoptr(FoundryDapDebugger) debugger = NULL;

  g_assert (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self));

  if ((debugger = g_weak_ref_get (&self->debugger_wr)))
    return dex_future_then (foundry_dap_debugger_call (debugger,
                                                       FOUNDRY_JSON_OBJECT_NEW ("type", "request",
                                                                                "command", "pause",
                                                                                "arguments", "{",
                                                                                "threadId", FOUNDRY_JSON_NODE_PUT_INT (self->id),
                                                                                "}")),
                            foundry_dap_protocol_unwrap_error,
                            NULL, NULL);

  return foundry_future_new_disposed ();
}

static void
foundry_dap_debugger_thread_finalize (GObject *object)
{
  FoundryDapDebuggerThread *self = (FoundryDapDebuggerThread *)object;

  g_weak_ref_clear (&self->debugger_wr);

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

static void
foundry_dap_debugger_thread_class_init (FoundryDapDebuggerThreadClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FoundryDebuggerThreadClass *thread_class = FOUNDRY_DEBUGGER_THREAD_CLASS (klass);

  object_class->finalize = foundry_dap_debugger_thread_finalize;

  thread_class->dup_id = foundry_dap_debugger_thread_dup_id;
  thread_class->list_frames = foundry_dap_debugger_thread_list_frames;
  thread_class->is_stopped = foundry_dap_debugger_thread_is_stopped;
  thread_class->move = foundry_dap_debugger_thread_move;
  thread_class->can_move = foundry_dap_debugger_thread_can_move;
  thread_class->interrupt = foundry_dap_debugger_thread_interrupt;
}

static void
foundry_dap_debugger_thread_init (FoundryDapDebuggerThread *self)
{
  g_weak_ref_init (&self->debugger_wr, NULL);
}

FoundryDebuggerThread *
foundry_dap_debugger_thread_new (FoundryDapDebugger *debugger,
                                 gint64              id)
{
  FoundryDapDebuggerThread *self;

  g_return_val_if_fail (FOUNDRY_IS_DAP_DEBUGGER (debugger), NULL);

  self = g_object_new (FOUNDRY_TYPE_DAP_DEBUGGER_THREAD, NULL);
  g_weak_ref_set (&self->debugger_wr, debugger);
  self->id = id;

  return FOUNDRY_DEBUGGER_THREAD (self);
}

void
foundry_dap_debugger_thread_set_stopped (FoundryDapDebuggerThread *self,
                                         gboolean                  stopped)
{
  g_return_if_fail (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self));

  stopped = !!stopped;

  if (stopped != self->stopped)
    {
      self->stopped = stopped;
      g_object_notify (G_OBJECT (self), "stopped");
    }
}

gint64
foundry_dap_debugger_thread_get_id (FoundryDapDebuggerThread *self)
{
  g_return_val_if_fail (FOUNDRY_IS_DAP_DEBUGGER_THREAD (self), 0);

  return self->id;
}