| 1 | /* Copyright (c) 2010, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "my_dbug.h" |
| 27 | #include "unittest/gunit/thread_utils.h" |
| 28 | |
| 29 | using thread::Notification; |
| 30 | using thread::Thread; |
| 31 | |
| 32 | namespace dbug_unittest { |
| 33 | |
| 34 | #if defined(NDEBUG) |
| 35 | TEST(DebugTest, NoSuicide) { DBUG_SUICIDE(); } |
| 36 | #else |
| 37 | TEST(DebugDeathTest, Suicide) { |
| 38 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 39 | EXPECT_DEATH_IF_SUPPORTED(DBUG_SUICIDE(), ""); |
| 40 | } |
| 41 | #endif |
| 42 | |
| 43 | #if !defined(NDEBUG) && !defined(_WIN32) |
| 44 | class DbugGcovThread : public Thread { |
| 45 | public: |
| 46 | DbugGcovThread(Notification *start_notification) |
| 47 | : m_start_notification(start_notification) {} |
| 48 | |
| 49 | virtual void run() { |
| 50 | m_start_notification->notify(); |
| 51 | _db_flush_gcov_(); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | Notification *m_start_notification; |
| 56 | }; |
| 57 | |
| 58 | TEST(DebugFlushGcov, FlushGcovParallel) { |
| 59 | Notification start_notification; |
| 60 | DbugGcovThread debug_thread(&start_notification); |
| 61 | debug_thread.start(); |
| 62 | |
| 63 | // Wait for the other thread to start, then flush in parallel. |
| 64 | start_notification.wait_for_notification(); |
| 65 | _db_flush_gcov_(); |
| 66 | debug_thread.join(); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | #if !defined(NDEBUG) |
| 71 | TEST(DebugPrintTest, PrintEval) { |
| 72 | int y = 0; |
| 73 | |
| 74 | // This DBUG_PRINT args should never be evaluated. |
| 75 | DBUG_PRINT("never", ("%d", y += 1)); |
| 76 | EXPECT_EQ(y, 0) << "DBUG_PRINT arg is evaluated."; |
| 77 | } |
| 78 | |
| 79 | TEST(DebugPrintEvalTest, PrintEval) { |
| 80 | int y = 0; |
| 81 | |
| 82 | DBUG_SET("+d,never"); |
| 83 | DBUG_PRINT("never", ("%d", y += 1)); |
| 84 | DBUG_SET(""); |
| 85 | EXPECT_GE(y, 1) << "DBUG_PRINT arg is not evaluated."; |
| 86 | } |
| 87 | |
| 88 | TEST(DebugSetTest, DebugKeywordsTest) { |
| 89 | char buf[1024]; |
| 90 | |
| 91 | /* |
| 92 | Enable d flag, then enable debug on a keyword. The debug should |
| 93 | remain set to d and empty list of keywords indicating debug is |
| 94 | enabled for all. |
| 95 | */ |
| 96 | DBUG_SET("d"); |
| 97 | DBUG_SET("+d,keyword"); |
| 98 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 99 | EXPECT_STREQ("d", buf); |
| 100 | DBUG_SET(""); |
| 101 | |
| 102 | /* |
| 103 | Set debug on a specific keyword. Debug should be enabled |
| 104 | for the keyword. |
| 105 | */ |
| 106 | DBUG_SET("+d,keyword"); |
| 107 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 108 | EXPECT_STREQ("d,keyword", buf); |
| 109 | |
| 110 | /* |
| 111 | Remove the keyword from debug list. Debug should be |
| 112 | disabled. |
| 113 | */ |
| 114 | DBUG_SET("-d,keyword"); |
| 115 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 116 | EXPECT_STREQ("", buf); |
| 117 | DBUG_SET(""); |
| 118 | |
| 119 | /* |
| 120 | Enable debug for a keyword. Then enable debug for all |
| 121 | keywords. Debug should now be enabled for all keywords. |
| 122 | */ |
| 123 | DBUG_SET("+d,keyword"); |
| 124 | DBUG_SET("+d"); |
| 125 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 126 | EXPECT_STREQ("d", buf); |
| 127 | DBUG_SET(""); |
| 128 | |
| 129 | // Add multiple debug keywords. |
| 130 | DBUG_SET("+d,keyword1"); |
| 131 | DBUG_SET("+d,keyword2"); |
| 132 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 133 | EXPECT_STREQ("d,keyword1,keyword2", buf); |
| 134 | DBUG_SET("-d,keyword1"); |
| 135 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 136 | EXPECT_STREQ("d,keyword2", buf); |
| 137 | DBUG_SET("-d,keyword2"); |
| 138 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 139 | EXPECT_STREQ("", buf); |
| 140 | DBUG_SET(""); |
| 141 | |
| 142 | // Add two keywords, the second keyword being a prefix of the first keyword. |
| 143 | DBUG_SET("+d,simulate_file_error_once,simulate_file_error"); |
| 144 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 145 | EXPECT_STREQ("d,simulate_file_error_once,simulate_file_error", buf); |
| 146 | DBUG_SET(""); |
| 147 | |
| 148 | // Add same keyword thrice, keyword should show up once in debug list. |
| 149 | DBUG_SET("+d,keyword,keyword,keyword"); |
| 150 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 151 | EXPECT_STREQ("d,keyword", buf); |
| 152 | DBUG_SET(""); |
| 153 | |
| 154 | // Add some combination of keywords with whitespace and duplicates. |
| 155 | DBUG_SET("+d, keyword1, keyword2, keyword1,keyword3 "); |
| 156 | DBUG_EXPLAIN(buf, sizeof(buf)); |
| 157 | EXPECT_STREQ("d,keyword1,keyword2,keyword3", buf); |
| 158 | DBUG_SET(""); |
| 159 | } |
| 160 | #endif /* NDEBUG */ |
| 161 | } // namespace dbug_unittest |