debugger_support.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "debugger_support.h"
  32. #include "se_trace.h"
  33. #include "util.h"
  34. #include "se_debugger_lib.h"
  35. #include "rts.h"
  36. #include "thread_data.h"
  37. #include "se_memory.h"
  38. #include "enclave.h"
  39. #include <string.h>
  40. #include <util.h>
  41. #include <pthread.h>
  42. #define fastcall __attribute__((regparm(3)))
  43. extern "C" void fastcall sgx_debug_load_state_add_element(const debug_enclave_info_t *enclave_info, debug_enclave_info_t** g_debug_enclave_info_list);
  44. extern "C" void fastcall sgx_debug_unload_state_remove_element(const debug_enclave_info_t *enclave_info, debug_enclave_info_t** pre_enclave_info, debug_enclave_info_t* next_enclave_info);
  45. static pthread_mutex_t g_debug_info_mutex = PTHREAD_MUTEX_INITIALIZER;
  46. debug_enclave_info_t *g_debug_enclave_info_list = NULL;
  47. // There is no need to add lock here:
  48. // This function only be called by CEnclave::add_thread and the enclave_info is a member
  49. // of CEnclave instance. There is no race condition
  50. void insert_debug_tcs_info_head(debug_enclave_info_t* enclave_info, debug_tcs_info_t* tcs_info)
  51. {
  52. tcs_info->next_tcs_info = enclave_info->tcs_list;
  53. enclave_info->tcs_list = tcs_info;
  54. }
  55. static void insert_debug_info_head(debug_enclave_info_t *enclave_info)
  56. {
  57. enclave_info->next_enclave_info = g_debug_enclave_info_list;
  58. //g_debug_enclave_info_list = enclave_info;
  59. //To avoid the race between attach event and load event, we set load event bp at where the list is changed
  60. sgx_debug_load_state_add_element(enclave_info, &g_debug_enclave_info_list);
  61. }
  62. static void remove_debug_info(debug_enclave_info_t *enclave_info)
  63. {
  64. debug_enclave_info_t **pre_entry = &g_debug_enclave_info_list;
  65. debug_enclave_info_t *cur = g_debug_enclave_info_list;
  66. while(cur)
  67. {
  68. if(cur == enclave_info)
  69. {
  70. //*pre_entry = cur->next_enclave_info;
  71. //To avoid the race between attach event and unload event, we set unload event bp at where the list is changed
  72. sgx_debug_unload_state_remove_element(enclave_info, pre_entry, cur->next_enclave_info);
  73. break;
  74. }
  75. pre_entry = &cur->next_enclave_info;
  76. cur = cur->next_enclave_info;
  77. }
  78. }
  79. void generate_enclave_debug_event(uint32_t code, const debug_enclave_info_t* enclave_info)
  80. {
  81. if(URTS_EXCEPTION_POSTINITENCLAVE == code)
  82. {
  83. if(pthread_mutex_lock(&g_debug_info_mutex) != 0)
  84. abort();
  85. insert_debug_info_head(const_cast<debug_enclave_info_t *>(enclave_info));
  86. if(pthread_mutex_unlock(&g_debug_info_mutex) != 0)
  87. abort();
  88. }
  89. else if(URTS_EXCEPTION_PREREMOVEENCLAVE == code)
  90. {
  91. if(pthread_mutex_lock(&g_debug_info_mutex) != 0)
  92. abort();
  93. remove_debug_info(const_cast<debug_enclave_info_t *>(enclave_info));
  94. if(pthread_mutex_unlock(&g_debug_info_mutex) != 0)
  95. abort();
  96. }
  97. }
  98. extern "C" void fastcall notify_gdb_to_update(void* base, tcs_t* tcs, uintptr_t of)
  99. {
  100. UNUSED(base);
  101. UNUSED(tcs);
  102. UNUSED(of);
  103. }
  104. extern "C" void push_ocall_frame(uintptr_t frame_point, tcs_t* tcs, CTrustThread *trust_thread)
  105. {
  106. assert(trust_thread != NULL);
  107. CEnclave* enclave = trust_thread->get_enclave();
  108. assert(enclave != NULL);
  109. enclave->push_ocall_frame(container_of(frame_point, ocall_frame_t, xbp), trust_thread);
  110. notify_gdb_to_update(enclave->get_start_address(), tcs, (uintptr_t)container_of(frame_point, ocall_frame_t, xbp));
  111. }
  112. extern "C" void pop_ocall_frame(tcs_t* tcs, CTrustThread *trust_thread)
  113. {
  114. UNUSED(tcs);
  115. assert(trust_thread != NULL);
  116. CEnclave* enclave = trust_thread->get_enclave();
  117. assert(enclave != NULL);
  118. enclave->pop_ocall_frame(trust_thread);
  119. }