OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <cassert> | 10 #include <cassert> |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 CheckOpResult(std::string* message) : message_(message) {} | 489 CheckOpResult(std::string* message) : message_(message) {} |
490 // Returns true if the check succeeded. | 490 // Returns true if the check succeeded. |
491 operator bool() const { return !message_; } | 491 operator bool() const { return !message_; } |
492 // Returns the message. | 492 // Returns the message. |
493 std::string* message() { return message_; } | 493 std::string* message() { return message_; } |
494 | 494 |
495 private: | 495 private: |
496 std::string* message_; | 496 std::string* message_; |
497 }; | 497 }; |
498 | 498 |
499 // Crashes in the fastest possible way with no attempt at logging. | 499 // Crashes in the fastest, simplest possible way with no attempt at logging. |
500 // There are different constraints to satisfy here, see http://crbug.com/664209 | |
501 // for more context: | |
502 // - The trap instructions, and hence the PC value at crash time, have to be | |
503 // distinct and not get folded into the same opcode by the compiler. | |
504 // On Linux/Android this is tricky because GCC still folds identical | |
505 // asm volatile blocks. The workaround is generating distinct opcodes for | |
506 // each CHECK using the __COUNTER__ macro. | |
507 // - The debug info for the trap instruction has to be attributed to the source | |
508 // line that has the CHECK(), to make crash reports actionable. This rules | |
509 // out the ability of using a inline function, at least as long as clang | |
510 // doesn't support attribute(artificial). | |
511 // - Failed CHECKs should produce a signal that is distinguishable from an | |
512 // invalid memory access, to improve the actionability of crash reports. | |
513 // - The compiler should treat the CHECK as no-return instructions, so that the | |
514 // trap code can be efficiently packed in the prologue of the function and | |
515 // doesn't interfere with the main execution flow. | |
516 // - When debugging, developers shouldn't be able to accidentally step over a | |
517 // CHECK. This is achieved by putting opcodes that will cause a non | |
518 // continuable exception after the actual trap instruction. | |
519 // - Don't cause too much binary bloat. | |
520 #if defined(COMPILER_GCC) | 500 #if defined(COMPILER_GCC) |
521 | 501 #define IMMEDIATE_CRASH() __builtin_trap() |
522 #if defined(ARCH_CPU_X86_FAMILY) | |
523 // int 3 will generate a SIGTRAP. | |
524 #define TRAP_SEQUENCE() \ | |
525 asm volatile( \ | |
526 "int3; ud2; push %0;" ::"i"(static_cast<unsigned char>(__COUNTER__))) | |
527 | |
528 #elif defined(ARCH_CPU_ARMEL) | |
529 // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running | |
530 // as a 32 bit userspace app on arm64. There doesn't seem to be any way to | |
531 // cause a SIGTRAP from userspace without using a syscall (which would be a | |
532 // problem for sandboxing). | |
533 #define TRAP_SEQUENCE() \ | |
534 asm volatile("bkpt #0; udf %0;" ::"i"(__COUNTER__ % 256)) | |
535 | |
536 #elif defined(ARCH_CPU_ARM64) | |
537 | |
538 // This will always generate a SIGTRAP on arm64. | |
539 #define TRAP_SEQUENCE() \ | |
540 asm volatile("brk #0; hlt %0;" ::"i"(__COUNTER__ % 65536)) | |
541 #else | |
542 // Crash report accuracy will not be guaranteed on other architectures, but at | |
543 // least this will crash as expected. | |
544 #define TRAP_SEQUENCE() __builtin_trap() | |
545 #endif | |
546 | |
547 #define IMMEDIATE_CRASH() \ | |
548 ({ \ | |
549 TRAP_SEQUENCE(); \ | |
550 __builtin_unreachable(); \ | |
551 }) | |
552 | |
553 #elif defined(COMPILER_MSVC) | 502 #elif defined(COMPILER_MSVC) |
554 #define IMMEDIATE_CRASH() __debugbreak() | 503 #define IMMEDIATE_CRASH() __debugbreak() |
555 #else | 504 #else |
556 #error Port | 505 #error Port |
557 #endif | 506 #endif |
558 | 507 |
559 // CHECK dies with a fatal error if condition is not true. It is *not* | 508 // CHECK dies with a fatal error if condition is not true. It is *not* |
560 // controlled by NDEBUG, so the check will be executed regardless of | 509 // controlled by NDEBUG, so the check will be executed regardless of |
561 // compilation mode. | 510 // compilation mode. |
562 // | 511 // |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 #elif NOTIMPLEMENTED_POLICY == 5 | 1057 #elif NOTIMPLEMENTED_POLICY == 5 |
1109 #define NOTIMPLEMENTED() do {\ | 1058 #define NOTIMPLEMENTED() do {\ |
1110 static bool logged_once = false;\ | 1059 static bool logged_once = false;\ |
1111 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1060 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
1112 logged_once = true;\ | 1061 logged_once = true;\ |
1113 } while(0);\ | 1062 } while(0);\ |
1114 EAT_STREAM_PARAMETERS | 1063 EAT_STREAM_PARAMETERS |
1115 #endif | 1064 #endif |
1116 | 1065 |
1117 #endif // BASE_LOGGING_H_ | 1066 #endif // BASE_LOGGING_H_ |
OLD | NEW |