| 1 |
// Copyright (c) 2011-2016 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#ifndef WINPTY_ASSERT_H |
| 22 |
#define WINPTY_ASSERT_H |
| 23 |
|
| 24 |
#ifdef WINPTY_AGENT_ASSERT |
| 25 |
|
| 26 |
void agentShutdown(); |
| 27 |
void agentAssertFail(const char *file, int line, const char *cond); |
| 28 |
|
| 29 |
// Calling the standard assert() function does not work in the agent because |
| 30 |
// the error message would be printed to the console, and the only way the |
| 31 |
// user can see the console is via a working agent! Moreover, the console may |
| 32 |
// be frozen, so attempting to write to it would block forever. This custom |
| 33 |
// assert function instead sends the message to the DebugServer, then attempts |
| 34 |
// to close the console, then quietly exits. |
| 35 |
#define ASSERT(cond) \ |
| 36 |
do { \ |
| 37 |
if (!(cond)) { \ |
| 38 |
agentAssertFail(__FILE__, __LINE__, #cond); \ |
| 39 |
} \ |
| 40 |
} while(0) |
| 41 |
|
| 42 |
#else |
| 43 |
|
| 44 |
void assertTrace(const char *file, int line, const char *cond); |
| 45 |
|
| 46 |
// In the other targets, log the assert failure to the debugserver, then fail |
| 47 |
// using the ordinary assert mechanism. In case assert is compiled out, fail |
| 48 |
// using abort. The amount of code inlined is unfortunate, but asserts aren't |
| 49 |
// used much outside the agent. |
| 50 |
#include <assert.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#define ASSERT_CONDITION(cond) (false && (cond)) |
| 53 |
#define ASSERT(cond) \ |
| 54 |
do { \ |
| 55 |
if (!(cond)) { \ |
| 56 |
assertTrace(__FILE__, __LINE__, #cond); \ |
| 57 |
assert(ASSERT_CONDITION(#cond)); \ |
| 58 |
abort(); \ |
| 59 |
} \ |
| 60 |
} while(0) |
| 61 |
|
| 62 |
#endif |
| 63 |
|
| 64 |
#endif // WINPTY_ASSERT_H |