You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1508 lines
38 KiB

  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 *
  5. * *
  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. *
  10. * *
  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. *
  15. * *
  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. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <helper/time_support.h>
  23. #include <jtag/jtag.h>
  24. #include "target/target.h"
  25. #include "target/target_type.h"
  26. #include "helper/log.h"
  27. #include "helper/types.h"
  28. #include "rtos.h"
  29. #include "rtos_standard_stackings.h"
  30. #include <target/register.h>
  31. #include "server/gdb_server.h"
  32. #define LINUX_USER_KERNEL_BORDER 0xc0000000
  33. #include "linux_header.h"
  34. #define PHYS
  35. #define MAX_THREADS 200
  36. /* specific task */
  37. struct linux_os {
  38. const char *name;
  39. uint32_t init_task_addr;
  40. int thread_count;
  41. int threadid_count;
  42. int preupdtate_threadid_count;
  43. int nr_cpus;
  44. int threads_lookup;
  45. int threads_needs_update;
  46. struct current_thread *current_threads;
  47. struct threads *thread_list;
  48. /* virt2phys parameter */
  49. uint32_t phys_mask;
  50. uint32_t phys_base;
  51. };
  52. struct current_thread {
  53. int64_t threadid;
  54. int32_t core_id;
  55. #ifdef PID_CHECK
  56. uint32_t pid;
  57. #endif
  58. uint32_t TS;
  59. struct current_thread *next;
  60. };
  61. struct threads {
  62. char name[17];
  63. uint32_t base_addr; /* address to read magic */
  64. uint32_t state; /* magic value : filled only at creation */
  65. uint32_t pid; /* linux pid : id for identifying a thread */
  66. uint32_t oncpu; /* content cpu number in current thread */
  67. uint32_t asid; /* filled only at creation */
  68. int64_t threadid;
  69. int status; /* dead = 1 alive = 2 current = 3 alive and current */
  70. /* value that should not change during the live of a thread ? */
  71. uint32_t thread_info_addr; /* contain latest thread_info_addr computed */
  72. /* retrieve from thread_info */
  73. struct cpu_context *context;
  74. struct threads *next;
  75. };
  76. struct cpu_context {
  77. uint32_t R4;
  78. uint32_t R5;
  79. uint32_t R6;
  80. uint32_t R7;
  81. uint32_t R8;
  82. uint32_t R9;
  83. uint32_t IP;
  84. uint32_t FP;
  85. uint32_t SP;
  86. uint32_t PC;
  87. uint32_t preempt_count;
  88. };
  89. static struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
  90. uint32_t *info_addr);
  91. static int insert_into_threadlist(struct target *target, struct threads *t);
  92. static int linux_os_create(struct target *target);
  93. static int linux_os_dummy_update(struct rtos *rtos)
  94. {
  95. /* update is done only when thread request come
  96. * too many thread to do it on each stop */
  97. return 0;
  98. }
  99. static int linux_compute_virt2phys(struct target *target, target_addr_t address)
  100. {
  101. struct linux_os *linux_os = (struct linux_os *)
  102. target->rtos->rtos_specific_params;
  103. target_addr_t pa = 0;
  104. int retval = target->type->virt2phys(target, address, &pa);
  105. if (retval != ERROR_OK) {
  106. LOG_ERROR("Cannot compute linux virt2phys translation");
  107. /* fixes default address */
  108. linux_os->phys_base = 0;
  109. return ERROR_FAIL;
  110. }
  111. linux_os->init_task_addr = address;
  112. address = address & linux_os->phys_mask;
  113. linux_os->phys_base = pa - address;
  114. return ERROR_OK;
  115. }
  116. static int linux_read_memory(struct target *target,
  117. uint32_t address, uint32_t size, uint32_t count,
  118. uint8_t *buffer)
  119. {
  120. #ifdef PHYS
  121. struct linux_os *linux_os = (struct linux_os *)
  122. target->rtos->rtos_specific_params;
  123. uint32_t pa = (address & linux_os->phys_mask) + linux_os->phys_base;
  124. #endif
  125. if (address < 0xc000000) {
  126. LOG_ERROR("linux awareness : address in user space");
  127. return ERROR_FAIL;
  128. }
  129. #ifdef PHYS
  130. target_read_phys_memory(target, pa, size, count, buffer);
  131. #endif
  132. target_read_memory(target, address, size, count, buffer);
  133. return ERROR_OK;
  134. }
  135. static int fill_buffer(struct target *target, uint32_t addr, uint8_t *buffer)
  136. {
  137. if ((addr & 0xfffffffc) != addr)
  138. LOG_INFO("unaligned address %" PRIx32 "!!", addr);
  139. int retval = linux_read_memory(target, addr, 4, 1, buffer);
  140. return retval;
  141. }
  142. static uint32_t get_buffer(struct target *target, const uint8_t *buffer)
  143. {
  144. uint32_t value = 0;
  145. const uint8_t *value_ptr = buffer;
  146. value = target_buffer_get_u32(target, value_ptr);
  147. return value;
  148. }
  149. static int linux_os_thread_reg_list(struct rtos *rtos,
  150. int64_t thread_id, struct rtos_reg **reg_list, int *num_regs)
  151. {
  152. struct target *target = rtos->target;
  153. struct linux_os *linux_os = (struct linux_os *)
  154. target->rtos->rtos_specific_params;
  155. struct current_thread *tmp = linux_os->current_threads;
  156. struct current_thread *next;
  157. int found = 0;
  158. int retval;
  159. /* check if a current thread is requested */
  160. next = tmp;
  161. do {
  162. if (next->threadid == thread_id)
  163. found = 1;
  164. else
  165. next = next->next;
  166. } while ((found == 0) && (next != tmp) && (next));
  167. if (found == 0) {
  168. LOG_ERROR("could not find thread: %" PRIx64, thread_id);
  169. return ERROR_FAIL;
  170. }
  171. /* search target to perform the access */
  172. struct reg **gdb_reg_list;
  173. struct target_list *head;
  174. head = target->head;
  175. found = 0;
  176. do {
  177. if (head->target->coreid == next->core_id) {
  178. target = head->target;
  179. found = 1;
  180. } else
  181. head = head->next;
  182. } while ((head != (struct target_list *)NULL) && (found == 0));
  183. if (found == 0) {
  184. LOG_ERROR
  185. (
  186. "current thread %" PRIx64 ": no target to perform access of core id %" PRIx32,
  187. thread_id,
  188. next->core_id);
  189. return ERROR_FAIL;
  190. }
  191. /*LOG_INFO("thread %lx current on core %x",thread_id, target->coreid);*/
  192. retval = target_get_gdb_reg_list(target, &gdb_reg_list, num_regs, REG_CLASS_GENERAL);
  193. if (retval != ERROR_OK)
  194. return retval;
  195. *reg_list = calloc(*num_regs, sizeof(struct rtos_reg));
  196. for (int i = 0; i < *num_regs; ++i) {
  197. if (!gdb_reg_list[i]->valid)
  198. gdb_reg_list[i]->type->get(gdb_reg_list[i]);
  199. (*reg_list)[i].number = gdb_reg_list[i]->number;
  200. (*reg_list)[i].size = gdb_reg_list[i]->size;
  201. buf_cpy(gdb_reg_list[i]->value, (*reg_list)[i].value, (*reg_list)[i].size);
  202. }
  203. return ERROR_OK;
  204. }
  205. static bool linux_os_detect(struct target *target)
  206. {
  207. LOG_INFO("should no be called");
  208. return false;
  209. }
  210. static int linux_os_smp_init(struct target *target);
  211. static int linux_os_clean(struct target *target);
  212. #define INIT_TASK 0
  213. static const char * const linux_symbol_list[] = {
  214. "init_task",
  215. NULL
  216. };
  217. static int linux_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
  218. {
  219. unsigned int i;
  220. *symbol_list = (struct symbol_table_elem *)
  221. calloc(ARRAY_SIZE(linux_symbol_list), sizeof(struct symbol_table_elem));
  222. for (i = 0; i < ARRAY_SIZE(linux_symbol_list); i++)
  223. (*symbol_list)[i].symbol_name = linux_symbol_list[i];
  224. return 0;
  225. }
  226. static char *linux_ps_command(struct target *target);
  227. const struct rtos_type linux_rtos = {
  228. .name = "linux",
  229. .detect_rtos = linux_os_detect,
  230. .create = linux_os_create,
  231. .smp_init = linux_os_smp_init,
  232. .update_threads = linux_os_dummy_update,
  233. .get_thread_reg_list = linux_os_thread_reg_list,
  234. .get_symbol_list_to_lookup = linux_get_symbol_list_to_lookup,
  235. .clean = linux_os_clean,
  236. .ps_command = linux_ps_command,
  237. };
  238. static int linux_thread_packet(struct connection *connection, char const *packet,
  239. int packet_size);
  240. static void linux_identify_current_threads(struct target *target);
  241. #ifdef PID_CHECK
  242. int fill_task_pid(struct target *target, struct threads *t)
  243. {
  244. uint32_t pid_addr = t->base_addr + PID;
  245. uint8_t buffer[4];
  246. int retval = fill_buffer(target, pid_addr, buffer);
  247. if (retval == ERROR_OK) {
  248. uint32_t val = get_buffer(target, buffer);
  249. t->pid = val;
  250. } else
  251. LOG_ERROR("fill_task_pid: unable to read memory");
  252. return retval;
  253. }
  254. #endif
  255. static int fill_task(struct target *target, struct threads *t)
  256. {
  257. int retval;
  258. uint32_t pid_addr = t->base_addr + PID;
  259. uint32_t mem_addr = t->base_addr + MEM;
  260. uint32_t on_cpu = t->base_addr + ONCPU;
  261. uint8_t *buffer = calloc(1, 4);
  262. retval = fill_buffer(target, t->base_addr, buffer);
  263. if (retval == ERROR_OK) {
  264. uint32_t val = get_buffer(target, buffer);
  265. t->state = val;
  266. } else
  267. LOG_ERROR("fill_task: unable to read memory");
  268. retval = fill_buffer(target, pid_addr, buffer);
  269. if (retval == ERROR_OK) {
  270. uint32_t val = get_buffer(target, buffer);
  271. t->pid = val;
  272. } else
  273. LOG_ERROR("fill task: unable to read memory");
  274. retval = fill_buffer(target, on_cpu, buffer);
  275. if (retval == ERROR_OK) {
  276. uint32_t val = get_buffer(target, buffer);
  277. t->oncpu = val;
  278. } else
  279. LOG_ERROR("fill task: unable to read memory");
  280. retval = fill_buffer(target, mem_addr, buffer);
  281. if (retval == ERROR_OK) {
  282. uint32_t val = get_buffer(target, buffer);
  283. if (val != 0) {
  284. uint32_t asid_addr = val + MM_CTX;
  285. retval = fill_buffer(target, asid_addr, buffer);
  286. if (retval == ERROR_OK) {
  287. val = get_buffer(target, buffer);
  288. t->asid = val;
  289. } else
  290. LOG_ERROR
  291. ("fill task: unable to read memory -- ASID");
  292. } else
  293. t->asid = 0;
  294. } else
  295. LOG_ERROR("fill task: unable to read memory");
  296. free(buffer);
  297. return retval;
  298. }
  299. static int get_name(struct target *target, struct threads *t)
  300. {
  301. int retval;
  302. uint32_t full_name[4];
  303. uint32_t comm = t->base_addr + COMM;
  304. int i;
  305. for (i = 0; i < 17; i++)
  306. t->name[i] = 0;
  307. retval = linux_read_memory(target, comm, 4, 4, (uint8_t *) full_name);
  308. if (retval != ERROR_OK) {
  309. LOG_ERROR("get_name: unable to read memory\n");
  310. return ERROR_FAIL;
  311. }
  312. uint32_t raw_name = target_buffer_get_u32(target,
  313. (const uint8_t *)
  314. &full_name[0]);
  315. t->name[3] = raw_name >> 24;
  316. t->name[2] = raw_name >> 16;
  317. t->name[1] = raw_name >> 8;
  318. t->name[0] = raw_name;
  319. raw_name =
  320. target_buffer_get_u32(target, (const uint8_t *)&full_name[1]);
  321. t->name[7] = raw_name >> 24;
  322. t->name[6] = raw_name >> 16;
  323. t->name[5] = raw_name >> 8;
  324. t->name[4] = raw_name;
  325. raw_name =
  326. target_buffer_get_u32(target, (const uint8_t *)&full_name[2]);
  327. t->name[11] = raw_name >> 24;
  328. t->name[10] = raw_name >> 16;
  329. t->name[9] = raw_name >> 8;
  330. t->name[8] = raw_name;
  331. raw_name =
  332. target_buffer_get_u32(target, (const uint8_t *)&full_name[3]);
  333. t->name[15] = raw_name >> 24;
  334. t->name[14] = raw_name >> 16;
  335. t->name[13] = raw_name >> 8;
  336. t->name[12] = raw_name;
  337. return ERROR_OK;
  338. }
  339. static int get_current(struct target *target, int create)
  340. {
  341. struct target_list *head;
  342. head = target->head;
  343. uint8_t *buf;
  344. uint32_t val;
  345. uint32_t ti_addr;
  346. uint8_t *buffer = calloc(1, 4);
  347. struct linux_os *linux_os = (struct linux_os *)
  348. target->rtos->rtos_specific_params;
  349. struct current_thread *ctt = linux_os->current_threads;
  350. /* invalid current threads content */
  351. while (ctt) {
  352. ctt->threadid = -1;
  353. ctt->TS = 0xdeadbeef;
  354. ctt = ctt->next;
  355. }
  356. while (head != (struct target_list *)NULL) {
  357. struct reg **reg_list;
  358. int reg_list_size;
  359. int retval;
  360. if (target_get_gdb_reg_list(head->target, &reg_list,
  361. &reg_list_size, REG_CLASS_GENERAL) != ERROR_OK) {
  362. free(buffer);
  363. return ERROR_TARGET_FAILURE;
  364. }
  365. if (!reg_list[13]->valid)
  366. reg_list[13]->type->get(reg_list[13]);
  367. buf = reg_list[13]->value;
  368. val = get_buffer(target, buf);
  369. ti_addr = (val & 0xffffe000);
  370. uint32_t ts_addr = ti_addr + 0xc;
  371. retval = fill_buffer(target, ts_addr, buffer);
  372. if (retval == ERROR_OK) {
  373. uint32_t TS = get_buffer(target, buffer);
  374. uint32_t cpu, on_cpu = TS + ONCPU;
  375. retval = fill_buffer(target, on_cpu, buffer);
  376. if (retval == ERROR_OK) {
  377. /*uint32_t cpu = get_buffer(target, buffer);*/
  378. struct current_thread *ct =
  379. linux_os->current_threads;
  380. cpu = head->target->coreid;
  381. while ((ct) && (ct->core_id != (int32_t) cpu))
  382. ct = ct->next;
  383. if ((ct) && (ct->TS == 0xdeadbeef))
  384. ct->TS = TS;
  385. else
  386. LOG_ERROR
  387. ("error in linux current thread update");
  388. if (create && ct) {
  389. struct threads *t;
  390. t = calloc(1, sizeof(struct threads));
  391. t->base_addr = ct->TS;
  392. fill_task(target, t);
  393. get_name(target, t);
  394. t->oncpu = cpu;
  395. insert_into_threadlist(target, t);
  396. t->status = 3;
  397. t->thread_info_addr = 0xdeadbeef;
  398. ct->threadid = t->threadid;
  399. linux_os->thread_count++;
  400. #ifdef PID_CHECK
  401. ct->pid = t->pid;
  402. #endif
  403. /*LOG_INFO("Creation of current thread %s",t->name);*/
  404. }
  405. }
  406. }
  407. free(reg_list);
  408. head = head->next;
  409. }
  410. free(buffer);
  411. return ERROR_OK;
  412. }
  413. static struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
  414. uint32_t *thread_info_addr_old)
  415. {
  416. struct cpu_context *context = calloc(1, sizeof(struct cpu_context));
  417. uint32_t preempt_count_addr = 0;
  418. uint32_t registers[10];
  419. uint8_t *buffer = calloc(1, 4);
  420. uint32_t stack = base_addr + QAT;
  421. uint32_t thread_info_addr = 0;
  422. uint32_t thread_info_addr_update = 0;
  423. int retval = ERROR_FAIL;
  424. context->R4 = 0xdeadbeef;
  425. context->R5 = 0xdeadbeef;
  426. context->R6 = 0xdeadbeef;
  427. context->R7 = 0xdeadbeef;
  428. context->R8 = 0xdeadbeef;
  429. context->R9 = 0xdeadbeef;
  430. context->IP = 0xdeadbeef;
  431. context->FP = 0xdeadbeef;
  432. context->SP = 0xdeadbeef;
  433. context->PC = 0xdeadbeef;
  434. retry:
  435. if (*thread_info_addr_old == 0xdeadbeef) {
  436. retval = fill_buffer(target, stack, buffer);
  437. if (retval == ERROR_OK)
  438. thread_info_addr = get_buffer(target, buffer);
  439. else
  440. LOG_ERROR("cpu_context: unable to read memory");
  441. thread_info_addr_update = thread_info_addr;
  442. } else
  443. thread_info_addr = *thread_info_addr_old;
  444. preempt_count_addr = thread_info_addr + PREEMPT;
  445. retval = fill_buffer(target, preempt_count_addr, buffer);
  446. if (retval == ERROR_OK)
  447. context->preempt_count = get_buffer(target, buffer);
  448. else {
  449. if (*thread_info_addr_old != 0xdeadbeef) {
  450. LOG_ERROR
  451. ("cpu_context: cannot read at thread_info_addr");
  452. if (*thread_info_addr_old < LINUX_USER_KERNEL_BORDER)
  453. LOG_INFO
  454. ("cpu_context : thread_info_addr in userspace!!!");
  455. *thread_info_addr_old = 0xdeadbeef;
  456. goto retry;
  457. }
  458. LOG_ERROR("cpu_context: unable to read memory");
  459. }
  460. thread_info_addr += CPU_CONT;
  461. retval = linux_read_memory(target, thread_info_addr, 4, 10,
  462. (uint8_t *) registers);
  463. if (retval != ERROR_OK) {
  464. free(buffer);
  465. LOG_ERROR("cpu_context: unable to read memory\n");
  466. return context;
  467. }
  468. context->R4 =
  469. target_buffer_get_u32(target, (const uint8_t *)&registers[0]);
  470. context->R5 =
  471. target_buffer_get_u32(target, (const uint8_t *)&registers[1]);
  472. context->R6 =
  473. target_buffer_get_u32(target, (const uint8_t *)&registers[2]);
  474. context->R7 =
  475. target_buffer_get_u32(target, (const uint8_t *)&registers[3]);
  476. context->R8 =
  477. target_buffer_get_u32(target, (const uint8_t *)&registers[4]);
  478. context->R9 =
  479. target_buffer_get_u32(target, (const uint8_t *)&registers[5]);
  480. context->IP =
  481. target_buffer_get_u32(target, (const uint8_t *)&registers[6]);
  482. context->FP =
  483. target_buffer_get_u32(target, (const uint8_t *)&registers[7]);
  484. context->SP =
  485. target_buffer_get_u32(target, (const uint8_t *)&registers[8]);
  486. context->PC =
  487. target_buffer_get_u32(target, (const uint8_t *)&registers[9]);
  488. if (*thread_info_addr_old == 0xdeadbeef)
  489. *thread_info_addr_old = thread_info_addr_update;
  490. free(buffer);
  491. return context;
  492. }
  493. static uint32_t next_task(struct target *target, struct threads *t)
  494. {
  495. uint8_t *buffer = calloc(1, 4);
  496. uint32_t next_addr = t->base_addr + NEXT;
  497. int retval = fill_buffer(target, next_addr, buffer);
  498. if (retval == ERROR_OK) {
  499. uint32_t val = get_buffer(target, buffer);
  500. val = val - NEXT;
  501. free(buffer);
  502. return val;
  503. } else
  504. LOG_ERROR("next task: unable to read memory");
  505. free(buffer);
  506. return 0;
  507. }
  508. static struct current_thread *add_current_thread(struct current_thread *currents,
  509. struct current_thread *ct)
  510. {
  511. ct->next = NULL;
  512. if (!currents) {
  513. currents = ct;
  514. return currents;
  515. } else {
  516. struct current_thread *temp = currents;
  517. while (temp->next)
  518. temp = temp->next;
  519. temp->next = ct;
  520. return currents;
  521. }
  522. }
  523. static struct threads *liste_del_task(struct threads *task_list, struct threads **t,
  524. struct threads *prev)
  525. {
  526. LOG_INFO("del task %" PRId64, (*t)->threadid);
  527. if (prev)
  528. prev->next = (*t)->next;
  529. else
  530. task_list = (*t)->next;
  531. /* free content of threads */
  532. free((*t)->context);
  533. free(*t);
  534. *t = prev ? prev : task_list;
  535. return task_list;
  536. }
  537. static struct threads *liste_add_task(struct threads *task_list, struct threads *t,
  538. struct threads **last)
  539. {
  540. t->next = NULL;
  541. if (!*last)
  542. if (!task_list) {
  543. task_list = t;
  544. return task_list;
  545. } else {
  546. struct threads *temp = task_list;
  547. while (temp->next)
  548. temp = temp->next;
  549. temp->next = t;
  550. *last = t;
  551. return task_list;
  552. } else {
  553. (*last)->next = t;
  554. *last = t;
  555. return task_list;
  556. }
  557. }
  558. #ifdef PID_CHECK
  559. static int current_pid(struct linux_os *linux_os, uint32_t pid)
  560. #else
  561. static int current_base_addr(struct linux_os *linux_os, uint32_t base_addr)
  562. #endif
  563. {
  564. struct current_thread *ct = linux_os->current_threads;
  565. #ifdef PID_CHECK
  566. while ((ct) && (ct->pid != pid))
  567. #else
  568. while ((ct) && (ct->TS != base_addr))
  569. #endif
  570. ct = ct->next;
  571. #ifdef PID_CHECK
  572. if ((ct) && (ct->pid == pid))
  573. #else
  574. if ((ct) && (ct->TS == base_addr))
  575. #endif
  576. return 1;
  577. return 0;
  578. }
  579. static int linux_get_tasks(struct target *target, int context)
  580. {
  581. int loop = 0;
  582. int retval = 0;
  583. struct linux_os *linux_os = (struct linux_os *)
  584. target->rtos->rtos_specific_params;
  585. linux_os->thread_list = NULL;
  586. linux_os->thread_count = 0;
  587. if (linux_os->init_task_addr == 0xdeadbeef) {
  588. LOG_INFO("no init symbol\n");
  589. return ERROR_FAIL;
  590. }
  591. int64_t start = timeval_ms();
  592. struct threads *t = calloc(1, sizeof(struct threads));
  593. struct threads *last = NULL;
  594. t->base_addr = linux_os->init_task_addr;
  595. /* retrieve the thread id , currently running in the different smp core */
  596. get_current(target, 1);
  597. while (((t->base_addr != linux_os->init_task_addr) &&
  598. (t->base_addr != 0)) || (loop == 0)) {
  599. loop++;
  600. fill_task(target, t);
  601. retval = get_name(target, t);
  602. if (loop > MAX_THREADS) {
  603. free(t);
  604. LOG_INFO("more than %d threads !!", MAX_THREADS);
  605. return ERROR_FAIL;
  606. }
  607. if (retval != ERROR_OK) {
  608. free(t);
  609. return ERROR_FAIL;
  610. }
  611. /* check that this thread is not one the current threads already
  612. * created */
  613. uint32_t base_addr;
  614. #ifdef PID_CHECK
  615. if (!current_pid(linux_os, t->pid)) {
  616. #else
  617. if (!current_base_addr(linux_os, t->base_addr)) {
  618. #endif
  619. t->threadid = linux_os->threadid_count;
  620. t->status = 1;
  621. linux_os->threadid_count++;
  622. linux_os->thread_list =
  623. liste_add_task(linux_os->thread_list, t, &last);
  624. /* no interest to fill the context if it is a current thread. */
  625. linux_os->thread_count++;
  626. t->thread_info_addr = 0xdeadbeef;
  627. if (context)
  628. t->context =
  629. cpu_context_read(target, t->base_addr,
  630. &t->thread_info_addr);
  631. base_addr = next_task(target, t);
  632. } else {
  633. /*LOG_INFO("thread %s is a current thread already created",t->name); */
  634. base_addr = next_task(target, t);
  635. free(t);
  636. }
  637. t = calloc(1, sizeof(struct threads));
  638. t->base_addr = base_addr;
  639. }
  640. linux_os->threads_lookup = 1;
  641. linux_os->threads_needs_update = 0;
  642. linux_os->preupdtate_threadid_count = linux_os->threadid_count - 1;
  643. /* check that all current threads have been identified */
  644. LOG_INFO("complete time %" PRId64 ", thread mean %" PRId64 "\n",
  645. (timeval_ms() - start),
  646. (timeval_ms() - start) / linux_os->threadid_count);
  647. LOG_INFO("threadid count %d", linux_os->threadid_count);
  648. free(t);
  649. return ERROR_OK;
  650. }
  651. static int clean_threadlist(struct target *target)
  652. {
  653. struct linux_os *linux_os = (struct linux_os *)
  654. target->rtos->rtos_specific_params;
  655. struct threads *old, *temp = linux_os->thread_list;
  656. while (temp) {
  657. old = temp;
  658. free(temp->context);
  659. temp = temp->next;
  660. free(old);
  661. }
  662. return ERROR_OK;
  663. }
  664. static int linux_os_clean(struct target *target)
  665. {
  666. struct linux_os *os_linux = (struct linux_os *)
  667. target->rtos->rtos_specific_params;
  668. clean_threadlist(target);
  669. os_linux->init_task_addr = 0xdeadbeef;
  670. os_linux->name = "linux";
  671. os_linux->thread_list = NULL;
  672. os_linux->thread_count = 0;
  673. os_linux->nr_cpus = 0;
  674. os_linux->threads_lookup = 0;
  675. os_linux->threads_needs_update = 0;
  676. os_linux->threadid_count = 1;
  677. return ERROR_OK;
  678. }
  679. static int insert_into_threadlist(struct target *target, struct threads *t)
  680. {
  681. struct linux_os *linux_os = (struct linux_os *)
  682. target->rtos->rtos_specific_params;
  683. struct threads *temp = linux_os->thread_list;
  684. t->threadid = linux_os->threadid_count;
  685. linux_os->threadid_count++;
  686. t->status = 1;
  687. t->next = NULL;
  688. if (!temp)
  689. linux_os->thread_list = t;
  690. else {
  691. while (temp->next)
  692. temp = temp->next;
  693. t->next = NULL;
  694. temp->next = t;
  695. }
  696. return ERROR_OK;
  697. }
  698. static void linux_identify_current_threads(struct target *target)
  699. {
  700. struct linux_os *linux_os = (struct linux_os *)
  701. target->rtos->rtos_specific_params;
  702. struct threads *thread_list = linux_os->thread_list;
  703. struct current_thread *ct = linux_os->current_threads;
  704. struct threads *t = NULL;
  705. while ((ct)) {
  706. if (ct->threadid == -1) {
  707. /* un-identified thread */
  708. int found = 0;
  709. t = calloc(1, sizeof(struct threads));
  710. t->base_addr = ct->TS;
  711. #ifdef PID_CHECK
  712. if (fill_task_pid(target, t) != ERROR_OK) {
  713. error_handling:
  714. free(t);
  715. LOG_ERROR
  716. ("linux identify_current_threads: unable to read pid");
  717. return;
  718. }
  719. #endif
  720. /* search in the list of threads if pid
  721. already present */
  722. while ((thread_list) && (found == 0)) {
  723. #ifdef PID_CHECK
  724. if (thread_list->pid == t->pid) {
  725. #else
  726. if (thread_list->base_addr == t->base_addr) {
  727. #endif
  728. free(t);
  729. t = thread_list;
  730. found = 1;
  731. }
  732. thread_list = thread_list->next;
  733. }
  734. if (!found) {
  735. /* it is a new thread */
  736. if (fill_task(target, t) != ERROR_OK)
  737. goto error_handling;
  738. get_name(target, t);
  739. insert_into_threadlist(target, t);
  740. t->thread_info_addr = 0xdeadbeef;
  741. }
  742. t->status = 3;
  743. ct->threadid = t->threadid;
  744. #ifdef PID_CHECK
  745. ct->pid = t->pid;
  746. #endif
  747. linux_os->thread_count++;
  748. #if 0
  749. if (found == 0)
  750. LOG_INFO("current thread core %x identified %s",
  751. ct->core_id, t->name);
  752. else
  753. LOG_INFO("current thread core %x, reused %s",
  754. ct->core_id, t->name);
  755. #endif
  756. }
  757. #if 0
  758. else {
  759. struct threads tmp;
  760. tmp.base_addr = ct->TS;
  761. get_name(target, &tmp);
  762. LOG_INFO("current thread core %x , already identified %s !!!",
  763. ct->core_id, tmp.name);
  764. }
  765. #endif
  766. ct = ct->next;
  767. }
  768. return;
  769. #ifndef PID_CHECK
  770. error_handling:
  771. free(t);
  772. LOG_ERROR("unable to read pid");
  773. return;
  774. #endif
  775. }
  776. static int linux_task_update(struct target *target, int context)
  777. {
  778. struct linux_os *linux_os = (struct linux_os *)
  779. target->rtos->rtos_specific_params;
  780. struct threads *thread_list = linux_os->thread_list;
  781. int retval;
  782. int loop = 0;
  783. linux_os->thread_count = 0;
  784. /*thread_list = thread_list->next; skip init_task*/
  785. while (thread_list) {
  786. thread_list->status = 0; /*setting all tasks to dead state*/
  787. free(thread_list->context);
  788. thread_list->context = NULL;
  789. thread_list = thread_list->next;
  790. }
  791. int found = 0;
  792. if (linux_os->init_task_addr == 0xdeadbeef) {
  793. LOG_INFO("no init symbol\n");
  794. return ERROR_FAIL;
  795. }
  796. int64_t start = timeval_ms();
  797. struct threads *t = calloc(1, sizeof(struct threads));
  798. uint32_t previous = 0xdeadbeef;
  799. t->base_addr = linux_os->init_task_addr;
  800. retval = get_current(target, 0);
  801. /*check that all current threads have been identified */
  802. linux_identify_current_threads(target);
  803. while (((t->base_addr != linux_os->init_task_addr) &&
  804. (t->base_addr != previous)) || (loop == 0)) {
  805. /* for avoiding any permanent loop for any reason possibly due to
  806. * target */
  807. loop++;
  808. previous = t->base_addr;
  809. /* read only pid */
  810. #ifdef PID_CHECK
  811. retval = fill_task_pid(target, t);
  812. #endif
  813. if (retval != ERROR_OK) {
  814. free(t);
  815. return ERROR_FAIL;
  816. }
  817. thread_list = linux_os->thread_list;
  818. while (thread_list) {
  819. #ifdef PID_CHECK
  820. if (t->pid == thread_list->pid) {
  821. #else
  822. if (t->base_addr == thread_list->base_addr) {
  823. #endif
  824. if (!thread_list->status) {
  825. #ifdef PID_CHECK
  826. if (t->base_addr != thread_list->base_addr)
  827. LOG_INFO("thread base_addr has changed !!");
  828. #endif
  829. /* this is not a current thread */
  830. thread_list->base_addr = t->base_addr;
  831. thread_list->status = 1;
  832. /* we don 't update this field any more */
  833. /*thread_list->state = t->state;
  834. thread_list->oncpu = t->oncpu;
  835. thread_list->asid = t->asid;
  836. */
  837. if (context)
  838. thread_list->context =
  839. cpu_context_read(target,
  840. thread_list->base_addr,
  841. &thread_list->thread_info_addr);
  842. } else {
  843. /* it is a current thread no need to read context */
  844. }
  845. linux_os->thread_count++;
  846. found = 1;
  847. break;
  848. } else {
  849. found = 0;
  850. thread_list = thread_list->next;
  851. }
  852. }
  853. if (found == 0) {
  854. uint32_t base_addr;
  855. fill_task(target, t);
  856. get_name(target, t);
  857. retval = insert_into_threadlist(target, t);
  858. t->thread_info_addr = 0xdeadbeef;
  859. if (context)
  860. t->context =
  861. cpu_context_read(target, t->base_addr,
  862. &t->thread_info_addr);
  863. base_addr = next_task(target, t);
  864. t = calloc(1, sizeof(struct threads));
  865. t->base_addr = base_addr;
  866. linux_os->thread_count++;
  867. } else
  868. t->base_addr = next_task(target, t);
  869. }
  870. LOG_INFO("update thread done %" PRId64 ", mean%" PRId64 "\n",
  871. (timeval_ms() - start), (timeval_ms() - start) / loop);
  872. free(t);
  873. linux_os->threads_needs_update = 0;
  874. return ERROR_OK;
  875. }
  876. static int linux_gdb_thread_packet(struct target *target,
  877. struct connection *connection, char const *packet,
  878. int packet_size)
  879. {
  880. int retval;
  881. struct linux_os *linux_os =
  882. (struct linux_os *)target->rtos->rtos_specific_params;
  883. if (linux_os->init_task_addr == 0xdeadbeef) {
  884. /* it has not been initialized */
  885. LOG_INFO("received thread request without init task address");
  886. gdb_put_packet(connection, "l", 1);
  887. return ERROR_OK;
  888. }
  889. retval = linux_get_tasks(target, 1);
  890. if (retval != ERROR_OK)
  891. return ERROR_TARGET_FAILURE;
  892. char *out_str = calloc(MAX_THREADS * 17 + 10, 1);
  893. char *tmp_str = out_str;
  894. tmp_str += sprintf(tmp_str, "m");
  895. struct threads *temp = linux_os->thread_list;
  896. while (temp) {
  897. tmp_str += sprintf(tmp_str, "%016" PRIx64, temp->threadid);
  898. temp = temp->next;
  899. if (temp)
  900. tmp_str += sprintf(tmp_str, ",");
  901. }
  902. gdb_put_packet(connection, out_str, strlen(out_str));
  903. free(out_str);
  904. return ERROR_OK;
  905. }
  906. static int linux_gdb_thread_update(struct target *target,
  907. struct connection *connection, char const *packet,
  908. int packet_size)
  909. {
  910. int found = 0;
  911. struct linux_os *linux_os = (struct linux_os *)
  912. target->rtos->rtos_specific_params;
  913. struct threads *temp = linux_os->thread_list;
  914. while (temp) {
  915. if (temp->threadid == linux_os->preupdtate_threadid_count + 1) {
  916. /*LOG_INFO("FOUND");*/
  917. found = 1;
  918. break;
  919. } else
  920. temp = temp->next;
  921. }
  922. if (found == 1) {
  923. /*LOG_INFO("INTO GDB THREAD UPDATE FOUNDING START TASK");*/
  924. char *out_strr = calloc(MAX_THREADS * 17 + 10, 1);
  925. char *tmp_strr = out_strr;
  926. tmp_strr += sprintf(tmp_strr, "m");
  927. /*LOG_INFO("CHAR MALLOC & M DONE");*/
  928. tmp_strr += sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
  929. temp = temp->next;
  930. while (temp) {
  931. /*LOG_INFO("INTO GDB THREAD UPDATE WHILE");*/
  932. tmp_strr += sprintf(tmp_strr, ",");
  933. tmp_strr +=
  934. sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
  935. temp = temp->next;
  936. }
  937. /*tmp_str[0] = 0;*/
  938. gdb_put_packet(connection, out_strr, strlen(out_strr));
  939. linux_os->preupdtate_threadid_count =
  940. linux_os->threadid_count - 1;
  941. free(out_strr);
  942. } else
  943. gdb_put_packet(connection, "l", 1);
  944. return ERROR_OK;
  945. }
  946. static int linux_thread_extra_info(struct target *target,
  947. struct connection *connection, char const *packet,
  948. int packet_size)
  949. {
  950. int64_t threadid = 0;
  951. struct linux_os *linux_os = (struct linux_os *)
  952. target->rtos->rtos_specific_params;
  953. sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid);
  954. /*LOG_INFO("lookup extra info for thread %" SCNx64, threadid);*/
  955. struct threads *temp = linux_os->thread_list;
  956. while (temp) {
  957. if (temp->threadid == threadid) {
  958. char *pid = " PID: ";
  959. char *pid_current = "*PID: ";
  960. char *name = "Name: ";
  961. int str_size = strlen(pid) + strlen(name);
  962. char *tmp_str = calloc(1, str_size + 50);
  963. char *tmp_str_ptr = tmp_str;
  964. /* discriminate current task */
  965. if (temp->status == 3)
  966. tmp_str_ptr += sprintf(tmp_str_ptr, "%s",
  967. pid_current);
  968. else
  969. tmp_str_ptr += sprintf(tmp_str_ptr, "%s", pid);
  970. tmp_str_ptr += sprintf(tmp_str_ptr, "%d, ", (int)temp->pid);
  971. sprintf(tmp_str_ptr, "%s", name);
  972. sprintf(tmp_str_ptr, "%s", temp->name);
  973. char *hex_str = calloc(1, strlen(tmp_str) * 2 + 1);
  974. size_t pkt_len = hexify(hex_str, (const uint8_t *)tmp_str,
  975. strlen(tmp_str), strlen(tmp_str) * 2 + 1);
  976. gdb_put_packet(connection, hex_str, pkt_len);
  977. free(hex_str);
  978. free(tmp_str);
  979. return ERROR_OK;
  980. }
  981. temp = temp->next;
  982. }
  983. LOG_INFO("thread not found");
  984. return ERROR_OK;
  985. }
  986. static int linux_gdb_t_packet(struct connection *connection,
  987. struct target *target, char const *packet, int packet_size)
  988. {
  989. int64_t threadid;
  990. struct linux_os *linux_os = (struct linux_os *)
  991. target->rtos->rtos_specific_params;
  992. int retval = ERROR_OK;
  993. sscanf(packet, "T%" SCNx64, &threadid);
  994. if (linux_os->threads_needs_update == 0) {
  995. struct threads *temp = linux_os->thread_list;
  996. struct threads *prev = NULL;
  997. while (temp) {
  998. if (temp->threadid == threadid) {
  999. if (temp->status != 0) {
  1000. gdb_put_packet(connection, "OK", 2);
  1001. return ERROR_OK;
  1002. } else {
  1003. /* delete item in the list */
  1004. linux_os->thread_list =
  1005. liste_del_task(linux_os->thread_list,
  1006. &temp, prev);
  1007. linux_os->thread_count--;
  1008. gdb_put_packet(connection, "E01", 3);
  1009. return ERROR_OK;
  1010. }
  1011. }
  1012. /* for deletion */
  1013. prev = temp;
  1014. temp = temp->next;
  1015. }
  1016. LOG_INFO("gdb requested status on non existing thread");
  1017. gdb_put_packet(connection, "E01", 3);
  1018. return ERROR_OK;
  1019. } else {
  1020. retval = linux_task_update(target, 1);
  1021. struct threads *temp = linux_os->thread_list;
  1022. while (temp) {
  1023. if (temp->threadid == threadid) {
  1024. if (temp->status == 1) {
  1025. gdb_put_packet(connection, "OK", 2);
  1026. return ERROR_OK;
  1027. } else {
  1028. gdb_put_packet(connection, "E01", 3);
  1029. return ERROR_OK;
  1030. }
  1031. }
  1032. temp = temp->next;
  1033. }
  1034. }
  1035. return retval;
  1036. }
  1037. static int linux_gdb_h_packet(struct connection *connection,
  1038. struct target *target, char const *packet, int packet_size)
  1039. {
  1040. struct linux_os *linux_os = (struct linux_os *)
  1041. target->rtos->rtos_specific_params;
  1042. struct current_thread *ct = linux_os->current_threads;
  1043. /* select to display the current thread of the selected target */
  1044. while ((ct) && (ct->core_id != target->coreid))
  1045. ct = ct->next;
  1046. int64_t current_gdb_thread_rq;
  1047. if (linux_os->threads_lookup == 1) {
  1048. if ((ct) && (ct->threadid == -1)) {
  1049. ct = linux_os->current_threads;
  1050. while ((ct) && (ct->threadid == -1))
  1051. ct = ct->next;
  1052. }
  1053. if (!ct) {
  1054. /* no current thread can be identified
  1055. * any way with smp */
  1056. LOG_INFO("no current thread identified");
  1057. /* attempt to display the name of the 2 threads identified with
  1058. * get_current */
  1059. struct threads t;
  1060. ct = linux_os->current_threads;
  1061. while ((ct) && (ct->threadid == -1)) {
  1062. t.base_addr = ct->TS;
  1063. get_name(target, &t);
  1064. LOG_INFO("name of unidentified thread %s",
  1065. t.name);
  1066. ct = ct->next;
  1067. }
  1068. gdb_put_packet(connection, "OK", 2);
  1069. return ERROR_OK;
  1070. }
  1071. if (packet[1] == 'g') {
  1072. sscanf(packet, "Hg%16" SCNx64, &current_gdb_thread_rq);
  1073. if (current_gdb_thread_rq == 0) {
  1074. target->rtos->current_threadid = ct->threadid;
  1075. gdb_put_packet(connection, "OK", 2);
  1076. } else {
  1077. target->rtos->current_threadid =
  1078. current_gdb_thread_rq;
  1079. gdb_put_packet(connection, "OK", 2);
  1080. }
  1081. } else if (packet[1] == 'c') {
  1082. sscanf(packet, "Hc%16" SCNx64, &current_gdb_thread_rq);
  1083. if ((current_gdb_thread_rq == 0) ||
  1084. (current_gdb_thread_rq == ct->threadid)) {
  1085. target->rtos->current_threadid = ct->threadid;
  1086. gdb_put_packet(connection, "OK", 2);
  1087. } else
  1088. gdb_put_packet(connection, "E01", 3);
  1089. }
  1090. } else
  1091. gdb_put_packet(connection, "OK", 2);
  1092. return ERROR_OK;
  1093. }
  1094. static int linux_thread_packet(struct connection *connection, char const *packet,
  1095. int packet_size)
  1096. {
  1097. int retval = ERROR_OK;
  1098. struct current_thread *ct;
  1099. struct target *target = get_target_from_connection(connection);
  1100. struct linux_os *linux_os = (struct linux_os *)
  1101. target->rtos->rtos_specific_params;
  1102. switch (packet[0]) {
  1103. case 'T': /* Is thread alive?*/
  1104. linux_gdb_t_packet(connection, target, packet, packet_size);
  1105. break;
  1106. case 'H': /* Set current thread */
  1107. /* ( 'c' for step and continue, 'g' for all other operations )*/
  1108. /*LOG_INFO(" H packet received '%s'", packet);*/
  1109. linux_gdb_h_packet(connection, target, packet, packet_size);
  1110. break;
  1111. case 'q':
  1112. if (strncmp(packet, "qSymbol", 7) == 0) {
  1113. if (rtos_qsymbol(connection, packet, packet_size) == 1) {
  1114. linux_compute_virt2phys(target,
  1115. target->rtos->symbols[INIT_TASK].address);
  1116. }
  1117. break;
  1118. } else if (strncmp(packet, "qfThreadInfo", 12) == 0) {
  1119. if (!linux_os->thread_list) {
  1120. retval = linux_gdb_thread_packet(target,
  1121. connection,
  1122. packet,
  1123. packet_size);
  1124. break;
  1125. } else {
  1126. retval = linux_gdb_thread_update(target,
  1127. connection,
  1128. packet,
  1129. packet_size);
  1130. break;
  1131. }
  1132. } else if (strncmp(packet, "qsThreadInfo", 12) == 0) {
  1133. gdb_put_packet(connection, "l", 1);
  1134. break;
  1135. } else if (strncmp(packet, "qThreadExtraInfo,", 17) == 0) {
  1136. linux_thread_extra_info(target, connection, packet,
  1137. packet_size);
  1138. break;
  1139. } else {
  1140. retval = GDB_THREAD_PACKET_NOT_CONSUMED;
  1141. break;
  1142. }
  1143. case 'Q':
  1144. /* previously response was : thread not found
  1145. * gdb_put_packet(connection, "E01", 3); */
  1146. retval = GDB_THREAD_PACKET_NOT_CONSUMED;
  1147. break;
  1148. case 'c':
  1149. case 's': {
  1150. if (linux_os->threads_lookup == 1) {
  1151. ct = linux_os->current_threads;
  1152. while ((ct) && (ct->core_id) != target->coreid)
  1153. ct = ct->next;
  1154. if ((ct) && (ct->threadid == -1)) {
  1155. ct = linux_os->current_threads;
  1156. while ((ct) && (ct->threadid == -1))
  1157. ct = ct->next;
  1158. }
  1159. if ((ct) && (ct->threadid !=
  1160. target->rtos->current_threadid)
  1161. && (target->rtos->current_threadid != -1))
  1162. LOG_WARNING("WARNING! current GDB thread do not match "
  1163. "current thread running. "
  1164. "Switch thread in GDB to threadid %d",
  1165. (int)ct->threadid);
  1166. LOG_INFO("threads_needs_update = 1");
  1167. linux_os->threads_needs_update = 1;
  1168. }
  1169. }
  1170. /* if a packet handler returned an error, exit input loop */
  1171. if (retval != ERROR_OK)
  1172. return retval;
  1173. }
  1174. return retval;
  1175. }
  1176. static int linux_os_smp_init(struct target *target)
  1177. {
  1178. struct target_list *head;
  1179. /* keep only target->rtos */
  1180. struct rtos *rtos = target->rtos;
  1181. struct linux_os *os_linux =
  1182. (struct linux_os *)rtos->rtos_specific_params;
  1183. struct current_thread *ct;
  1184. head = target->head;
  1185. while (head != (struct target_list *)NULL) {
  1186. if (head->target->rtos != rtos) {
  1187. struct linux_os *smp_os_linux =
  1188. (struct linux_os *)head->target->rtos->rtos_specific_params;
  1189. /* remap smp target on rtos */
  1190. free(head->target->rtos);
  1191. head->target->rtos = rtos;
  1192. /* reuse allocated ct */
  1193. ct = smp_os_linux->current_threads;
  1194. ct->threadid = -1;
  1195. ct->TS = 0xdeadbeef;
  1196. ct->core_id = head->target->coreid;
  1197. os_linux->current_threads =
  1198. add_current_thread(os_linux->current_threads, ct);
  1199. os_linux->nr_cpus++;
  1200. free(smp_os_linux);
  1201. }
  1202. head = head->next;
  1203. }
  1204. return ERROR_OK;
  1205. }
  1206. static int linux_os_create(struct target *target)
  1207. {
  1208. struct linux_os *os_linux = calloc(1, sizeof(struct linux_os));
  1209. struct current_thread *ct = calloc(1, sizeof(struct current_thread));
  1210. LOG_INFO("linux os creation\n");
  1211. os_linux->init_task_addr = 0xdeadbeef;
  1212. os_linux->name = "linux";
  1213. os_linux->thread_list = NULL;
  1214. os_linux->thread_count = 0;
  1215. target->rtos->current_threadid = -1;
  1216. os_linux->nr_cpus = 1;
  1217. os_linux->threads_lookup = 0;
  1218. os_linux->threads_needs_update = 0;
  1219. os_linux->threadid_count = 1;
  1220. os_linux->current_threads = NULL;
  1221. target->rtos->rtos_specific_params = os_linux;
  1222. ct->core_id = target->coreid;
  1223. ct->threadid = -1;
  1224. ct->TS = 0xdeadbeef;
  1225. os_linux->current_threads =
  1226. add_current_thread(os_linux->current_threads, ct);
  1227. /* overload rtos thread default handler */
  1228. target->rtos->gdb_thread_packet = linux_thread_packet;
  1229. /* initialize a default virt 2 phys translation */
  1230. os_linux->phys_mask = ~0xc0000000;
  1231. os_linux->phys_base = 0x0;
  1232. return JIM_OK;
  1233. }
  1234. static char *linux_ps_command(struct target *target)
  1235. {
  1236. struct linux_os *linux_os = (struct linux_os *)
  1237. target->rtos->rtos_specific_params;
  1238. int retval = ERROR_OK;
  1239. char *display;
  1240. if (linux_os->threads_lookup == 0)
  1241. retval = linux_get_tasks(target, 1);
  1242. else {
  1243. if (linux_os->threads_needs_update != 0)
  1244. retval = linux_task_update(target, 0);
  1245. }
  1246. if (retval == ERROR_OK) {
  1247. struct threads *temp = linux_os->thread_list;
  1248. char *tmp;
  1249. LOG_INFO("allocation for %d threads line",
  1250. linux_os->thread_count);
  1251. display = calloc((linux_os->thread_count + 2) * 80, 1);
  1252. if (!display)
  1253. goto error;
  1254. tmp = display;
  1255. tmp += sprintf(tmp, "PID\t\tCPU\t\tASID\t\tNAME\n");
  1256. tmp += sprintf(tmp, "---\t\t---\t\t----\t\t----\n");
  1257. while (temp) {
  1258. if (temp->status) {
  1259. if (temp->context)
  1260. tmp +=
  1261. sprintf(tmp,
  1262. "%" PRIu32 "\t\t%" PRIu32 "\t\t%" PRIx32 "\t\t%s\n",
  1263. temp->pid, temp->oncpu,
  1264. temp->asid, temp->name);
  1265. else
  1266. tmp +=
  1267. sprintf(tmp,
  1268. "%" PRIu32 "\t\t%" PRIu32 "\t\t%" PRIx32 "\t\t%s\n",
  1269. temp->pid, temp->oncpu,
  1270. temp->asid, temp->name);
  1271. }
  1272. temp = temp->next;
  1273. }
  1274. return display;
  1275. }
  1276. error:
  1277. display = calloc(40, 1);
  1278. sprintf(display, "linux_ps_command failed\n");
  1279. return display;
  1280. }