Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / service_runtime / nacl_assert.h
blobafc937c91b00b416521a8a3ab48ea529082df0ee
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl service runtime, assertion macros.
35 * THIS FILE SHOULD BE USED ONLY IN TEST CODE.
37 #ifndef NATIVE_CLIENT_SERVICE_RUNTIME_NACL_ASSERT_H_
38 #define NATIVE_CLIENT_SERVICE_RUNTIME_NACL_ASSERT_H_
40 #include "native_client/include/portability.h"
41 /* get PRIxPTR */
44 * Instead of using <assert.h>, we define a version that works with
45 * our testing framework, printing out FAIL / SUCCESS to standard
46 * output and then exiting with a non-zero exit status.
49 #define ASSERT(bool_expr) do { \
50 if (!(bool_expr)) { \
51 fprintf(stderr, \
52 "Error at line %d, %s: " #bool_expr " is FALSE\n", \
53 __LINE__, __FILE__); \
54 printf("FAIL\n"); \
55 exit(1); \
56 } \
57 } while (0)
59 #define ASSERT_MSG(bool_expr, msg) do { \
60 if (!(bool_expr)) { \
61 fprintf(stderr, \
62 "Error at line %d, %s: " #bool_expr " is FALSE\n", \
63 __LINE__, __FILE__); \
64 fprintf(stderr, "%s\n", msg); \
65 printf("FAIL\n"); \
66 exit(1); \
67 } \
68 } while (0)
70 #define ASSERT_OP_THUNK(lhs, op, rhs, slhs, srhs, thunk) do { \
71 if (!(lhs op rhs)) { \
72 fprintf(stderr, \
73 "Error at line %d, %s:\n" \
74 "Error: %s "#op" %s" \
75 " is FALSE\n", \
76 __LINE__, __FILE__, slhs, srhs); \
77 fprintf(stderr, \
78 "got 0x%08"PRIxPTR" (%"PRIdPTR"); " \
79 "comparison value 0x%08"PRIxPTR" (%"PRIdPTR")\n", \
80 (uintptr_t) lhs, (uintptr_t) lhs, \
81 (uintptr_t) rhs, (uintptr_t) rhs); \
82 thunk; \
83 printf("FAIL\n"); \
84 exit(1); \
85 } \
86 } while (0)
88 #define ASSERT_OP_MSG(lhs, op, rhs, slhs, srhs, msg) \
89 ASSERT_OP_THUNK(lhs, op, rhs, \
90 slhs, srhs, do { fprintf(stderr, "%s\n", msg); } while (0))
92 #define ASSERT_EQ_MSG(lhs, rhs, msg) \
93 ASSERT_OP_MSG(lhs, ==, rhs, #lhs, #rhs, msg)
94 #define ASSERT_NE_MSG(lhs, rhs, msg) \
95 ASSERT_OP_MSG(lhs, !=, rhs, #lhs, #rhs, msg)
96 #define ASSERT_LE_MSG(lhs, rhs, msg) \
97 ASSERT_OP_MSG(lhs, <=, rhs, #lhs, #rhs, msg)
99 #define ASSERT_OP(lhs, op, rhs, slhs, srhs) \
100 ASSERT_OP_THUNK(lhs, op, rhs, slhs, srhs, do { ; } while (0))
102 #define ASSERT_EQ(lhs, rhs) ASSERT_OP(lhs, ==, rhs, #lhs, #rhs)
103 #define ASSERT_NE(lhs, rhs) ASSERT_OP(lhs, !=, rhs, #lhs, #rhs)
104 #define ASSERT_LE(lhs, rhs) ASSERT_OP(lhs, <=, rhs, #lhs, #rhs)
105 #define ASSERT_GT(lhs, rhs) ASSERT_OP(lhs, >, rhs, #lhs, #rhs)
106 #define ASSERT_GE(lhs, rhs) ASSERT_OP(lhs, >=, rhs, #lhs, #rhs)
108 #endif /* NATIVE_CLIENT_SERVICE_RUNTIME_NACL_ASSERT_H_ */