shim_debug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_debug.c
  17. *
  18. * This file contains codes for registering libraries to GDB.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_tls.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_fs.h>
  26. #include <shim_ipc.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <fcntl.h>
  30. struct gdb_link_map
  31. {
  32. void * l_addr;
  33. char * l_name;
  34. void * l_ld;
  35. struct gdb_link_map *l_next, *l_prev;
  36. };
  37. static struct gdb_link_map * link_map_list = NULL;
  38. void clean_link_map_list (void)
  39. {
  40. if (!link_map_list)
  41. return;
  42. if (link_map_list->l_prev)
  43. link_map_list->l_prev->l_next = NULL;
  44. struct gdb_link_map * m = link_map_list;
  45. for ( ; m ; m = m->l_next) {
  46. DkDebugDetachBinary(m->l_addr);
  47. free(m);
  48. }
  49. link_map_list = NULL;
  50. }
  51. void remove_r_debug (void * addr)
  52. {
  53. struct gdb_link_map * m = link_map_list;
  54. for ( ; m ; m = m->l_next)
  55. if (m->l_addr == addr)
  56. break;
  57. if (!m)
  58. return;
  59. debug("remove a library for gdb: %s\n", m->l_name);
  60. if (m->l_prev)
  61. m->l_prev->l_next = m->l_next;
  62. if (m->l_next)
  63. m->l_next->l_prev = m->l_prev;
  64. DkDebugDetachBinary(addr);
  65. }
  66. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  67. {
  68. struct gdb_link_map * new = malloc(sizeof(struct gdb_link_map));
  69. int uri_len = strlen(uri);
  70. char * new_uri = malloc(uri_len + 1);
  71. memcpy(new_uri, uri, uri_len + 1);
  72. new->l_addr = addr;
  73. new->l_ld = dyn_addr;
  74. new->l_name = new_uri;
  75. struct gdb_link_map *prev = NULL;
  76. struct gdb_link_map **tail = &link_map_list;
  77. while (*tail) {
  78. prev = *tail;
  79. tail = &(*tail)->l_next;
  80. }
  81. debug("add a library for gdb: %s\n", uri);
  82. new->l_prev = prev;
  83. new->l_next = NULL;
  84. *tail = new;
  85. DkDebugAttachBinary(uri, addr);
  86. }
  87. DEFINE_MIGRATE_FUNC(gdb_map)
  88. MIGRATE_FUNC_BODY(gdb_map)
  89. {
  90. struct gdb_link_map *m = link_map_list;
  91. struct gdb_link_map *newm = NULL;
  92. while (m) {
  93. unsigned long off = ADD_OFFSET(sizeof(struct gdb_link_map));
  94. if (!dry) {
  95. newm = (struct gdb_link_map *) (base + off);
  96. memcpy(newm, m, sizeof(struct gdb_link_map));
  97. newm->l_prev = newm->l_next = NULL;
  98. }
  99. ADD_OFFSET(strlen(m->l_name) + 1);
  100. if (!dry) {
  101. newm->l_name = (char *) (base + *offset);
  102. memcpy(newm->l_name, m->l_name, strlen(m->l_name) + 1);
  103. }
  104. ADD_FUNC_ENTRY(off);
  105. m = m->l_next;
  106. }
  107. }
  108. END_MIGRATE_FUNC
  109. RESUME_FUNC_BODY(gdb_map)
  110. {
  111. uint64_t off = GET_FUNC_ENTRY();
  112. struct gdb_link_map *map = (struct gdb_link_map *) (base + off);
  113. RESUME_REBASE(map->l_name);
  114. RESUME_REBASE(map->l_prev);
  115. RESUME_REBASE(map->l_next);
  116. struct gdb_link_map *prev = NULL;
  117. struct gdb_link_map **tail = &link_map_list;
  118. while (*tail) {
  119. prev = *tail;
  120. tail = &(*tail)->l_next;
  121. }
  122. map->l_prev = prev;
  123. *tail = map;
  124. #ifdef DEBUG_RESUME
  125. debug("gdb: %s loaded at %p\n", map->l_name, map->l_addr);
  126. #endif
  127. DkDebugAttachBinary(map->l_name, map->l_addr);
  128. }
  129. END_RESUME_FUNC