shim_debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. #ifndef DEBUG
  30. void clean_link_map_list (void)
  31. {
  32. /* do nothing */
  33. }
  34. void remove_r_debug (void * addr)
  35. {
  36. /* do nothing */
  37. }
  38. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  39. {
  40. /* do nothing */
  41. }
  42. #else /* !DEBUG */
  43. struct gdb_link_map
  44. {
  45. void * l_addr;
  46. char * l_name;
  47. void * l_ld;
  48. struct gdb_link_map *l_next, *l_prev;
  49. };
  50. static struct gdb_link_map * link_map_list = NULL;
  51. void clean_link_map_list (void)
  52. {
  53. if (!link_map_list)
  54. return;
  55. if (link_map_list->l_prev)
  56. link_map_list->l_prev->l_next = NULL;
  57. struct gdb_link_map * m = link_map_list;
  58. for ( ; m ; m = m->l_next) {
  59. DkDebugDetachBinary(m->l_addr);
  60. free(m);
  61. }
  62. link_map_list = NULL;
  63. }
  64. void remove_r_debug (void * addr)
  65. {
  66. struct gdb_link_map * m = link_map_list;
  67. for ( ; m ; m = m->l_next)
  68. if (m->l_addr == addr)
  69. break;
  70. if (!m)
  71. return;
  72. debug("remove a library for gdb: %s\n", m->l_name);
  73. if (m->l_prev)
  74. m->l_prev->l_next = m->l_next;
  75. if (m->l_next)
  76. m->l_next->l_prev = m->l_prev;
  77. DkDebugDetachBinary(addr);
  78. }
  79. void append_r_debug (const char * uri, void * addr, void * dyn_addr)
  80. {
  81. struct gdb_link_map * new = malloc(sizeof(struct gdb_link_map));
  82. if (!new)
  83. return;
  84. int uri_len = strlen(uri);
  85. char * new_uri = malloc(uri_len + 1);
  86. if (!new_uri) {
  87. free(new);
  88. return;
  89. }
  90. memcpy(new_uri, uri, uri_len + 1);
  91. new->l_addr = addr;
  92. new->l_ld = dyn_addr;
  93. new->l_name = new_uri;
  94. struct gdb_link_map *prev = NULL;
  95. struct gdb_link_map **tail = &link_map_list;
  96. while (*tail) {
  97. prev = *tail;
  98. tail = &(*tail)->l_next;
  99. }
  100. debug("add a library for gdb: %s\n", uri);
  101. new->l_prev = prev;
  102. new->l_next = NULL;
  103. *tail = new;
  104. DkDebugAttachBinary(uri, addr);
  105. }
  106. BEGIN_CP_FUNC(gdb_map)
  107. {
  108. struct gdb_link_map *m = link_map_list;
  109. struct gdb_link_map *newm = NULL;
  110. while (m) {
  111. ptr_t off = ADD_CP_OFFSET(sizeof(struct gdb_link_map));
  112. newm = (struct gdb_link_map *) (base + off);
  113. memcpy(newm, m, sizeof(struct gdb_link_map));
  114. newm->l_prev = newm->l_next = NULL;
  115. int len = strlen(newm->l_name);
  116. newm->l_name = (char *) (base + ADD_CP_OFFSET(len + 1));
  117. memcpy(newm->l_name, m->l_name, len + 1);
  118. ADD_CP_FUNC_ENTRY(off);
  119. m = m->l_next;
  120. }
  121. }
  122. END_CP_FUNC(gdb_map)
  123. BEGIN_RS_FUNC(gdb_map)
  124. {
  125. struct gdb_link_map * map = (void *) (base + GET_CP_FUNC_ENTRY());
  126. CP_REBASE(map->l_name);
  127. CP_REBASE(map->l_prev);
  128. CP_REBASE(map->l_next);
  129. struct gdb_link_map *prev = NULL;
  130. struct gdb_link_map **tail = &link_map_list;
  131. while (*tail) {
  132. prev = *tail;
  133. tail = &(*tail)->l_next;
  134. }
  135. map->l_prev = prev;
  136. *tail = map;
  137. DkDebugAttachBinary(map->l_name, map->l_addr);
  138. DEBUG_RS("base=%p,name=%s", map->l_addr, map->l_name);
  139. }
  140. END_RS_FUNC(gdb_map)
  141. #endif