shim_debug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. int uri_len = strlen(uri);
  83. char * new_uri = malloc(uri_len + 1);
  84. memcpy(new_uri, uri, uri_len + 1);
  85. new->l_addr = addr;
  86. new->l_ld = dyn_addr;
  87. new->l_name = new_uri;
  88. struct gdb_link_map *prev = NULL;
  89. struct gdb_link_map **tail = &link_map_list;
  90. while (*tail) {
  91. prev = *tail;
  92. tail = &(*tail)->l_next;
  93. }
  94. debug("add a library for gdb: %s\n", uri);
  95. new->l_prev = prev;
  96. new->l_next = NULL;
  97. *tail = new;
  98. DkDebugAttachBinary(uri, addr);
  99. }
  100. BEGIN_CP_FUNC(gdb_map)
  101. {
  102. struct gdb_link_map *m = link_map_list;
  103. struct gdb_link_map *newm = NULL;
  104. while (m) {
  105. ptr_t off = ADD_CP_OFFSET(sizeof(struct gdb_link_map));
  106. newm = (struct gdb_link_map *) (base + off);
  107. memcpy(newm, m, sizeof(struct gdb_link_map));
  108. newm->l_prev = newm->l_next = NULL;
  109. int len = strlen(newm->l_name);
  110. newm->l_name = (char *) (base + ADD_CP_OFFSET(len + 1));
  111. memcpy(newm->l_name, m->l_name, len + 1);
  112. ADD_CP_FUNC_ENTRY(off);
  113. m = m->l_next;
  114. }
  115. }
  116. END_CP_FUNC(gdb_map)
  117. BEGIN_RS_FUNC(gdb_map)
  118. {
  119. struct gdb_link_map * map = (void *) (base + GET_CP_FUNC_ENTRY());
  120. CP_REBASE(map->l_name);
  121. CP_REBASE(map->l_prev);
  122. CP_REBASE(map->l_next);
  123. struct gdb_link_map *prev = NULL;
  124. struct gdb_link_map **tail = &link_map_list;
  125. while (*tail) {
  126. prev = *tail;
  127. tail = &(*tail)->l_next;
  128. }
  129. map->l_prev = prev;
  130. *tail = map;
  131. DkDebugAttachBinary(map->l_name, map->l_addr);
  132. DEBUG_RS("base=%p,name=%s", map->l_addr, map->l_name);
  133. }
  134. END_RS_FUNC(gdb_map)
  135. #endif