shim_debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <shim_internal.h>
  19. #include <shim_tls.h>
  20. #include <shim_handle.h>
  21. #include <shim_vma.h>
  22. #include <shim_checkpoint.h>
  23. #include <shim_fs.h>
  24. #include <shim_ipc.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #ifndef DEBUG
  28. void clean_link_map_list (void)
  29. {
  30. /* do nothing */
  31. }
  32. void remove_r_debug (void * addr)
  33. {
  34. __UNUSED(addr);
  35. /* do nothing */
  36. }
  37. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  38. {
  39. __UNUSED(uri);
  40. __UNUSED(addr);
  41. __UNUSED(dyn_addr);
  42. /* do nothing */
  43. }
  44. #else /* !DEBUG */
  45. struct gdb_link_map
  46. {
  47. void * l_addr;
  48. char * l_name;
  49. void * l_ld;
  50. struct gdb_link_map *l_next, *l_prev;
  51. };
  52. /* XXX: What lock protects this? vma_list_lock? */
  53. static struct gdb_link_map * link_map_list = NULL;
  54. void clean_link_map_list (void)
  55. {
  56. if (!link_map_list)
  57. return;
  58. if (link_map_list->l_prev)
  59. link_map_list->l_prev->l_next = NULL;
  60. struct gdb_link_map * m = link_map_list;
  61. for ( ; m ; m = m->l_next) {
  62. DkDebugDetachBinary(m->l_addr);
  63. free(m);
  64. }
  65. link_map_list = NULL;
  66. }
  67. void remove_r_debug (void * addr)
  68. {
  69. struct gdb_link_map * m = link_map_list;
  70. for ( ; m ; m = m->l_next)
  71. if (m->l_addr == addr)
  72. break;
  73. if (!m)
  74. return;
  75. debug("remove a library for gdb: %s\n", m->l_name);
  76. if (m->l_prev)
  77. m->l_prev->l_next = m->l_next;
  78. if (m->l_next)
  79. m->l_next->l_prev = m->l_prev;
  80. DkDebugDetachBinary(addr);
  81. }
  82. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  83. {
  84. struct gdb_link_map * new = malloc(sizeof(struct gdb_link_map));
  85. if (!new)
  86. return;
  87. int uri_len = strlen(uri);
  88. char * new_uri = malloc(uri_len + 1);
  89. if (!new_uri) {
  90. free(new);
  91. return;
  92. }
  93. memcpy(new_uri, uri, uri_len + 1);
  94. new->l_addr = addr;
  95. new->l_ld = dyn_addr;
  96. new->l_name = new_uri;
  97. struct gdb_link_map *prev = NULL;
  98. struct gdb_link_map **tail = &link_map_list;
  99. while (*tail) {
  100. prev = *tail;
  101. tail = &(*tail)->l_next;
  102. }
  103. debug("add a library for gdb: %s\n", uri);
  104. new->l_prev = prev;
  105. new->l_next = NULL;
  106. *tail = new;
  107. DkDebugAttachBinary(uri, addr);
  108. }
  109. BEGIN_CP_FUNC(gdb_map)
  110. {
  111. __UNUSED(obj);
  112. __UNUSED(size);
  113. __UNUSED(objp);
  114. struct gdb_link_map *m = link_map_list;
  115. struct gdb_link_map *newm = NULL;
  116. while (m) {
  117. ptr_t off = ADD_CP_OFFSET(sizeof(struct gdb_link_map));
  118. newm = (struct gdb_link_map *) (base + off);
  119. memcpy(newm, m, sizeof(struct gdb_link_map));
  120. newm->l_prev = newm->l_next = NULL;
  121. size_t len = strlen(newm->l_name);
  122. newm->l_name = (char *) (base + ADD_CP_OFFSET(len + 1));
  123. memcpy(newm->l_name, m->l_name, len + 1);
  124. ADD_CP_FUNC_ENTRY(off);
  125. m = m->l_next;
  126. }
  127. }
  128. END_CP_FUNC(gdb_map)
  129. BEGIN_RS_FUNC(gdb_map)
  130. {
  131. __UNUSED(offset);
  132. struct gdb_link_map * map = (void *) (base + GET_CP_FUNC_ENTRY());
  133. CP_REBASE(map->l_name);
  134. CP_REBASE(map->l_prev);
  135. CP_REBASE(map->l_next);
  136. struct gdb_link_map *prev = NULL;
  137. struct gdb_link_map **tail = &link_map_list;
  138. while (*tail) {
  139. prev = *tail;
  140. tail = &(*tail)->l_next;
  141. }
  142. map->l_prev = prev;
  143. *tail = map;
  144. DkDebugAttachBinary(map->l_name, map->l_addr);
  145. DEBUG_RS("base=%p,name=%s", map->l_addr, map->l_name);
  146. }
  147. END_RS_FUNC(gdb_map)
  148. #endif