1 /***************************************************************************
2 * Copyright (C) 2011 by STEricsson *
3 * Heythem Bouhaja heythem.bouhaja@stericsson.com : creation *
4 * Michel JAOUEN michel.jaouen@stericsson.com : adaptation to rtos *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
24 #include <helper/time_support.h>
25 #include <jtag/jtag.h>
26 #include "target/target.h"
27 #include "target/target_type.h"
28 #include "helper/log.h"
29 #include "helper/types.h"
31 #include "rtos_standard_stackings.h"
32 #include <target/register.h>
33 #include <target/smp.h>
34 #include "server/gdb_server.h"
36 #define LINUX_USER_KERNEL_BORDER 0xc0000000
37 #include "linux_header.h"
39 #define MAX_THREADS 200
43 uint32_t init_task_addr;
46 int preupdtate_threadid_count;
49 int threads_needs_update;
50 struct current_thread *current_threads;
51 struct threads *thread_list;
52 /* virt2phys parameter */
57 struct current_thread {
64 struct current_thread *next;
69 uint32_t base_addr; /* address to read magic */
70 uint32_t state; /* magic value : filled only at creation */
71 uint32_t pid; /* linux pid : id for identifying a thread */
72 uint32_t oncpu; /* content cpu number in current thread */
73 uint32_t asid; /* filled only at creation */
75 int status; /* dead = 1 alive = 2 current = 3 alive and current */
76 /* value that should not change during the live of a thread ? */
77 uint32_t thread_info_addr; /* contain latest thread_info_addr computed */
78 /* retrieve from thread_info */
79 struct cpu_context *context;
94 uint32_t preempt_count;
96 static struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
98 static int insert_into_threadlist(struct target *target, struct threads *t);
100 static int linux_os_create(struct target *target);
102 static int linux_os_dummy_update(struct rtos *rtos)
104 /* update is done only when thread request come
105 * too many thread to do it on each stop */
109 static int linux_compute_virt2phys(struct target *target, target_addr_t address)
111 struct linux_os *linux_os = (struct linux_os *)
112 target->rtos->rtos_specific_params;
113 target_addr_t pa = 0;
114 int retval = target->type->virt2phys(target, address, &pa);
115 if (retval != ERROR_OK) {
116 LOG_ERROR("Cannot compute linux virt2phys translation");
117 /* fixes default address */
118 linux_os->phys_base = 0;
122 linux_os->init_task_addr = address;
123 address = address & linux_os->phys_mask;
124 linux_os->phys_base = pa - address;
128 static int linux_read_memory(struct target *target,
129 uint32_t address, uint32_t size, uint32_t count,
133 struct linux_os *linux_os = (struct linux_os *)
134 target->rtos->rtos_specific_params;
135 uint32_t pa = (address & linux_os->phys_mask) + linux_os->phys_base;
137 if (address < 0xc000000) {
138 LOG_ERROR("linux awareness : address in user space");
142 target_read_phys_memory(target, pa, size, count, buffer);
144 target_read_memory(target, address, size, count, buffer);
148 static int fill_buffer(struct target *target, uint32_t addr, uint8_t *buffer)
151 if ((addr & 0xfffffffc) != addr)
152 LOG_INFO("unaligned address %" PRIx32 "!!", addr);
154 int retval = linux_read_memory(target, addr, 4, 1, buffer);
159 static uint32_t get_buffer(struct target *target, const uint8_t *buffer)
162 const uint8_t *value_ptr = buffer;
163 value = target_buffer_get_u32(target, value_ptr);
167 static int linux_os_thread_reg_list(struct rtos *rtos,
168 int64_t thread_id, struct rtos_reg **reg_list, int *num_regs)
170 struct target *target = rtos->target;
171 struct linux_os *linux_os = (struct linux_os *)
172 target->rtos->rtos_specific_params;
173 struct current_thread *tmp = linux_os->current_threads;
174 struct current_thread *next;
177 /* check if a current thread is requested */
181 if (next->threadid == thread_id)
185 } while ((found == 0) && (next != tmp) && (next));
188 LOG_ERROR("could not find thread: %" PRIx64, thread_id);
192 /* search target to perform the access */
193 struct reg **gdb_reg_list;
194 struct target_list *head;
196 foreach_smp_target(head, target->smp_targets) {
197 if (head->target->coreid == next->core_id) {
198 target = head->target;
207 "current thread %" PRIx64 ": no target to perform access of core id %" PRIx32,
213 /*LOG_INFO("thread %lx current on core %x",thread_id, target->coreid);*/
214 retval = target_get_gdb_reg_list(target, &gdb_reg_list, num_regs, REG_CLASS_GENERAL);
215 if (retval != ERROR_OK)
218 *reg_list = calloc(*num_regs, sizeof(struct rtos_reg));
220 for (int i = 0; i < *num_regs; ++i) {
221 if (!gdb_reg_list[i]->valid)
222 gdb_reg_list[i]->type->get(gdb_reg_list[i]);
224 (*reg_list)[i].number = gdb_reg_list[i]->number;
225 (*reg_list)[i].size = gdb_reg_list[i]->size;
227 buf_cpy(gdb_reg_list[i]->value, (*reg_list)[i].value, (*reg_list)[i].size);
233 static bool linux_os_detect(struct target *target)
235 LOG_INFO("should no be called");
239 static int linux_os_smp_init(struct target *target);
240 static int linux_os_clean(struct target *target);
242 static const char * const linux_symbol_list[] = {
247 static int linux_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
250 *symbol_list = (struct symbol_table_elem *)
251 calloc(ARRAY_SIZE(linux_symbol_list), sizeof(struct symbol_table_elem));
253 for (i = 0; i < ARRAY_SIZE(linux_symbol_list); i++)
254 (*symbol_list)[i].symbol_name = linux_symbol_list[i];
259 static char *linux_ps_command(struct target *target);
261 const struct rtos_type linux_rtos = {
263 .detect_rtos = linux_os_detect,
264 .create = linux_os_create,
265 .smp_init = linux_os_smp_init,
266 .update_threads = linux_os_dummy_update,
267 .get_thread_reg_list = linux_os_thread_reg_list,
268 .get_symbol_list_to_lookup = linux_get_symbol_list_to_lookup,
269 .clean = linux_os_clean,
270 .ps_command = linux_ps_command,
273 static int linux_thread_packet(struct connection *connection, char const *packet,
275 static void linux_identify_current_threads(struct target *target);
278 int fill_task_pid(struct target *target, struct threads *t)
280 uint32_t pid_addr = t->base_addr + PID;
282 int retval = fill_buffer(target, pid_addr, buffer);
284 if (retval == ERROR_OK) {
285 uint32_t val = get_buffer(target, buffer);
288 LOG_ERROR("fill_task_pid: unable to read memory");
294 static int fill_task(struct target *target, struct threads *t)
297 uint32_t pid_addr = t->base_addr + PID;
298 uint32_t mem_addr = t->base_addr + MEM;
299 uint32_t on_cpu = t->base_addr + ONCPU;
300 uint8_t *buffer = calloc(1, 4);
301 retval = fill_buffer(target, t->base_addr, buffer);
303 if (retval == ERROR_OK) {
304 uint32_t val = get_buffer(target, buffer);
307 LOG_ERROR("fill_task: unable to read memory");
309 retval = fill_buffer(target, pid_addr, buffer);
311 if (retval == ERROR_OK) {
312 uint32_t val = get_buffer(target, buffer);
315 LOG_ERROR("fill task: unable to read memory");
317 retval = fill_buffer(target, on_cpu, buffer);
319 if (retval == ERROR_OK) {
320 uint32_t val = get_buffer(target, buffer);
323 LOG_ERROR("fill task: unable to read memory");
325 retval = fill_buffer(target, mem_addr, buffer);
327 if (retval == ERROR_OK) {
328 uint32_t val = get_buffer(target, buffer);
331 uint32_t asid_addr = val + MM_CTX;
332 retval = fill_buffer(target, asid_addr, buffer);
334 if (retval == ERROR_OK) {
335 val = get_buffer(target, buffer);
339 ("fill task: unable to read memory -- ASID");
343 LOG_ERROR("fill task: unable to read memory");
350 static int get_name(struct target *target, struct threads *t)
353 uint32_t full_name[4];
354 uint32_t comm = t->base_addr + COMM;
357 for (i = 0; i < 17; i++)
360 retval = linux_read_memory(target, comm, 4, 4, (uint8_t *) full_name);
362 if (retval != ERROR_OK) {
363 LOG_ERROR("get_name: unable to read memory\n");
367 uint32_t raw_name = target_buffer_get_u32(target,
370 t->name[3] = raw_name >> 24;
371 t->name[2] = raw_name >> 16;
372 t->name[1] = raw_name >> 8;
373 t->name[0] = raw_name;
375 target_buffer_get_u32(target, (const uint8_t *)&full_name[1]);
376 t->name[7] = raw_name >> 24;
377 t->name[6] = raw_name >> 16;
378 t->name[5] = raw_name >> 8;
379 t->name[4] = raw_name;
381 target_buffer_get_u32(target, (const uint8_t *)&full_name[2]);
382 t->name[11] = raw_name >> 24;
383 t->name[10] = raw_name >> 16;
384 t->name[9] = raw_name >> 8;
385 t->name[8] = raw_name;
387 target_buffer_get_u32(target, (const uint8_t *)&full_name[3]);
388 t->name[15] = raw_name >> 24;
389 t->name[14] = raw_name >> 16;
390 t->name[13] = raw_name >> 8;
391 t->name[12] = raw_name;
396 static int get_current(struct target *target, int create)
398 struct target_list *head;
402 uint8_t *buffer = calloc(1, 4);
403 struct linux_os *linux_os = (struct linux_os *)
404 target->rtos->rtos_specific_params;
405 struct current_thread *ctt = linux_os->current_threads;
407 /* invalid current threads content */
410 ctt->TS = 0xdeadbeef;
414 foreach_smp_target(head, target->smp_targets) {
415 struct reg **reg_list;
419 if (target_get_gdb_reg_list(head->target, ®_list,
420 ®_list_size, REG_CLASS_GENERAL) != ERROR_OK) {
422 return ERROR_TARGET_FAILURE;
425 if (!reg_list[13]->valid)
426 reg_list[13]->type->get(reg_list[13]);
428 buf = reg_list[13]->value;
429 val = get_buffer(target, buf);
430 ti_addr = (val & 0xffffe000);
431 uint32_t ts_addr = ti_addr + 0xc;
432 retval = fill_buffer(target, ts_addr, buffer);
434 if (retval == ERROR_OK) {
435 uint32_t TS = get_buffer(target, buffer);
436 uint32_t cpu, on_cpu = TS + ONCPU;
437 retval = fill_buffer(target, on_cpu, buffer);
439 if (retval == ERROR_OK) {
440 /*uint32_t cpu = get_buffer(target, buffer);*/
441 struct current_thread *ct =
442 linux_os->current_threads;
443 cpu = head->target->coreid;
445 while ((ct) && (ct->core_id != (int32_t) cpu))
448 if ((ct) && (ct->TS == 0xdeadbeef))
452 ("error in linux current thread update");
456 t = calloc(1, sizeof(struct threads));
457 t->base_addr = ct->TS;
458 fill_task(target, t);
461 insert_into_threadlist(target, t);
463 t->thread_info_addr = 0xdeadbeef;
464 ct->threadid = t->threadid;
465 linux_os->thread_count++;
469 /*LOG_INFO("Creation of current thread %s",t->name);*/
482 static struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
483 uint32_t *thread_info_addr_old)
485 struct cpu_context *context = calloc(1, sizeof(struct cpu_context));
486 uint32_t preempt_count_addr = 0;
487 uint32_t registers[10];
488 uint8_t *buffer = calloc(1, 4);
489 uint32_t stack = base_addr + QAT;
490 uint32_t thread_info_addr = 0;
491 uint32_t thread_info_addr_update = 0;
492 int retval = ERROR_FAIL;
493 context->R4 = 0xdeadbeef;
494 context->R5 = 0xdeadbeef;
495 context->R6 = 0xdeadbeef;
496 context->R7 = 0xdeadbeef;
497 context->R8 = 0xdeadbeef;
498 context->R9 = 0xdeadbeef;
499 context->IP = 0xdeadbeef;
500 context->FP = 0xdeadbeef;
501 context->SP = 0xdeadbeef;
502 context->PC = 0xdeadbeef;
505 if (*thread_info_addr_old == 0xdeadbeef) {
506 retval = fill_buffer(target, stack, buffer);
508 if (retval == ERROR_OK)
509 thread_info_addr = get_buffer(target, buffer);
511 LOG_ERROR("cpu_context: unable to read memory");
513 thread_info_addr_update = thread_info_addr;
515 thread_info_addr = *thread_info_addr_old;
517 preempt_count_addr = thread_info_addr + PREEMPT;
518 retval = fill_buffer(target, preempt_count_addr, buffer);
520 if (retval == ERROR_OK)
521 context->preempt_count = get_buffer(target, buffer);
523 if (*thread_info_addr_old != 0xdeadbeef) {
525 ("cpu_context: cannot read at thread_info_addr");
527 if (*thread_info_addr_old < LINUX_USER_KERNEL_BORDER)
529 ("cpu_context : thread_info_addr in userspace!!!");
531 *thread_info_addr_old = 0xdeadbeef;
535 LOG_ERROR("cpu_context: unable to read memory");
538 thread_info_addr += CPU_CONT;
540 retval = linux_read_memory(target, thread_info_addr, 4, 10,
541 (uint8_t *) registers);
543 if (retval != ERROR_OK) {
545 LOG_ERROR("cpu_context: unable to read memory\n");
550 target_buffer_get_u32(target, (const uint8_t *)®isters[0]);
552 target_buffer_get_u32(target, (const uint8_t *)®isters[1]);
554 target_buffer_get_u32(target, (const uint8_t *)®isters[2]);
556 target_buffer_get_u32(target, (const uint8_t *)®isters[3]);
558 target_buffer_get_u32(target, (const uint8_t *)®isters[4]);
560 target_buffer_get_u32(target, (const uint8_t *)®isters[5]);
562 target_buffer_get_u32(target, (const uint8_t *)®isters[6]);
564 target_buffer_get_u32(target, (const uint8_t *)®isters[7]);
566 target_buffer_get_u32(target, (const uint8_t *)®isters[8]);
568 target_buffer_get_u32(target, (const uint8_t *)®isters[9]);
570 if (*thread_info_addr_old == 0xdeadbeef)
571 *thread_info_addr_old = thread_info_addr_update;
578 static uint32_t next_task(struct target *target, struct threads *t)
580 uint8_t *buffer = calloc(1, 4);
581 uint32_t next_addr = t->base_addr + NEXT;
582 int retval = fill_buffer(target, next_addr, buffer);
584 if (retval == ERROR_OK) {
585 uint32_t val = get_buffer(target, buffer);
590 LOG_ERROR("next task: unable to read memory");
597 static struct current_thread *add_current_thread(struct current_thread *currents,
598 struct current_thread *ct)
606 struct current_thread *temp = currents;
616 static struct threads *liste_del_task(struct threads *task_list, struct threads **t,
617 struct threads *prev)
619 LOG_INFO("del task %" PRId64, (*t)->threadid);
621 prev->next = (*t)->next;
623 task_list = (*t)->next;
625 /* free content of threads */
629 *t = prev ? prev : task_list;
633 static struct threads *liste_add_task(struct threads *task_list, struct threads *t,
634 struct threads **last)
643 struct threads *temp = task_list;
659 static int current_pid(struct linux_os *linux_os, uint32_t pid)
661 static int current_base_addr(struct linux_os *linux_os, uint32_t base_addr)
664 struct current_thread *ct = linux_os->current_threads;
667 while ((ct) && (ct->pid != pid))
669 while ((ct) && (ct->TS != base_addr))
673 if ((ct) && (ct->pid == pid))
675 if ((ct) && (ct->TS == base_addr))
682 static int linux_get_tasks(struct target *target, int context)
686 struct linux_os *linux_os = (struct linux_os *)
687 target->rtos->rtos_specific_params;
688 linux_os->thread_list = NULL;
689 linux_os->thread_count = 0;
691 if (linux_os->init_task_addr == 0xdeadbeef) {
692 LOG_INFO("no init symbol\n");
696 int64_t start = timeval_ms();
698 struct threads *t = calloc(1, sizeof(struct threads));
699 struct threads *last = NULL;
700 t->base_addr = linux_os->init_task_addr;
701 /* retrieve the thread id , currently running in the different smp core */
702 get_current(target, 1);
704 while (((t->base_addr != linux_os->init_task_addr) &&
705 (t->base_addr != 0)) || (loop == 0)) {
707 fill_task(target, t);
708 retval = get_name(target, t);
710 if (loop > MAX_THREADS) {
712 LOG_INFO("more than %d threads !!", MAX_THREADS);
716 if (retval != ERROR_OK) {
721 /* check that this thread is not one the current threads already
726 if (!current_pid(linux_os, t->pid)) {
728 if (!current_base_addr(linux_os, t->base_addr)) {
730 t->threadid = linux_os->threadid_count;
732 linux_os->threadid_count++;
734 linux_os->thread_list =
735 liste_add_task(linux_os->thread_list, t, &last);
736 /* no interest to fill the context if it is a current thread. */
737 linux_os->thread_count++;
738 t->thread_info_addr = 0xdeadbeef;
742 cpu_context_read(target, t->base_addr,
743 &t->thread_info_addr);
744 base_addr = next_task(target, t);
746 /*LOG_INFO("thread %s is a current thread already created",t->name); */
747 base_addr = next_task(target, t);
751 t = calloc(1, sizeof(struct threads));
752 t->base_addr = base_addr;
755 linux_os->threads_lookup = 1;
756 linux_os->threads_needs_update = 0;
757 linux_os->preupdtate_threadid_count = linux_os->threadid_count - 1;
758 /* check that all current threads have been identified */
760 LOG_INFO("complete time %" PRId64 ", thread mean %" PRId64 "\n",
761 (timeval_ms() - start),
762 (timeval_ms() - start) / linux_os->threadid_count);
764 LOG_INFO("threadid count %d", linux_os->threadid_count);
770 static int clean_threadlist(struct target *target)
772 struct linux_os *linux_os = (struct linux_os *)
773 target->rtos->rtos_specific_params;
774 struct threads *old, *temp = linux_os->thread_list;
788 static int linux_os_clean(struct target *target)
790 struct linux_os *os_linux = (struct linux_os *)
791 target->rtos->rtos_specific_params;
792 clean_threadlist(target);
793 os_linux->init_task_addr = 0xdeadbeef;
794 os_linux->name = "linux";
795 os_linux->thread_list = NULL;
796 os_linux->thread_count = 0;
797 os_linux->nr_cpus = 0;
798 os_linux->threads_lookup = 0;
799 os_linux->threads_needs_update = 0;
800 os_linux->threadid_count = 1;
804 static int insert_into_threadlist(struct target *target, struct threads *t)
806 struct linux_os *linux_os = (struct linux_os *)
807 target->rtos->rtos_specific_params;
808 struct threads *temp = linux_os->thread_list;
809 t->threadid = linux_os->threadid_count;
810 linux_os->threadid_count++;
815 linux_os->thread_list = t;
827 static void linux_identify_current_threads(struct target *target)
829 struct linux_os *linux_os = (struct linux_os *)
830 target->rtos->rtos_specific_params;
831 struct threads *thread_list = linux_os->thread_list;
832 struct current_thread *ct = linux_os->current_threads;
833 struct threads *t = NULL;
836 if (ct->threadid == -1) {
838 /* un-identified thread */
840 t = calloc(1, sizeof(struct threads));
841 t->base_addr = ct->TS;
844 if (fill_task_pid(target, t) != ERROR_OK) {
848 ("linux identify_current_threads: unable to read pid");
853 /* search in the list of threads if pid
855 while ((thread_list) && (found == 0)) {
857 if (thread_list->pid == t->pid) {
859 if (thread_list->base_addr == t->base_addr) {
865 thread_list = thread_list->next;
869 /* it is a new thread */
870 if (fill_task(target, t) != ERROR_OK)
874 insert_into_threadlist(target, t);
875 t->thread_info_addr = 0xdeadbeef;
879 ct->threadid = t->threadid;
883 linux_os->thread_count++;
886 LOG_INFO("current thread core %x identified %s",
887 ct->core_id, t->name);
889 LOG_INFO("current thread core %x, reused %s",
890 ct->core_id, t->name);
896 tmp.base_addr = ct->TS;
897 get_name(target, &tmp);
898 LOG_INFO("current thread core %x , already identified %s !!!",
899 ct->core_id, tmp.name);
909 LOG_ERROR("unable to read pid");
915 static int linux_task_update(struct target *target, int context)
917 struct linux_os *linux_os = (struct linux_os *)
918 target->rtos->rtos_specific_params;
919 struct threads *thread_list = linux_os->thread_list;
922 linux_os->thread_count = 0;
924 /*thread_list = thread_list->next; skip init_task*/
925 while (thread_list) {
926 thread_list->status = 0; /*setting all tasks to dead state*/
928 free(thread_list->context);
929 thread_list->context = NULL;
931 thread_list = thread_list->next;
936 if (linux_os->init_task_addr == 0xdeadbeef) {
937 LOG_INFO("no init symbol\n");
940 int64_t start = timeval_ms();
941 struct threads *t = calloc(1, sizeof(struct threads));
942 uint32_t previous = 0xdeadbeef;
943 t->base_addr = linux_os->init_task_addr;
944 retval = get_current(target, 0);
945 /*check that all current threads have been identified */
946 linux_identify_current_threads(target);
948 while (((t->base_addr != linux_os->init_task_addr) &&
949 (t->base_addr != previous)) || (loop == 0)) {
950 /* for avoiding any permanent loop for any reason possibly due to
953 previous = t->base_addr;
956 retval = fill_task_pid(target, t);
959 if (retval != ERROR_OK) {
964 thread_list = linux_os->thread_list;
966 while (thread_list) {
968 if (t->pid == thread_list->pid) {
970 if (t->base_addr == thread_list->base_addr) {
972 if (!thread_list->status) {
974 if (t->base_addr != thread_list->base_addr)
975 LOG_INFO("thread base_addr has changed !!");
977 /* this is not a current thread */
978 thread_list->base_addr = t->base_addr;
979 thread_list->status = 1;
981 /* we don 't update this field any more */
983 /*thread_list->state = t->state;
984 thread_list->oncpu = t->oncpu;
985 thread_list->asid = t->asid;
988 thread_list->context =
989 cpu_context_read(target,
990 thread_list->base_addr,
991 &thread_list->thread_info_addr);
993 /* it is a current thread no need to read context */
996 linux_os->thread_count++;
1001 thread_list = thread_list->next;
1007 fill_task(target, t);
1008 get_name(target, t);
1009 retval = insert_into_threadlist(target, t);
1010 t->thread_info_addr = 0xdeadbeef;
1014 cpu_context_read(target, t->base_addr,
1015 &t->thread_info_addr);
1017 base_addr = next_task(target, t);
1018 t = calloc(1, sizeof(struct threads));
1019 t->base_addr = base_addr;
1020 linux_os->thread_count++;
1022 t->base_addr = next_task(target, t);
1025 LOG_INFO("update thread done %" PRId64 ", mean%" PRId64 "\n",
1026 (timeval_ms() - start), (timeval_ms() - start) / loop);
1028 linux_os->threads_needs_update = 0;
1032 static int linux_gdb_thread_packet(struct target *target,
1033 struct connection *connection, char const *packet,
1037 struct linux_os *linux_os =
1038 (struct linux_os *)target->rtos->rtos_specific_params;
1040 if (linux_os->init_task_addr == 0xdeadbeef) {
1041 /* it has not been initialized */
1042 LOG_INFO("received thread request without init task address");
1043 gdb_put_packet(connection, "l", 1);
1047 retval = linux_get_tasks(target, 1);
1049 if (retval != ERROR_OK)
1050 return ERROR_TARGET_FAILURE;
1052 char *out_str = calloc(MAX_THREADS * 17 + 10, 1);
1053 char *tmp_str = out_str;
1054 tmp_str += sprintf(tmp_str, "m");
1055 struct threads *temp = linux_os->thread_list;
1058 tmp_str += sprintf(tmp_str, "%016" PRIx64, temp->threadid);
1061 tmp_str += sprintf(tmp_str, ",");
1064 gdb_put_packet(connection, out_str, strlen(out_str));
1069 static int linux_gdb_thread_update(struct target *target,
1070 struct connection *connection, char const *packet,
1074 struct linux_os *linux_os = (struct linux_os *)
1075 target->rtos->rtos_specific_params;
1076 struct threads *temp = linux_os->thread_list;
1079 if (temp->threadid == linux_os->preupdtate_threadid_count + 1) {
1080 /*LOG_INFO("FOUND");*/
1088 /*LOG_INFO("INTO GDB THREAD UPDATE FOUNDING START TASK");*/
1089 char *out_strr = calloc(MAX_THREADS * 17 + 10, 1);
1090 char *tmp_strr = out_strr;
1091 tmp_strr += sprintf(tmp_strr, "m");
1092 /*LOG_INFO("CHAR MALLOC & M DONE");*/
1093 tmp_strr += sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
1098 /*LOG_INFO("INTO GDB THREAD UPDATE WHILE");*/
1099 tmp_strr += sprintf(tmp_strr, ",");
1101 sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
1106 gdb_put_packet(connection, out_strr, strlen(out_strr));
1107 linux_os->preupdtate_threadid_count =
1108 linux_os->threadid_count - 1;
1111 gdb_put_packet(connection, "l", 1);
1116 static int linux_thread_extra_info(struct target *target,
1117 struct connection *connection, char const *packet,
1120 int64_t threadid = 0;
1121 struct linux_os *linux_os = (struct linux_os *)
1122 target->rtos->rtos_specific_params;
1123 sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid);
1124 /*LOG_INFO("lookup extra info for thread %" SCNx64, threadid);*/
1125 struct threads *temp = linux_os->thread_list;
1128 if (temp->threadid == threadid) {
1129 char *pid = " PID: ";
1130 char *pid_current = "*PID: ";
1131 char *name = "Name: ";
1132 int str_size = strlen(pid) + strlen(name);
1133 char *tmp_str = calloc(1, str_size + 50);
1134 char *tmp_str_ptr = tmp_str;
1136 /* discriminate current task */
1137 if (temp->status == 3)
1138 tmp_str_ptr += sprintf(tmp_str_ptr, "%s",
1141 tmp_str_ptr += sprintf(tmp_str_ptr, "%s", pid);
1143 tmp_str_ptr += sprintf(tmp_str_ptr, "%d, ", (int)temp->pid);
1144 sprintf(tmp_str_ptr, "%s", name);
1145 sprintf(tmp_str_ptr, "%s", temp->name);
1146 char *hex_str = calloc(1, strlen(tmp_str) * 2 + 1);
1147 size_t pkt_len = hexify(hex_str, (const uint8_t *)tmp_str,
1148 strlen(tmp_str), strlen(tmp_str) * 2 + 1);
1149 gdb_put_packet(connection, hex_str, pkt_len);
1158 LOG_INFO("thread not found");
1162 static int linux_gdb_t_packet(struct connection *connection,
1163 struct target *target, char const *packet, int packet_size)
1166 struct linux_os *linux_os = (struct linux_os *)
1167 target->rtos->rtos_specific_params;
1168 int retval = ERROR_OK;
1169 sscanf(packet, "T%" SCNx64, &threadid);
1171 if (linux_os->threads_needs_update == 0) {
1172 struct threads *temp = linux_os->thread_list;
1173 struct threads *prev = NULL;
1176 if (temp->threadid == threadid) {
1177 if (temp->status != 0) {
1178 gdb_put_packet(connection, "OK", 2);
1181 /* delete item in the list */
1182 linux_os->thread_list =
1183 liste_del_task(linux_os->thread_list,
1185 linux_os->thread_count--;
1186 gdb_put_packet(connection, "E01", 3);
1196 LOG_INFO("gdb requested status on non existing thread");
1197 gdb_put_packet(connection, "E01", 3);
1201 retval = linux_task_update(target, 1);
1202 struct threads *temp = linux_os->thread_list;
1205 if (temp->threadid == threadid) {
1206 if (temp->status == 1) {
1207 gdb_put_packet(connection, "OK", 2);
1210 gdb_put_packet(connection, "E01", 3);
1222 static int linux_gdb_h_packet(struct connection *connection,
1223 struct target *target, char const *packet, int packet_size)
1225 struct linux_os *linux_os = (struct linux_os *)
1226 target->rtos->rtos_specific_params;
1227 struct current_thread *ct = linux_os->current_threads;
1229 /* select to display the current thread of the selected target */
1230 while ((ct) && (ct->core_id != target->coreid))
1233 int64_t current_gdb_thread_rq;
1235 if (linux_os->threads_lookup == 1) {
1236 if ((ct) && (ct->threadid == -1)) {
1237 ct = linux_os->current_threads;
1239 while ((ct) && (ct->threadid == -1))
1244 /* no current thread can be identified
1245 * any way with smp */
1246 LOG_INFO("no current thread identified");
1247 /* attempt to display the name of the 2 threads identified with
1250 ct = linux_os->current_threads;
1252 while ((ct) && (ct->threadid == -1)) {
1253 t.base_addr = ct->TS;
1254 get_name(target, &t);
1255 LOG_INFO("name of unidentified thread %s",
1260 gdb_put_packet(connection, "OK", 2);
1264 if (packet[1] == 'g') {
1265 sscanf(packet, "Hg%16" SCNx64, ¤t_gdb_thread_rq);
1267 if (current_gdb_thread_rq == 0) {
1268 target->rtos->current_threadid = ct->threadid;
1269 gdb_put_packet(connection, "OK", 2);
1271 target->rtos->current_threadid =
1272 current_gdb_thread_rq;
1273 gdb_put_packet(connection, "OK", 2);
1275 } else if (packet[1] == 'c') {
1276 sscanf(packet, "Hc%16" SCNx64, ¤t_gdb_thread_rq);
1278 if ((current_gdb_thread_rq == 0) ||
1279 (current_gdb_thread_rq == ct->threadid)) {
1280 target->rtos->current_threadid = ct->threadid;
1281 gdb_put_packet(connection, "OK", 2);
1283 gdb_put_packet(connection, "E01", 3);
1286 gdb_put_packet(connection, "OK", 2);
1291 static int linux_thread_packet(struct connection *connection, char const *packet,
1294 int retval = ERROR_OK;
1295 struct current_thread *ct;
1296 struct target *target = get_target_from_connection(connection);
1297 struct linux_os *linux_os = (struct linux_os *)
1298 target->rtos->rtos_specific_params;
1300 switch (packet[0]) {
1301 case 'T': /* Is thread alive?*/
1303 linux_gdb_t_packet(connection, target, packet, packet_size);
1305 case 'H': /* Set current thread */
1306 /* ( 'c' for step and continue, 'g' for all other operations )*/
1307 /*LOG_INFO(" H packet received '%s'", packet);*/
1308 linux_gdb_h_packet(connection, target, packet, packet_size);
1312 if (strncmp(packet, "qSymbol", 7) == 0) {
1313 if (rtos_qsymbol(connection, packet, packet_size) == 1) {
1314 linux_compute_virt2phys(target,
1315 target->rtos->symbols[INIT_TASK].address);
1319 } else if (strncmp(packet, "qfThreadInfo", 12) == 0) {
1320 if (!linux_os->thread_list) {
1321 retval = linux_gdb_thread_packet(target,
1327 retval = linux_gdb_thread_update(target,
1333 } else if (strncmp(packet, "qsThreadInfo", 12) == 0) {
1334 gdb_put_packet(connection, "l", 1);
1336 } else if (strncmp(packet, "qThreadExtraInfo,", 17) == 0) {
1337 linux_thread_extra_info(target, connection, packet,
1341 retval = GDB_THREAD_PACKET_NOT_CONSUMED;
1346 /* previously response was : thread not found
1347 * gdb_put_packet(connection, "E01", 3); */
1348 retval = GDB_THREAD_PACKET_NOT_CONSUMED;
1352 if (linux_os->threads_lookup == 1) {
1353 ct = linux_os->current_threads;
1355 while ((ct) && (ct->core_id) != target->coreid)
1358 if ((ct) && (ct->threadid == -1)) {
1359 ct = linux_os->current_threads;
1361 while ((ct) && (ct->threadid == -1))
1365 if ((ct) && (ct->threadid !=
1366 target->rtos->current_threadid)
1367 && (target->rtos->current_threadid != -1))
1368 LOG_WARNING("WARNING! current GDB thread do not match "
1369 "current thread running. "
1370 "Switch thread in GDB to threadid %d",
1373 LOG_INFO("threads_needs_update = 1");
1374 linux_os->threads_needs_update = 1;
1378 /* if a packet handler returned an error, exit input loop */
1379 if (retval != ERROR_OK)
1386 static int linux_os_smp_init(struct target *target)
1388 struct target_list *head;
1389 /* keep only target->rtos */
1390 struct rtos *rtos = target->rtos;
1391 struct linux_os *os_linux =
1392 (struct linux_os *)rtos->rtos_specific_params;
1393 struct current_thread *ct;
1395 foreach_smp_target(head, target->smp_targets) {
1396 if (head->target->rtos != rtos) {
1397 struct linux_os *smp_os_linux =
1398 (struct linux_os *)head->target->rtos->rtos_specific_params;
1399 /* remap smp target on rtos */
1400 free(head->target->rtos);
1401 head->target->rtos = rtos;
1402 /* reuse allocated ct */
1403 ct = smp_os_linux->current_threads;
1405 ct->TS = 0xdeadbeef;
1406 ct->core_id = head->target->coreid;
1407 os_linux->current_threads =
1408 add_current_thread(os_linux->current_threads, ct);
1409 os_linux->nr_cpus++;
1417 static int linux_os_create(struct target *target)
1419 struct linux_os *os_linux = calloc(1, sizeof(struct linux_os));
1420 struct current_thread *ct = calloc(1, sizeof(struct current_thread));
1421 LOG_INFO("linux os creation\n");
1422 os_linux->init_task_addr = 0xdeadbeef;
1423 os_linux->name = "linux";
1424 os_linux->thread_list = NULL;
1425 os_linux->thread_count = 0;
1426 target->rtos->current_threadid = -1;
1427 os_linux->nr_cpus = 1;
1428 os_linux->threads_lookup = 0;
1429 os_linux->threads_needs_update = 0;
1430 os_linux->threadid_count = 1;
1431 os_linux->current_threads = NULL;
1432 target->rtos->rtos_specific_params = os_linux;
1433 ct->core_id = target->coreid;
1435 ct->TS = 0xdeadbeef;
1436 os_linux->current_threads =
1437 add_current_thread(os_linux->current_threads, ct);
1438 /* overload rtos thread default handler */
1439 target->rtos->gdb_thread_packet = linux_thread_packet;
1440 /* initialize a default virt 2 phys translation */
1441 os_linux->phys_mask = ~0xc0000000;
1442 os_linux->phys_base = 0x0;
1446 static char *linux_ps_command(struct target *target)
1448 struct linux_os *linux_os = (struct linux_os *)
1449 target->rtos->rtos_specific_params;
1450 int retval = ERROR_OK;
1453 if (linux_os->threads_lookup == 0)
1454 retval = linux_get_tasks(target, 1);
1456 if (linux_os->threads_needs_update != 0)
1457 retval = linux_task_update(target, 0);
1460 if (retval == ERROR_OK) {
1461 struct threads *temp = linux_os->thread_list;
1463 LOG_INFO("allocation for %d threads line",
1464 linux_os->thread_count);
1465 display = calloc((linux_os->thread_count + 2) * 80, 1);
1471 tmp += sprintf(tmp, "PID\t\tCPU\t\tASID\t\tNAME\n");
1472 tmp += sprintf(tmp, "---\t\t---\t\t----\t\t----\n");
1479 "%" PRIu32 "\t\t%" PRIu32 "\t\t%" PRIx32 "\t\t%s\n",
1480 temp->pid, temp->oncpu,
1481 temp->asid, temp->name);
1485 "%" PRIu32 "\t\t%" PRIu32 "\t\t%" PRIx32 "\t\t%s\n",
1486 temp->pid, temp->oncpu,
1487 temp->asid, temp->name);
1497 display = calloc(40, 1);
1498 sprintf(display, "linux_ps_command failed\n");