shim_debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_debug.c
  15. *
  16. * This file contains codes for registering libraries to GDB.
  17. */
  18. #include <pal.h>
  19. #include <pal_error.h>
  20. #include <shim_checkpoint.h>
  21. #include <shim_fs.h>
  22. #include <shim_handle.h>
  23. #include <shim_internal.h>
  24. #include <shim_ipc.h>
  25. #include <shim_tls.h>
  26. #include <shim_vma.h>
  27. #ifndef DEBUG
  28. void clean_link_map_list(void) {
  29. /* do nothing */
  30. }
  31. void remove_r_debug(void* addr) {
  32. __UNUSED(addr);
  33. /* do nothing */
  34. }
  35. void append_r_debug(const char* uri, void* addr, void* dyn_addr) {
  36. __UNUSED(uri);
  37. __UNUSED(addr);
  38. __UNUSED(dyn_addr);
  39. /* do nothing */
  40. }
  41. #else /* !DEBUG */
  42. struct gdb_link_map {
  43. void* l_addr;
  44. char* l_name;
  45. void* l_ld;
  46. struct gdb_link_map *l_next, *l_prev;
  47. };
  48. /* XXX: What lock protects this? vma_list_lock? */
  49. static struct gdb_link_map* link_map_list = NULL;
  50. void clean_link_map_list(void) {
  51. if (!link_map_list)
  52. return;
  53. if (link_map_list->l_prev)
  54. link_map_list->l_prev->l_next = NULL;
  55. struct gdb_link_map* m = link_map_list;
  56. for (; m; m = m->l_next) {
  57. DkDebugDetachBinary(m->l_addr);
  58. free(m);
  59. }
  60. link_map_list = NULL;
  61. }
  62. void remove_r_debug(void* addr) {
  63. struct gdb_link_map* m = link_map_list;
  64. for (; m; m = m->l_next)
  65. if (m->l_addr == addr)
  66. break;
  67. if (!m)
  68. return;
  69. debug("remove a library for gdb: %s\n", m->l_name);
  70. if (m->l_prev)
  71. m->l_prev->l_next = m->l_next;
  72. if (m->l_next)
  73. m->l_next->l_prev = m->l_prev;
  74. DkDebugDetachBinary(addr);
  75. }
  76. void append_r_debug(const char* uri, void* addr, void* dyn_addr) {
  77. struct gdb_link_map* new = malloc(sizeof(struct gdb_link_map));
  78. if (!new)
  79. return;
  80. int uri_len = strlen(uri);
  81. char* new_uri = malloc(uri_len + 1);
  82. if (!new_uri) {
  83. free(new);
  84. return;
  85. }
  86. memcpy(new_uri, uri, uri_len + 1);
  87. new->l_addr = addr;
  88. new->l_ld = dyn_addr;
  89. new->l_name = new_uri;
  90. struct gdb_link_map* prev = NULL;
  91. struct gdb_link_map** tail = &link_map_list;
  92. while (*tail) {
  93. prev = *tail;
  94. tail = &(*tail)->l_next;
  95. }
  96. debug("add a library for gdb: %s\n", uri);
  97. new->l_prev = prev;
  98. new->l_next = NULL;
  99. *tail = new;
  100. DkDebugAttachBinary(uri, addr);
  101. }
  102. BEGIN_CP_FUNC(gdb_map) {
  103. __UNUSED(obj);
  104. __UNUSED(size);
  105. __UNUSED(objp);
  106. struct gdb_link_map* m = link_map_list;
  107. struct gdb_link_map* newm = NULL;
  108. while (m) {
  109. ptr_t off = ADD_CP_OFFSET(sizeof(struct gdb_link_map));
  110. newm = (struct gdb_link_map*)(base + off);
  111. memcpy(newm, m, sizeof(struct gdb_link_map));
  112. newm->l_prev = newm->l_next = NULL;
  113. size_t len = strlen(newm->l_name);
  114. newm->l_name = (char*)(base + ADD_CP_OFFSET(len + 1));
  115. memcpy(newm->l_name, m->l_name, len + 1);
  116. ADD_CP_FUNC_ENTRY(off);
  117. m = m->l_next;
  118. }
  119. }
  120. END_CP_FUNC(gdb_map)
  121. BEGIN_RS_FUNC(gdb_map) {
  122. __UNUSED(offset);
  123. struct gdb_link_map* map = (void*)(base + GET_CP_FUNC_ENTRY());
  124. CP_REBASE(map->l_name);
  125. CP_REBASE(map->l_prev);
  126. CP_REBASE(map->l_next);
  127. struct gdb_link_map* prev = NULL;
  128. struct gdb_link_map** tail = &link_map_list;
  129. while (*tail) {
  130. prev = *tail;
  131. tail = &(*tail)->l_next;
  132. }
  133. map->l_prev = prev;
  134. *tail = map;
  135. DkDebugAttachBinary(map->l_name, map->l_addr);
  136. DEBUG_RS("base=%p,name=%s", map->l_addr, map->l_name);
  137. }
  138. END_RS_FUNC(gdb_map)
  139. #endif