1 1.1 jmmv // Copyright 2012 Google Inc. 2 1.1 jmmv // All rights reserved. 3 1.1 jmmv // 4 1.1 jmmv // Redistribution and use in source and binary forms, with or without 5 1.1 jmmv // modification, are permitted provided that the following conditions are 6 1.1 jmmv // met: 7 1.1 jmmv // 8 1.1 jmmv // * Redistributions of source code must retain the above copyright 9 1.1 jmmv // notice, this list of conditions and the following disclaimer. 10 1.1 jmmv // * Redistributions in binary form must reproduce the above copyright 11 1.1 jmmv // notice, this list of conditions and the following disclaimer in the 12 1.1 jmmv // documentation and/or other materials provided with the distribution. 13 1.1 jmmv // * Neither the name of Google Inc. nor the names of its contributors 14 1.1 jmmv // may be used to endorse or promote products derived from this software 15 1.1 jmmv // without specific prior written permission. 16 1.1 jmmv // 17 1.1 jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 1.1 jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 1.1 jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 1.1 jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 1.1 jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 1.1 jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 1.1 jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 1.1 jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 jmmv 29 1.1 jmmv #include "stacktrace.h" 30 1.1 jmmv 31 1.1 jmmv #include <sys/resource.h> 32 1.1 jmmv #include <sys/stat.h> 33 1.1 jmmv #include <sys/wait.h> 34 1.1 jmmv 35 1.1 jmmv #include <stdlib.h> 36 1.1 jmmv #include <string.h> 37 1.1 jmmv #include <unistd.h> 38 1.1 jmmv 39 1.1 jmmv #include <atf-c.h> 40 1.1 jmmv 41 1.1 jmmv #include "env.h" 42 1.1 jmmv #include "error.h" 43 1.1 jmmv #include "fs.h" 44 1.1 jmmv #include "run.h" 45 1.1 jmmv #include "text.h" 46 1.1 jmmv 47 1.1 jmmv 48 1.1 jmmv /// Ensures that the given expression does not return a kyua_error_t. 49 1.1 jmmv /// 50 1.1 jmmv /// \param expr Expression to evaluate. 51 1.1 jmmv #define RE(expr) ATF_REQUIRE(!kyua_error_is_set(expr)) 52 1.1 jmmv 53 1.1 jmmv 54 1.1 jmmv /// Ensures that the given expression does not return a kyua_error_t. 55 1.1 jmmv /// 56 1.1 jmmv /// \param expr Expression to evaluate. 57 1.1 jmmv /// \param msg Failure message. 58 1.1 jmmv #define RE_MSG(expr, msg) ATF_REQUIRE_MSG(!kyua_error_is_set(expr), msg) 59 1.1 jmmv 60 1.1 jmmv 61 1.1 jmmv /// Generates a core dump. 62 1.1 jmmv /// 63 1.1 jmmv /// Due to the complexity of this interface, you should probably use 64 1.1 jmmv /// generate_core() instead. 65 1.1 jmmv /// 66 1.1 jmmv /// \post If this fails to generate a core file, the test case is marked as 67 1.1 jmmv /// skipped. The caller therefore can rely that a core dump has been created on 68 1.1 jmmv /// return. 69 1.1 jmmv /// 70 1.1 jmmv /// \param tc Pointer to the caller test case. 71 1.1 jmmv /// \param run_params Parameters for the execution of the helper. 72 1.1 jmmv /// \param helper_path Path to the created helper. 73 1.1 jmmv /// \param exec_path Name of the helper, prefixed with ./ so that it can be 74 1.1 jmmv /// executed from within the work directory. 75 1.1 jmmv /// \param helper_name Basename of the helper. 76 1.1 jmmv /// 77 1.1 jmmv /// \return The PID of the crashed binary. 78 1.1 jmmv static pid_t 79 1.1 jmmv generate_core_aux(const atf_tc_t* tc, const kyua_run_params_t* run_params, 80 1.1 jmmv const char* helper_path, const char* exec_path, 81 1.1 jmmv const char* helper_name) 82 1.1 jmmv { 83 1.1 jmmv const char* srcdir = atf_tc_get_config_var(tc, "srcdir"); 84 1.1 jmmv 85 1.1 jmmv char* src_helper; 86 1.1 jmmv RE(kyua_fs_concat(&src_helper, srcdir, "stacktrace_helper", NULL)); 87 1.1 jmmv atf_utils_copy_file(src_helper, helper_path); 88 1.1 jmmv free(src_helper); 89 1.1 jmmv 90 1.1 jmmv // We use kyua_run_fork for this to better simulate the final use case of 91 1.1 jmmv // the stacktrace gathering, as test programs are run through these 92 1.1 jmmv // functions. Also, kyua_run_fork provides us with automatic unlimiting of 93 1.1 jmmv // resources so that core files can be generated. 94 1.1 jmmv 95 1.1 jmmv pid_t pid; 96 1.1 jmmv const kyua_error_t error = kyua_run_fork(run_params, &pid); 97 1.1 jmmv if (!kyua_error_is_set(error) && pid == 0) { 98 1.1 jmmv const char* const args[] = { helper_name, NULL }; 99 1.1 jmmv kyua_run_exec(exec_path, args); 100 1.1 jmmv } 101 1.1 jmmv RE(error); 102 1.1 jmmv 103 1.1 jmmv int status; bool timed_out; 104 1.1 jmmv RE_MSG(kyua_run_wait(pid, &status, &timed_out), 105 1.1 jmmv "wait failed; unexpected problem during exec?"); 106 1.1 jmmv 107 1.1 jmmv ATF_REQUIRE(WIFSIGNALED(status)); 108 1.1 jmmv if (!WCOREDUMP(status)) 109 1.1 jmmv atf_tc_skip("Test failed to generate core dump"); 110 1.1 jmmv return pid; 111 1.1 jmmv } 112 1.1 jmmv 113 1.1 jmmv 114 1.1 jmmv /// Creates a script. 115 1.1 jmmv /// 116 1.1 jmmv /// \param script Path to the script to create. 117 1.1 jmmv /// \param contents Contents of the script. 118 1.1 jmmv static void 119 1.1 jmmv create_script(const char* script, const char* contents) 120 1.1 jmmv { 121 1.1 jmmv atf_utils_create_file(script, "#! /bin/sh\n\n%s\n", contents); 122 1.1 jmmv ATF_REQUIRE(chmod(script, 0755) != -1); 123 1.1 jmmv } 124 1.1 jmmv 125 1.1 jmmv 126 1.1 jmmv /// Generates a core file. 127 1.1 jmmv /// 128 1.1 jmmv /// \param tc Pointer to the calling test case. 129 1.1 jmmv /// \param work_directory Name of the directory in which to place the binary 130 1.1 jmmv /// that will generate the stacktrace. 131 1.1 jmmv /// \param program_name Basename of the binary that will crash. 132 1.1 jmmv /// 133 1.1 jmmv /// \return PID of the process that generated the core file. 134 1.1 jmmv static pid_t 135 1.1 jmmv generate_core(const atf_tc_t* tc, const char* work_directory, 136 1.1 jmmv const char* program_name) 137 1.1 jmmv { 138 1.1 jmmv kyua_run_params_t run_params; 139 1.1 jmmv kyua_run_params_init(&run_params); 140 1.1 jmmv if (strcmp(work_directory, ".") != 0) { 141 1.1 jmmv ATF_REQUIRE(mkdir(work_directory, 0755) != -1); 142 1.1 jmmv run_params.work_directory = work_directory; 143 1.1 jmmv } 144 1.1 jmmv 145 1.1 jmmv char* copy_to; char* exec_path; 146 1.1 jmmv RE(kyua_text_printf(©_to, "%s/%s", work_directory, program_name)); 147 1.1 jmmv RE(kyua_text_printf(&exec_path, "./%s", program_name)); 148 1.1 jmmv const pid_t pid = generate_core_aux(tc, &run_params, copy_to, exec_path, 149 1.1 jmmv program_name); 150 1.1 jmmv free(exec_path); 151 1.1 jmmv free(copy_to); 152 1.1 jmmv return pid; 153 1.1 jmmv } 154 1.1 jmmv 155 1.1 jmmv 156 1.1 jmmv /// Prepares and runs kyua_stacktrace_dump(). 157 1.1 jmmv /// 158 1.1 jmmv /// \param tc Pointer to the calling test case. 159 1.1 jmmv /// \param work_directory Name of the directory in which to place the binary 160 1.1 jmmv /// that will generate the stacktrace. 161 1.1 jmmv /// \param program_name Basename of the binary that will crash. 162 1.1 jmmv /// \param output_name Name of the file to which to write the stacktrace. 163 1.1 jmmv /// \param timeout_seconds Time to give GDB to complete. 164 1.1 jmmv static void 165 1.1 jmmv do_dump(const atf_tc_t* tc, const char* work_directory, 166 1.1 jmmv const char* program_name, const char* output_name, 167 1.1 jmmv const int timeout_seconds) 168 1.1 jmmv { 169 1.1 jmmv const pid_t pid = generate_core(tc, work_directory, program_name); 170 1.1 jmmv 171 1.1 jmmv kyua_run_params_t run_params; 172 1.1 jmmv kyua_run_params_init(&run_params); 173 1.1 jmmv run_params.timeout_seconds = timeout_seconds + 100; // Some large value. 174 1.1 jmmv run_params.work_directory = work_directory; // Created by generate_core. 175 1.1 jmmv 176 1.1 jmmv kyua_stacktrace_gdb_timeout = timeout_seconds; 177 1.1 jmmv 178 1.1 jmmv FILE* output = fopen(output_name, "w"); 179 1.1 jmmv ATF_REQUIRE(output != NULL); 180 1.1 jmmv kyua_stacktrace_dump(program_name, pid, &run_params, output); 181 1.1 jmmv fclose(output); 182 1.1 jmmv atf_utils_cat_file(output_name, "dump output: "); 183 1.1 jmmv } 184 1.1 jmmv 185 1.1 jmmv 186 1.1 jmmv ATF_TC_WITHOUT_HEAD(find_core__found__short); 187 1.1 jmmv ATF_TC_BODY(find_core__found__short, tc) 188 1.1 jmmv { 189 1.1 jmmv const pid_t pid = generate_core(tc, "dir", "short"); 190 1.1 jmmv const char* core_name = kyua_stacktrace_find_core("short", "dir", pid); 191 1.1 jmmv if (core_name == NULL) 192 1.1 jmmv atf_tc_fail("Core dumped, but no candidates found"); 193 1.1 jmmv ATF_REQUIRE(strstr(core_name, "core") != NULL); 194 1.1 jmmv ATF_REQUIRE(access(core_name, F_OK) != -1); 195 1.1 jmmv } 196 1.1 jmmv 197 1.1 jmmv 198 1.1 jmmv ATF_TC_WITHOUT_HEAD(find_core__found__long); 199 1.1 jmmv ATF_TC_BODY(find_core__found__long, tc) 200 1.1 jmmv { 201 1.1 jmmv const pid_t pid = generate_core( 202 1.1 jmmv tc, "dir", "long-name-that-may-be-truncated-in-some-systems"); 203 1.1 jmmv const char* core_name = kyua_stacktrace_find_core( 204 1.1 jmmv "long-name-that-may-be-truncated-in-some-systems", "dir", pid); 205 1.1 jmmv if (core_name == NULL) 206 1.1 jmmv atf_tc_fail("Core dumped, but no candidates found"); 207 1.1 jmmv ATF_REQUIRE(strstr(core_name, "core") != NULL); 208 1.1 jmmv ATF_REQUIRE(access(core_name, F_OK) != -1); 209 1.1 jmmv } 210 1.1 jmmv 211 1.1 jmmv 212 1.1 jmmv ATF_TC_WITHOUT_HEAD(find_core__not_found); 213 1.1 jmmv ATF_TC_BODY(find_core__not_found, tc) 214 1.1 jmmv { 215 1.1 jmmv const char* core_name = kyua_stacktrace_find_core("missing", ".", 1); 216 1.1 jmmv if (core_name != NULL) 217 1.1 jmmv atf_tc_fail("Core not dumped, but candidate found: %s", core_name); 218 1.1 jmmv } 219 1.1 jmmv 220 1.1 jmmv 221 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__integration); 222 1.1 jmmv ATF_TC_BODY(dump__integration, tc) 223 1.1 jmmv { 224 1.1 jmmv do_dump(tc, "dir", "short", "stacktrace", 10); 225 1.1 jmmv 226 1.1 jmmv // It is hard to validate the execution of an arbitrary GDB of which we know 227 1.1 jmmv // nothing anything. Just assume that the backtrace, at the very least, 228 1.1 jmmv // prints a frame identifier. 229 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("#0", "stacktrace")); 230 1.1 jmmv } 231 1.1 jmmv 232 1.1 jmmv 233 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__ok); 234 1.1 jmmv ATF_TC_BODY(dump__ok, tc) 235 1.1 jmmv { 236 1.1 jmmv RE(kyua_env_set("PATH", ".")); 237 1.1 jmmv create_script("fake-gdb", "echo 'frame 1'; echo 'frame 2'; " 238 1.1 jmmv "echo 'some warning' 1>&2; exit 0"); 239 1.1 jmmv kyua_stacktrace_gdb = "fake-gdb"; 240 1.1 jmmv 241 1.1 jmmv do_dump(tc, ".", "short", "stacktrace", 10); 242 1.1 jmmv 243 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("dumped core; attempting to gather", 244 1.1 jmmv "stacktrace")); 245 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("frame 1", "stacktrace")); 246 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("frame 2", "stacktrace")); 247 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("some warning", "stacktrace")); 248 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("GDB exited successfully", "stacktrace")); 249 1.1 jmmv } 250 1.1 jmmv 251 1.1 jmmv 252 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__cannot_find_core); 253 1.1 jmmv ATF_TC_BODY(dump__cannot_find_core, tc) 254 1.1 jmmv { 255 1.1 jmmv kyua_run_params_t run_params; 256 1.1 jmmv kyua_run_params_init(&run_params); 257 1.1 jmmv 258 1.1 jmmv FILE* output = fopen("stacktrace", "w"); 259 1.1 jmmv ATF_REQUIRE(output != NULL); 260 1.1 jmmv // This assumes that init(8) has never core dumped. 261 1.1 jmmv kyua_stacktrace_dump("missing", 1, &run_params, output); 262 1.1 jmmv fclose(output); 263 1.1 jmmv atf_utils_cat_file("stacktrace", "dump output: "); 264 1.1 jmmv 265 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("Cannot find any core file", "stacktrace")); 266 1.1 jmmv } 267 1.1 jmmv 268 1.1 jmmv 269 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__cannot_find_gdb); 270 1.1 jmmv ATF_TC_BODY(dump__cannot_find_gdb, tc) 271 1.1 jmmv { 272 1.1 jmmv RE(kyua_env_set("PATH", ".")); 273 1.1 jmmv kyua_stacktrace_gdb = "missing-gdb"; 274 1.1 jmmv 275 1.1 jmmv do_dump(tc, ".", "dont-care", "stacktrace", 10); 276 1.1 jmmv 277 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("execvp failed", "stacktrace")); 278 1.1 jmmv } 279 1.1 jmmv 280 1.1 jmmv 281 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__gdb_fail); 282 1.1 jmmv ATF_TC_BODY(dump__gdb_fail, tc) 283 1.1 jmmv { 284 1.1 jmmv RE(kyua_env_set("PATH", ".")); 285 1.1 jmmv create_script("fake-gdb", "echo 'foo'; echo 'bar' 1>&2; exit 56"); 286 1.1 jmmv kyua_stacktrace_gdb = "fake-gdb"; 287 1.1 jmmv 288 1.1 jmmv do_dump(tc, ".", "short", "stacktrace", 10); 289 1.1 jmmv 290 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("foo", "stacktrace")); 291 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("bar", "stacktrace")); 292 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("GDB failed with code 56; see output above " 293 1.1 jmmv "for details", "stacktrace")); 294 1.1 jmmv } 295 1.1 jmmv 296 1.1 jmmv 297 1.1 jmmv ATF_TC_WITHOUT_HEAD(dump__gdb_times_out); 298 1.1 jmmv ATF_TC_BODY(dump__gdb_times_out, tc) 299 1.1 jmmv { 300 1.1 jmmv RE(kyua_env_set("PATH", ".")); 301 1.1 jmmv create_script("fake-gdb", "echo 'foo'; echo 'bar' 1>&2; " 302 1.1 jmmv "/bin/sleep 10; /usr/bin/sleep 10; exit 0"); 303 1.1 jmmv kyua_stacktrace_gdb = "fake-gdb"; 304 1.1 jmmv 305 1.1 jmmv do_dump(tc, ".", "short", "stacktrace", 1); 306 1.1 jmmv 307 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("foo", "stacktrace")); 308 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("bar", "stacktrace")); 309 1.1 jmmv ATF_REQUIRE(atf_utils_grep_file("GDB failed; timed out", "stacktrace")); 310 1.1 jmmv } 311 1.1 jmmv 312 1.1 jmmv 313 1.1 jmmv ATF_TP_ADD_TCS(tp) 314 1.1 jmmv { 315 1.1 jmmv ATF_TP_ADD_TC(tp, find_core__found__short); 316 1.1 jmmv ATF_TP_ADD_TC(tp, find_core__found__long); 317 1.1 jmmv ATF_TP_ADD_TC(tp, find_core__not_found); 318 1.1 jmmv 319 1.1 jmmv ATF_TP_ADD_TC(tp, dump__integration); 320 1.1 jmmv ATF_TP_ADD_TC(tp, dump__ok); 321 1.1 jmmv ATF_TP_ADD_TC(tp, dump__cannot_find_core); 322 1.1 jmmv ATF_TP_ADD_TC(tp, dump__cannot_find_gdb); 323 1.1 jmmv ATF_TP_ADD_TC(tp, dump__gdb_fail); 324 1.1 jmmv ATF_TP_ADD_TC(tp, dump__gdb_times_out); 325 1.1 jmmv 326 1.1 jmmv return atf_no_error(); 327 1.1 jmmv } 328