Remove duplicate of a counter in hwthread_update_threads
[openocd.git] / src / rtos / hwthread.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6
7 #include <helper/time_support.h>
8 #include <jtag/jtag.h>
9 #include "target/target.h"
10 #include "target/target_type.h"
11 #include "target/register.h"
12 #include <target/smp.h>
13 #include "rtos.h"
14 #include "helper/log.h"
15 #include "helper/types.h"
16 #include "server/gdb_server.h"
17
18 static bool hwthread_detect_rtos(struct target *target);
19 static int hwthread_create(struct target *target);
20 static int hwthread_update_threads(struct rtos *rtos);
21 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
22 uint32_t reg_num, struct rtos_reg *rtos_reg);
23 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
24 struct rtos_reg **reg_list, int *num_regs);
25 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
26 static int hwthread_smp_init(struct target *target);
27 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
28 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
29 uint32_t size, uint8_t *buffer);
30 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
31 uint32_t size, const uint8_t *buffer);
32
33 #define HW_THREAD_NAME_STR_SIZE (32)
34
35 extern int rtos_thread_packet(struct connection *connection, const char *packet, int packet_size);
36
37 static inline threadid_t threadid_from_target(const struct target *target)
38 {
39 return target->coreid + 1;
40 }
41
42 const struct rtos_type hwthread_rtos = {
43 .name = "hwthread",
44 .detect_rtos = hwthread_detect_rtos,
45 .create = hwthread_create,
46 .update_threads = hwthread_update_threads,
47 .get_thread_reg_list = hwthread_get_thread_reg_list,
48 .get_thread_reg = hwthread_get_thread_reg,
49 .get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
50 .smp_init = hwthread_smp_init,
51 .set_reg = hwthread_set_reg,
52 .read_buffer = hwthread_read_buffer,
53 .write_buffer = hwthread_write_buffer,
54 };
55
56 struct hwthread_params {
57 int dummy_param;
58 };
59
60 static int hwthread_fill_thread(struct rtos *rtos, struct target *curr, int thread_num)
61 {
62 char tmp_str[HW_THREAD_NAME_STR_SIZE];
63 threadid_t tid = threadid_from_target(curr);
64
65 memset(tmp_str, 0, HW_THREAD_NAME_STR_SIZE);
66
67 /* thread-id is the core-id of this core inside the SMP group plus 1 */
68 rtos->thread_details[thread_num].threadid = tid;
69 /* create the thread name */
70 rtos->thread_details[thread_num].exists = true;
71 rtos->thread_details[thread_num].thread_name_str = strdup(target_name(curr));
72 snprintf(tmp_str, HW_THREAD_NAME_STR_SIZE-1, "state: %s", debug_reason_name(curr));
73 rtos->thread_details[thread_num].extra_info_str = strdup(tmp_str);
74
75 return ERROR_OK;
76 }
77
78 static int hwthread_update_threads(struct rtos *rtos)
79 {
80 int threads_found = 0;
81 struct target_list *head;
82 struct target *target;
83 int64_t current_thread = 0;
84 enum target_debug_reason current_reason = DBG_REASON_UNDEFINED;
85
86 if (!rtos)
87 return -1;
88
89 target = rtos->target;
90
91 /* wipe out previous thread details if any */
92 rtos_free_threadlist(rtos);
93
94 /* determine the number of "threads" */
95 if (target->smp) {
96 foreach_smp_target(head, target->smp_targets) {
97 struct target *curr = head->target;
98
99 if (!target_was_examined(curr))
100 continue;
101
102 ++threads_found;
103 }
104 } else
105 threads_found = 1;
106
107 /* create space for new thread details */
108 rtos->thread_details = malloc(sizeof(struct thread_detail) * threads_found);
109
110 if (target->smp) {
111 /* loop over all threads */
112 foreach_smp_target(head, target->smp_targets) {
113 struct target *curr = head->target;
114
115 if (!target_was_examined(curr))
116 continue;
117
118 threadid_t tid = threadid_from_target(curr);
119
120 hwthread_fill_thread(rtos, curr, threads_found);
121
122 /* find an interesting thread to set as current */
123 switch (current_reason) {
124 case DBG_REASON_UNDEFINED:
125 current_reason = curr->debug_reason;
126 current_thread = tid;
127 break;
128 case DBG_REASON_SINGLESTEP:
129 /* single-step can only be overridden by itself */
130 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
131 if (tid == rtos->current_threadid)
132 current_thread = tid;
133 }
134 break;
135 case DBG_REASON_BREAKPOINT:
136 /* single-step overrides breakpoint */
137 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
138 current_reason = curr->debug_reason;
139 current_thread = tid;
140 } else
141 /* multiple breakpoints, prefer gdbs' threadid */
142 if (curr->debug_reason == DBG_REASON_BREAKPOINT) {
143 if (tid == rtos->current_threadid)
144 current_thread = tid;
145 }
146 break;
147 case DBG_REASON_WATCHPOINT:
148 /* breakpoint and single-step override watchpoint */
149 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
150 curr->debug_reason == DBG_REASON_BREAKPOINT) {
151 current_reason = curr->debug_reason;
152 current_thread = tid;
153 }
154 break;
155 case DBG_REASON_DBGRQ:
156 /* all other reasons override debug-request */
157 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
158 curr->debug_reason == DBG_REASON_WATCHPOINT ||
159 curr->debug_reason == DBG_REASON_BREAKPOINT) {
160 current_reason = curr->debug_reason;
161 current_thread = tid;
162 } else
163 if (curr->debug_reason == DBG_REASON_DBGRQ) {
164 if (tid == rtos->current_threadid)
165 current_thread = tid;
166 }
167
168 break;
169
170 default:
171 break;
172 }
173 }
174 } else {
175 hwthread_fill_thread(rtos, target, threads_found);
176 current_thread = threadid_from_target(target);
177 }
178
179 rtos->thread_count = threads_found;
180
181 /* we found an interesting thread, set it as current */
182 if (current_thread != 0)
183 rtos->current_thread = current_thread;
184 else if (rtos->current_threadid != 0)
185 rtos->current_thread = rtos->current_threadid;
186 else
187 rtos->current_thread = threadid_from_target(target);
188
189 LOG_DEBUG("%s current_thread=%i", __func__, (int)rtos->current_thread);
190 return 0;
191 }
192
193 static int hwthread_smp_init(struct target *target)
194 {
195 return hwthread_update_threads(target->rtos);
196 }
197
198 static struct target *hwthread_find_thread(struct target *target, int64_t thread_id)
199 {
200 /* Find the thread with that thread_id */
201 if (!target)
202 return NULL;
203 if (target->smp) {
204 struct target_list *head;
205 foreach_smp_target(head, target->smp_targets) {
206 if (thread_id == threadid_from_target(head->target))
207 return head->target;
208 }
209 } else if (thread_id == threadid_from_target(target)) {
210 return target;
211 }
212 return NULL;
213 }
214
215 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
216 struct rtos_reg **rtos_reg_list, int *rtos_reg_list_size)
217 {
218 if (!rtos)
219 return ERROR_FAIL;
220
221 struct target *target = rtos->target;
222
223 struct target *curr = hwthread_find_thread(target, thread_id);
224 if (!curr)
225 return ERROR_FAIL;
226
227 if (!target_was_examined(curr))
228 return ERROR_FAIL;
229
230 int reg_list_size;
231 struct reg **reg_list;
232 int retval = target_get_gdb_reg_list(curr, &reg_list, &reg_list_size,
233 REG_CLASS_GENERAL);
234 if (retval != ERROR_OK)
235 return retval;
236
237 int j = 0;
238 for (int i = 0; i < reg_list_size; i++) {
239 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
240 continue;
241 j++;
242 }
243 *rtos_reg_list_size = j;
244 *rtos_reg_list = calloc(*rtos_reg_list_size, sizeof(struct rtos_reg));
245 if (!*rtos_reg_list) {
246 free(reg_list);
247 return ERROR_FAIL;
248 }
249
250 j = 0;
251 for (int i = 0; i < reg_list_size; i++) {
252 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
253 continue;
254 (*rtos_reg_list)[j].number = (*reg_list)[i].number;
255 (*rtos_reg_list)[j].size = (*reg_list)[i].size;
256 memcpy((*rtos_reg_list)[j].value, (*reg_list)[i].value,
257 ((*reg_list)[i].size + 7) / 8);
258 j++;
259 }
260 free(reg_list);
261
262 return ERROR_OK;
263 }
264
265 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
266 uint32_t reg_num, struct rtos_reg *rtos_reg)
267 {
268 if (!rtos)
269 return ERROR_FAIL;
270
271 struct target *target = rtos->target;
272
273 struct target *curr = hwthread_find_thread(target, thread_id);
274 if (!curr) {
275 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64 ".", thread_id);
276 return ERROR_FAIL;
277 }
278
279 if (!target_was_examined(curr)) {
280 LOG_ERROR("Target %d hasn't been examined yet.", curr->coreid);
281 return ERROR_FAIL;
282 }
283
284 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
285 if (!reg) {
286 LOG_ERROR("Couldn't find register %" PRIu32 " in thread %" PRId64 ".", reg_num,
287 thread_id);
288 return ERROR_FAIL;
289 }
290
291 if (reg->type->get(reg) != ERROR_OK)
292 return ERROR_FAIL;
293
294 rtos_reg->number = reg->number;
295 rtos_reg->size = reg->size;
296 unsigned bytes = (reg->size + 7) / 8;
297 assert(bytes <= sizeof(rtos_reg->value));
298 memcpy(rtos_reg->value, reg->value, bytes);
299
300 return ERROR_OK;
301 }
302
303 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
304 {
305 if (!rtos)
306 return ERROR_FAIL;
307
308 struct target *target = rtos->target;
309
310 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
311 if (!curr)
312 return ERROR_FAIL;
313
314 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
315 if (!reg)
316 return ERROR_FAIL;
317
318 return reg->type->set(reg, reg_value);
319 }
320
321 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
322 {
323 /* return an empty list, we don't have any symbols to look up */
324 *symbol_list = calloc(1, sizeof(struct symbol_table_elem));
325 (*symbol_list)[0].symbol_name = NULL;
326 return 0;
327 }
328
329 static int hwthread_target_for_threadid(struct connection *connection, int64_t thread_id, struct target **p_target)
330 {
331 struct target *target = get_target_from_connection(connection);
332
333 struct target *curr = hwthread_find_thread(target, thread_id);
334 if (!curr)
335 return ERROR_FAIL;
336
337 *p_target = curr;
338
339 return ERROR_OK;
340 }
341
342 static bool hwthread_detect_rtos(struct target *target)
343 {
344 /* always return 0, avoid auto-detection */
345 return false;
346 }
347
348 static int hwthread_thread_packet(struct connection *connection, const char *packet, int packet_size)
349 {
350 struct target *target = get_target_from_connection(connection);
351
352 struct target *curr = NULL;
353 int64_t current_threadid;
354
355 if (packet[0] == 'H' && packet[1] == 'g') {
356 sscanf(packet, "Hg%16" SCNx64, &current_threadid);
357
358 if (current_threadid > 0) {
359 if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) {
360 LOG_ERROR("hwthread: cannot find thread id %"PRId64, current_threadid);
361 gdb_put_packet(connection, "E01", 3);
362 return ERROR_FAIL;
363 }
364 target->rtos->current_thread = current_threadid;
365 } else
366 if (current_threadid == 0 || current_threadid == -1)
367 target->rtos->current_thread = threadid_from_target(target);
368
369 target->rtos->current_threadid = current_threadid;
370
371 gdb_put_packet(connection, "OK", 2);
372 return ERROR_OK;
373 }
374
375 return rtos_thread_packet(connection, packet, packet_size);
376 }
377
378 static int hwthread_create(struct target *target)
379 {
380 LOG_INFO("Hardware thread awareness created");
381
382 target->rtos->rtos_specific_params = NULL;
383 target->rtos->current_thread = 0;
384 target->rtos->thread_details = NULL;
385 target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
386 target->rtos->gdb_thread_packet = hwthread_thread_packet;
387 return 0;
388 }
389
390 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
391 uint32_t size, uint8_t *buffer)
392 {
393 if (!rtos)
394 return ERROR_FAIL;
395
396 struct target *target = rtos->target;
397
398 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
399 if (!curr)
400 return ERROR_FAIL;
401
402 return target_read_buffer(curr, address, size, buffer);
403 }
404
405 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
406 uint32_t size, const uint8_t *buffer)
407 {
408 if (!rtos)
409 return ERROR_FAIL;
410
411 struct target *target = rtos->target;
412
413 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
414 if (!curr)
415 return ERROR_FAIL;
416
417 return target_write_buffer(curr, address, size, buffer);
418 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)