Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
| 2 * Use of this source code is governed by a BSD-style license that can be | |
| 3 * found in the LICENSE file. | |
| 4 */ | |
| 5 | |
| 6 #include "native_client/src/trusted/service_runtime/nacl_oop_debugger_hooks.h" | |
| 7 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | |
| 8 | |
| 9 #ifdef _WIN32 /* _WIN32 is defined on both 32-bit and 64-bit Windows */ | |
|
Mark Seaborn
2011/06/13 19:40:51
Use NACL_WINDOWS instead.
garianov1
2011/06/13 20:09:10
Done.
| |
| 10 /* Note: this function does not do anything if program | |
| 11 * is not running under debugger. | |
| 12 * If program (sel_ldr) is running under debugger, calling this function | |
| 13 * gives debugger a chance to modify state of the debuggee before | |
| 14 * resuming debuggee execution. | |
| 15 */ | |
| 16 void SendMessageToDebuggerAndHalt(const char* fmt, ...); | |
| 17 | |
| 18 void NaClOopDebuggerAppCreateHook(struct NaClApp* nap) { | |
| 19 if (NULL == nap) | |
| 20 return; | |
| 21 SendMessageToDebuggerAndHalt( | |
| 22 "-event AppCreate -nap %p -mem_start %p -user_entry_pt %p " | |
| 23 "-initial_entry_pt %p", | |
| 24 nap, | |
| 25 (void*)nap->mem_start, | |
|
Mark Seaborn
2011/06/13 19:40:51
Style in this component is "(void *) foo".
garianov1
2011/06/13 20:09:10
Done.
| |
| 26 (void*)nap->user_entry_pt, | |
| 27 (void*)nap->initial_entry_pt); | |
| 28 } | |
| 29 | |
| 30 void NaClOopDebuggerThreadCreateHook(struct NaClAppThread* natp) { | |
| 31 SendMessageToDebuggerAndHalt("-event ThreadCreate -natp %p", natp); | |
| 32 } | |
| 33 | |
| 34 void NaClOopDebuggerThreadExitHook(struct NaClAppThread* natp, int exit_code) { | |
| 35 SendMessageToDebuggerAndHalt("-event ThreadExit -natp %p -exit_code %d", | |
| 36 natp, | |
| 37 exit_code); | |
| 38 } | |
| 39 | |
| 40 void NaClOopDebuggerAppExitHook(int exit_code) { | |
| 41 SendMessageToDebuggerAndHalt("-event AppExit -exit_code %d", exit_code); | |
| 42 } | |
| 43 | |
| 44 #else | |
| 45 /* placeholders for OOP debugger hooks on other platforms */ | |
| 46 void NaClOopDebuggerAppCreateHook(struct NaClApp* nap) { | |
| 47 UNREFERENCED_PARAMETER(nap); | |
| 48 } | |
| 49 | |
| 50 void NaClOopDebuggerThreadCreateHook(struct NaClAppThread* natp, | |
| 51 int exit_code) { | |
| 52 UNREFERENCED_PARAMETER(natp); | |
| 53 UNREFERENCED_PARAMETER(exit_code); | |
| 54 } | |
| 55 void NaClOopDebuggerThreadExitHook(struct NaClAppThread* natp, | |
| 56 int exit_code) { | |
| 57 UNREFERENCED_PARAMETER(natp); | |
| 58 UNREFERENCED_PARAMETER(exit_code); | |
| 59 } | |
| 60 | |
| 61 void NaClOopDebuggerAppExitHook(int exit_code) { | |
| 62 } | |
| 63 #endif | |
| 64 | |
| 65 #ifdef _WIN32 /* _WIN32 is defined on both 32-bit and 64-bit Windows */ | |
| 66 void SendMessageToDebuggerAndHalt(const char* fmt, ...) { | |
| 67 #define kVarMsgSize 2048 | |
|
Mark Seaborn
2011/06/13 19:40:51
Use "const int" (local) or "static const int" (glo
garianov1
2011/06/13 20:09:10
Unfortunately, if I use 'const int', the following
| |
| 68 /* Determines whether the calling process is being debugged by a debugger. | |
|
Mark Seaborn
2011/06/13 19:40:51
Style here is
/*
* comment...
*/
garianov1
2011/06/13 20:09:10
Done.
| |
| 69 * IsDebuggerPresent() is a member of Windows Debugging API: | |
| 70 * http://msdn.microsoft.com/en-us/library/ms680345%28v=VS.85%29.aspx | |
| 71 */ | |
| 72 if (IsDebuggerPresent()) { | |
| 73 /* Prefix has GUID string specific to our OOP debugger, so that it | |
| 74 * can differentiate it from other uses of 'OutputDebugString'. | |
| 75 */ | |
| 76 char prefix[] = "{7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 "; | |
| 77 char msg[sizeof(prefix) + kVarMsgSize]; | |
| 78 char* post_pref_msg = msg + sizeof(prefix) - 1; | |
|
Mark Seaborn
2011/06/13 19:40:51
Style here is "char *foo".
garianov1
2011/06/13 20:09:10
Done.
| |
| 79 signed int res = 0; | |
| 80 va_list marker; | |
| 81 | |
| 82 strcpy(msg, prefix); | |
| 83 va_start(marker, fmt); | |
| 84 res = _vsnprintf_s(post_pref_msg, kVarMsgSize, _TRUNCATE, fmt, marker); | |
| 85 if (-1 != res) { | |
| 86 /* Sends a string to the debugger by raising OUTPUT_DEBUG_STRING_EVENT. | |
| 87 * OutputDebugString() is a member of Windows Debugging API: | |
| 88 * http://msdn.microsoft.com/en-us/library/aa363362%28v=VS.85%29.aspx | |
| 89 * | |
| 90 * It's conceptually eqvivalent to: | |
| 91 * ::RaiseException(MY_OWN_EXCEPTION_CODE, 0, 1, &msg); | |
| 92 * The difference is, raising MY_OWN_EXCEPTION_CODE can cause program | |
| 93 * to crash (if there's no VEH handler and no debugger; or can cause | |
| 94 * debugger to stop program (if debugger does not know about | |
| 95 * MY_OWN_EXCEPTION_CODE). | |
| 96 * | |
| 97 * On the contrary, using OutputDebugString() is safe, cc from MSDN page: | |
| 98 * "If the application has no debugger and the system debugger is not | |
| 99 * active, OutputDebugString does nothing." | |
| 100 */ | |
| 101 OutputDebugStringA(msg); | |
| 102 } | |
| 103 } | |
| 104 #undef kVarMsgSize | |
| 105 } | |
| 106 /* Example of captured sel_ldr -> debugger messages: | |
| 107 | |
| 108 11.62454796 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 109 -event AppCreate -nap 00000000001CD3F0 -mem_start 0000000C00000000 \ | |
| 110 -user_entry_pt 0000000000020080 -initial_entry_pt 0000000008000080 | |
| 111 11.62480354 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 112 -event ThreadCreate -natp 0000000002304C50 | |
| 113 ... | |
| 114 23.96814728 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 115 -event ThreadExit -natp 0000000002304C50 -exit_code 1 | |
| 116 23.96838570 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 117 -event AppExit -exit_code 1 | |
| 118 */ | |
| 119 #endif | |
| 120 | |
| OLD | NEW |