shim_debug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* do nothing */
  35. }
  36. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  37. {
  38. /* do nothing */
  39. }
  40. #else /* !DEBUG */
  41. struct gdb_link_map
  42. {
  43. void * l_addr;
  44. char * l_name;
  45. void * l_ld;
  46. struct gdb_link_map *l_next, *l_prev;
  47. };
  48. static struct gdb_link_map * link_map_list = NULL;
  49. void clean_link_map_list (void)
  50. {
  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. {
  64. struct gdb_link_map * m = link_map_list;
  65. for ( ; m ; m = m->l_next)
  66. if (m->l_addr == addr)
  67. break;
  68. if (!m)
  69. return;
  70. debug("remove a library for gdb: %s\n", m->l_name);
  71. if (m->l_prev)
  72. m->l_prev->l_next = m->l_next;
  73. if (m->l_next)
  74. m->l_next->l_prev = m->l_prev;
  75. DkDebugDetachBinary(addr);
  76. }
  77. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  78. {
  79. struct gdb_link_map * new = malloc(sizeof(struct gdb_link_map));
  80. if (!new)
  81. return;
  82. int uri_len = strlen(uri);
  83. char * new_uri = malloc(uri_len + 1);
  84. if (!new_uri) {
  85. free(new);
  86. return;
  87. }
  88. memcpy(new_uri, uri, uri_len + 1);
  89. new->l_addr = addr;
  90. new->l_ld = dyn_addr;
  91. new->l_name = new_uri;
  92. struct gdb_link_map *prev = NULL;
  93. struct gdb_link_map **tail = &link_map_list;
  94. while (*tail) {
  95. prev = *tail;
  96. tail = &(*tail)->l_next;
  97. }
  98. debug("add a library for gdb: %s\n", uri);
  99. new->l_prev = prev;
  100. new->l_next = NULL;
  101. *tail = new;
  102. DkDebugAttachBinary(uri, addr);
  103. }
  104. BEGIN_CP_FUNC(gdb_map)
  105. {
  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. {
  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