Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2007, 2011, 2013, 2014 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * Daniel Vetter <daniel.vetter@ffwll.ch> |
| 26 | * |
| 27 | */ |
| 28 | |
Daniel Vetter | 00215dd | 2017-09-05 14:36:05 +0200 | [diff] [blame] | 29 | #ifdef HAVE_LIBGEN_H |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 30 | #include <libgen.h> |
| 31 | #endif |
| 32 | #include <stdio.h> |
Daniel Vetter | 0e22f14 | 2014-03-22 19:27:04 +0100 | [diff] [blame] | 33 | #include <assert.h> |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 34 | #include <fcntl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <signal.h> |
| 40 | #include <pciaccess.h> |
| 41 | #include <getopt.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <unistd.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <sys/types.h> |
Alan Coopersmith | 99e6def | 2014-12-23 19:07:10 -0800 | [diff] [blame] | 46 | #ifdef __linux__ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 47 | #include <sys/syscall.h> |
Alan Coopersmith | 99e6def | 2014-12-23 19:07:10 -0800 | [diff] [blame] | 48 | #endif |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 49 | #include <pthread.h> |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 50 | #include <sys/utsname.h> |
| 51 | #include <termios.h> |
Daniel Vetter | 254f19b | 2014-03-22 21:29:01 +0100 | [diff] [blame] | 52 | #include <errno.h> |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 53 | #include <time.h> |
Thomas Wood | 2b05ecf | 2015-01-26 16:59:53 +0000 | [diff] [blame] | 54 | #include <ctype.h> |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 55 | #include <limits.h> |
Chris Wilson | 453792c | 2015-06-24 11:26:33 +0100 | [diff] [blame] | 56 | #include <locale.h> |
Derek Morton | ef724ab | 2016-02-04 12:06:57 +0000 | [diff] [blame] | 57 | #include <uwildmat/uwildmat.h> |
Petri Latvala | 08a2f88 | 2017-09-29 13:51:59 +0300 | [diff] [blame] | 58 | #ifdef HAVE_GLIB |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 59 | #include <glib.h> |
Petri Latvala | 08a2f88 | 2017-09-29 13:51:59 +0300 | [diff] [blame] | 60 | #endif |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 61 | |
| 62 | #include "drmtest.h" |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 63 | #include "intel_chipset.h" |
Daniel Vetter | c03c6ce | 2014-03-22 21:34:29 +0100 | [diff] [blame] | 64 | #include "intel_io.h" |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 65 | #include "igt_debugfs.h" |
Rodrigo Vivi | 82ad15f | 2014-06-09 11:15:47 -0700 | [diff] [blame] | 66 | #include "version.h" |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 67 | #include "config.h" |
| 68 | |
| 69 | #include "igt_core.h" |
Daniel Vetter | f5daeec | 2014-03-23 13:35:09 +0100 | [diff] [blame] | 70 | #include "igt_aux.h" |
Chris Wilson | 9c4114e | 2016-12-21 16:37:46 +0000 | [diff] [blame] | 71 | #include "igt_sysfs.h" |
Petri Latvala | 08a2f88 | 2017-09-29 13:51:59 +0300 | [diff] [blame] | 72 | #include "igt_rc.h" |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 73 | |
Alan Coopersmith | f04b9df | 2014-12-23 19:07:11 -0800 | [diff] [blame] | 74 | #ifdef HAVE_LIBGEN_H |
| 75 | #include <libgen.h> /* for basename() on Solaris */ |
| 76 | #endif |
| 77 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 78 | /** |
| 79 | * SECTION:igt_core |
| 80 | * @short_description: Core i-g-t testing support |
Damien Lespiau | 6ebd8c2 | 2015-06-26 14:28:41 +0100 | [diff] [blame] | 81 | * @title: Core |
Thomas Wood | f0381d1 | 2015-09-07 09:26:01 +0100 | [diff] [blame] | 82 | * @include: igt.h |
Daniel Vetter | cd6d5a6 | 2014-03-22 19:35:40 +0100 | [diff] [blame] | 83 | * |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 84 | * This library implements the core of the i-g-t test support infrastructure. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 85 | * Main features are the subtest enumeration, cmdline option parsing helpers for |
| 86 | * subtest handling and various helpers to structure testcases with subtests and |
| 87 | * handle subtest test results. |
| 88 | * |
| 89 | * Auxiliary code provides exit handlers, support for forked processes with test |
| 90 | * result propagation. Other generally useful functionality includes optional |
| 91 | * structure logging infrastructure and some support code for running reduced |
| 92 | * test set on in simulated hardware environments. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 93 | * |
| 94 | * When writing tests with subtests it is extremely important that nothing |
| 95 | * interferes with the subtest enumeration. In i-g-t subtests are enumerated at |
| 96 | * runtime, which allows powerful testcase enumeration. But it makes subtest |
| 97 | * enumeration a bit more tricky since the test code needs to be careful to |
| 98 | * never run any code which might fail (like trying to do privileged operations |
| 99 | * or opening device driver nodes). |
| 100 | * |
| 101 | * To allow this i-g-t provides #igt_fixture code blocks for setup code outside |
| 102 | * of subtests and automatically skips the subtest code blocks themselves. For |
Daniel Vetter | 72d04b8 | 2016-03-18 21:46:54 +0100 | [diff] [blame] | 103 | * special cases igt_only_list_subtests() is also provided. For setup code only |
| 104 | * shared by a group of subtest encapsulate the #igt_fixture block and all the |
| 105 | * subtestest in a #igt_subtest_group block. |
Daniel Vetter | 5cf8d8d | 2014-03-13 21:25:01 +0100 | [diff] [blame] | 106 | * |
| 107 | * # Magic Control Blocks |
| 108 | * |
| 109 | * i-g-t makes heavy use of C macros which serve as magic control blocks. They |
| 110 | * work fairly well and transparently but since C doesn't have full-blown |
| 111 | * closures there are caveats: |
| 112 | * |
| 113 | * - Asynchronous blocks which are used to spawn children internally use fork(). |
| 114 | * Which means that nonsensical control flow like jumping out of the control |
| 115 | * block is possible, but it will badly confuse the i-g-t library code. And of |
| 116 | * course all caveats of a real fork() call apply, namely that file |
| 117 | * descriptors are copied, but still point at the original file. This will |
| 118 | * terminally upset the libdrm buffer manager if both parent and child keep on |
| 119 | * using the same open instance of the drm device. Usually everything related |
| 120 | * to interacting with the kernel driver must be reinitialized to avoid such |
| 121 | * issues. |
| 122 | * |
| 123 | * - Code blocks with magic control flow are implemented with setjmp() and |
| 124 | * longjmp(). This applies to #igt_fixture and #igt_subtest blocks and all the |
| 125 | * three variants to finish test: igt_success(), igt_skip() and igt_fail(). |
| 126 | * Mostly this is of no concern, except when such a control block changes |
| 127 | * stack variables defined in the same function as the control block resides. |
| 128 | * Any store/load behaviour after a longjmp() is ill-defined for these |
| 129 | * variables. Avoid such code. |
| 130 | * |
| 131 | * Quoting the man page for longjmp(): |
| 132 | * |
| 133 | * "The values of automatic variables are unspecified after a call to |
| 134 | * longjmp() if they meet all the following criteria:" |
| 135 | * - "they are local to the function that made the corresponding setjmp() call; |
| 136 | * - "their values are changed between the calls to setjmp() and longjmp(); and |
| 137 | * - "they are not declared as volatile." |
Daniel Vetter | 10571b8 | 2014-03-16 19:34:37 +0100 | [diff] [blame] | 138 | * |
| 139 | * # Best Practices for Test Helper Libraries Design |
| 140 | * |
| 141 | * Kernel tests itself tend to have fairly complex logic already. It is |
| 142 | * therefore paramount that helper code, both in libraries and test-private |
| 143 | * functions, add as little boilerplate code to the main test logic as possible. |
| 144 | * But then dense code is hard to understand without constantly consulting |
| 145 | * the documentation and implementation of all the helper functions if it |
| 146 | * doesn't follow some clear patterns. Hence follow these established best |
| 147 | * practices: |
| 148 | * |
| 149 | * - Make extensive use of the implicit control flow afforded by igt_skip(), |
| 150 | * igt_fail and igt_success(). When dealing with optional kernel features |
| 151 | * combine igt_skip() with igt_fail() to skip when the kernel support isn't |
| 152 | * available but fail when anything else goes awry. void should be the most |
| 153 | * common return type in all your functions, except object constructors of |
| 154 | * course. |
| 155 | * |
| 156 | * - The main test logic should have no explicit control flow for failure |
| 157 | * conditions, but instead such assumptions should be written in a declarative |
| 158 | * style. Use one of the many macros which encapsulate i-g-t's implicit |
| 159 | * control flow. Pick the most suitable one to have as much debug output as |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 160 | * possible without polluting the code unnecessarily. For example |
Daniel Vetter | 10571b8 | 2014-03-16 19:34:37 +0100 | [diff] [blame] | 161 | * igt_assert_cmpint() for comparing integers or do_ioctl() for running ioctls |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 162 | * and checking their results. Feel free to add new ones to the library or |
Daniel Vetter | 10571b8 | 2014-03-16 19:34:37 +0100 | [diff] [blame] | 163 | * wrap up a set of checks into a private function to further condense your |
| 164 | * test logic. |
| 165 | * |
| 166 | * - When adding a new feature test function which uses igt_skip() internally, |
Thomas Wood | 5b7edb9 | 2015-04-20 12:35:01 +0100 | [diff] [blame] | 167 | * use the <prefix>_require_<feature_name> naming scheme. When you |
Daniel Vetter | 10571b8 | 2014-03-16 19:34:37 +0100 | [diff] [blame] | 168 | * instead add a feature test function which returns a boolean, because your |
| 169 | * main test logic must take different actions depending upon the feature's |
Thomas Wood | 5b7edb9 | 2015-04-20 12:35:01 +0100 | [diff] [blame] | 170 | * availability, then instead use the <prefix>_has_<feature_name>. |
Daniel Vetter | 10571b8 | 2014-03-16 19:34:37 +0100 | [diff] [blame] | 171 | * |
| 172 | * - As already mentioned eschew explicit error handling logic as much as |
| 173 | * possible. If your test absolutely has to handle the error of some function |
| 174 | * the customary naming pattern is to prefix those variants with __. Try to |
| 175 | * restrict explicit error handling to leaf functions. For the main test flow |
| 176 | * simply pass the expected error condition down into your helper code, which |
| 177 | * results in tidy and declarative test logic. |
| 178 | * |
| 179 | * - Make your library functions as simple to use as possible. Automatically |
| 180 | * register cleanup handlers through igt_install_exit_handler(). Reduce the |
| 181 | * amount of setup boilerplate needed by using implicit singletons and lazy |
| 182 | * structure initialization and similar design patterns. |
| 183 | * |
| 184 | * - Don't shy away from refactoring common code, even when there are just 2-3 |
| 185 | * users and even if it's not a net reduction in code. As long as it helps to |
| 186 | * remove boilerplate and makes the code more declarative the resulting |
| 187 | * clearer test flow is worth it. All i-g-t library code has been organically |
| 188 | * extracted from testcases in this fashion. |
| 189 | * |
| 190 | * - For general coding style issues please follow the kernel's rules laid out |
| 191 | * in |
| 192 | * [CodingStyle](https://www.kernel.org/doc/Documentation/CodingStyle). |
Daniel Vetter | 1503d17 | 2014-05-13 11:29:34 +0200 | [diff] [blame] | 193 | * |
| 194 | * # Interface with Testrunners |
| 195 | * |
| 196 | * i-g-t testcase are all executables which should be run as root on an |
| 197 | * otherwise completely idle system. The test status is reflected in the |
Daniel Vetter | 831eb21 | 2014-05-14 15:33:27 +0200 | [diff] [blame] | 198 | * exitcode. #IGT_EXIT_SUCCESS means "success", #IGT_EXIT_SKIP "skip", |
| 199 | * #IGT_EXIT_TIMEOUT that some operation "timed out". All other exit codes |
| 200 | * encode a failed test result, including any abnormal termination of the test |
| 201 | * (e.g. by SIGKILL). |
Daniel Vetter | 1503d17 | 2014-05-13 11:29:34 +0200 | [diff] [blame] | 202 | * |
| 203 | * On top of that tests may report unexpected results and minor issues to |
| 204 | * stderr. If stderr is non-empty the test result should be treated as "warn". |
| 205 | * |
| 206 | * The test lists are generated at build time. Simple testcases are listed in |
| 207 | * tests/single-tests.txt and tests with subtests are listed in |
| 208 | * tests/multi-tests.txt. When running tests with subtest from a test runner it |
| 209 | * is recommend to run each subtest individually, since otherwise the return |
| 210 | * code will only reflect the overall result. |
| 211 | * |
| 212 | * To do that obtain the lists of subtests with "--list-subtests", which can be |
| 213 | * run as non-root and doesn't require the i915 driver to be loaded (or any |
| 214 | * intel gpu to be present). Then individual subtests can be run with |
| 215 | * "--run-subtest". Usage help for tests with subtests can be obtained with the |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 216 | * "--help" command line option. |
Derek Morton | ef724ab | 2016-02-04 12:06:57 +0000 | [diff] [blame] | 217 | * |
| 218 | * A wildcard expression can be given to --run-subtest to specify a subset of |
| 219 | * subtests to run. See https://tools.ietf.org/html/rfc3977#section-4 for a |
| 220 | * description of allowed wildcard expressions. |
| 221 | * Some examples of allowed wildcard expressions are: |
| 222 | * |
| 223 | * - '*basic*' match any subtest containing basic |
| 224 | * - 'basic-???' match any subtest named basic- with 3 characters after - |
| 225 | * - 'basic-[0-9]' match any subtest named basic- with a single number after - |
| 226 | * - 'basic-[^0-9]' match any subtest named basic- with a single non numerical character after - |
| 227 | * - 'basic*,advanced*' match any subtest starting basic or advanced |
| 228 | * - '*,!basic*' match any subtest not starting basic |
| 229 | * - 'basic*,!basic-render*' match any subtest starting basic but not starting basic-render |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 230 | * |
| 231 | * # Configuration |
| 232 | * |
| 233 | * Some of IGT's behavior can be configured through a configuration file. |
| 234 | * By default, this file is expected to exist in ~/.igtrc . The directory for |
| 235 | * this can be overriden by setting the environment variable %IGT_CONFIG_PATH. |
| 236 | * An example configuration follows: |
| 237 | * |
| 238 | * |[<!-- language="plain" --> |
Paul Kocialkowski | 73a3b0b | 2017-08-28 11:12:03 +0300 | [diff] [blame] | 239 | * # The common configuration section follows. |
Paul Kocialkowski | ee31e0b | 2017-07-19 16:46:07 +0300 | [diff] [blame] | 240 | * [Common] |
| 241 | * FrameDumpPath=/tmp # The path to dump frames that fail comparison checks |
| 242 | * |
Paul Kocialkowski | 73a3b0b | 2017-08-28 11:12:03 +0300 | [diff] [blame] | 243 | * # The following section is used for configuring the Device Under Test. |
| 244 | * # It is not mandatory and allows overriding default values. |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 245 | * [DUT] |
| 246 | * SuspendResumeDelay=10 |
| 247 | * ]| |
| 248 | * |
| 249 | * Some specific configuration options may be used by specific parts of IGT, |
| 250 | * such as those related to Chamelium support. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 251 | */ |
| 252 | |
| 253 | static unsigned int exit_handler_count; |
Thomas Wood | a6c40c7 | 2015-03-12 14:59:20 +0000 | [diff] [blame] | 254 | const char *igt_interactive_debug; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 255 | |
| 256 | /* subtests helpers */ |
| 257 | static bool list_subtests = false; |
| 258 | static char *run_single_subtest = NULL; |
Thomas Wood | e057b86 | 2014-07-18 14:10:27 +0100 | [diff] [blame] | 259 | static bool run_single_subtest_found = false; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 260 | static const char *in_subtest = NULL; |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 261 | static struct timespec subtest_time; |
Joonas Lahtinen | d84e624 | 2015-11-20 13:57:11 +0200 | [diff] [blame] | 262 | static clockid_t igt_clock = (clockid_t)-1; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 263 | static bool in_fixture = false; |
| 264 | static bool test_with_subtests = false; |
Thomas Wood | e65c69b | 2015-07-16 17:39:59 +0100 | [diff] [blame] | 265 | static bool in_atexit_handler = false; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 266 | static enum { |
| 267 | CONT = 0, SKIP, FAIL |
| 268 | } skip_subtests_henceforth = CONT; |
| 269 | |
Thomas Wood | 790f1f8 | 2015-11-09 17:17:24 +0000 | [diff] [blame] | 270 | bool __igt_plain_output = false; |
| 271 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 272 | /* fork support state */ |
| 273 | pid_t *test_children; |
| 274 | int num_test_children; |
| 275 | int test_children_sz; |
| 276 | bool test_child; |
| 277 | |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 278 | enum { |
| 279 | OPT_LIST_SUBTESTS, |
| 280 | OPT_RUN_SUBTEST, |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 281 | OPT_DESCRIPTION, |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 282 | OPT_DEBUG, |
Rodrigo Vivi | 3d65ff7 | 2015-01-12 10:21:58 -0800 | [diff] [blame] | 283 | OPT_INTERACTIVE_DEBUG, |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 284 | OPT_HELP = 'h' |
| 285 | }; |
| 286 | |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 287 | static int igt_exitcode = IGT_EXIT_SUCCESS; |
| 288 | static const char *command_str; |
| 289 | |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 290 | static char* igt_log_domain_filter; |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 291 | static struct { |
| 292 | char *entries[256]; |
| 293 | uint8_t start, end; |
| 294 | } log_buffer; |
| 295 | static pthread_mutex_t log_buffer_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 296 | |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 297 | #ifdef HAVE_GLIB |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 298 | GKeyFile *igt_key_file; |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 299 | #endif |
| 300 | |
Daniel Vetter | a3a3e0f | 2017-09-05 14:36:08 +0200 | [diff] [blame] | 301 | char *igt_frame_dump_path; |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 302 | |
Chris Wilson | 7763349 | 2015-03-26 08:11:43 +0000 | [diff] [blame] | 303 | const char *igt_test_name(void) |
| 304 | { |
| 305 | return command_str; |
| 306 | } |
| 307 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 308 | static void _igt_log_buffer_append(char *line) |
| 309 | { |
| 310 | pthread_mutex_lock(&log_buffer_mutex); |
| 311 | |
| 312 | free(log_buffer.entries[log_buffer.end]); |
| 313 | log_buffer.entries[log_buffer.end] = line; |
| 314 | log_buffer.end++; |
| 315 | if (log_buffer.end == log_buffer.start) |
| 316 | log_buffer.start++; |
| 317 | |
| 318 | pthread_mutex_unlock(&log_buffer_mutex); |
| 319 | } |
| 320 | |
| 321 | static void _igt_log_buffer_reset(void) |
| 322 | { |
| 323 | pthread_mutex_lock(&log_buffer_mutex); |
| 324 | |
| 325 | log_buffer.start = log_buffer.end = 0; |
| 326 | |
| 327 | pthread_mutex_unlock(&log_buffer_mutex); |
| 328 | } |
| 329 | |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 330 | static void _igt_log_buffer_dump(void) |
| 331 | { |
| 332 | uint8_t i; |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 333 | |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 334 | if (in_subtest) |
| 335 | fprintf(stderr, "Subtest %s failed.\n", in_subtest); |
| 336 | else |
| 337 | fprintf(stderr, "Test %s failed.\n", command_str); |
| 338 | |
| 339 | if (log_buffer.start == log_buffer.end) { |
| 340 | fprintf(stderr, "No log.\n"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | pthread_mutex_lock(&log_buffer_mutex); |
Chris Wilson | b8db5d6 | 2015-03-06 08:49:52 +0000 | [diff] [blame] | 345 | fprintf(stderr, "**** DEBUG ****\n"); |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 346 | |
| 347 | i = log_buffer.start; |
| 348 | do { |
| 349 | char *last_line = log_buffer.entries[i]; |
Daniel Vetter | f45698d | 2015-02-27 19:32:33 +0100 | [diff] [blame] | 350 | fprintf(stderr, "%s", last_line); |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 351 | i++; |
| 352 | } while (i != log_buffer.start && i != log_buffer.end); |
| 353 | |
| 354 | /* reset the buffer */ |
| 355 | log_buffer.start = log_buffer.end = 0; |
| 356 | |
Chris Wilson | b8db5d6 | 2015-03-06 08:49:52 +0000 | [diff] [blame] | 357 | fprintf(stderr, "**** END ****\n"); |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 358 | pthread_mutex_unlock(&log_buffer_mutex); |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 359 | } |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 360 | |
Abdiel Janulgue | ef48fc8 | 2017-04-13 16:18:37 +0300 | [diff] [blame] | 361 | /** |
| 362 | * igt_log_buffer_inspect: |
| 363 | * |
| 364 | * Provides a way to replay the internal igt log buffer for inspection. |
| 365 | * @check: A user-specified handler that gets invoked for each line of |
| 366 | * the log buffer. The handler should return true to stop |
| 367 | * inspecting the rest of the buffer. |
| 368 | * @data: passed as a user argument to the inspection function. |
| 369 | */ |
| 370 | void igt_log_buffer_inspect(igt_buffer_log_handler_t check, void *data) |
| 371 | { |
| 372 | uint8_t i; |
| 373 | pthread_mutex_lock(&log_buffer_mutex); |
| 374 | |
| 375 | i = log_buffer.start; |
| 376 | do { |
| 377 | if (check(log_buffer.entries[i], data)) |
| 378 | break; |
| 379 | i++; |
| 380 | } while (i != log_buffer.start && i != log_buffer.end); |
| 381 | |
| 382 | pthread_mutex_unlock(&log_buffer_mutex); |
| 383 | } |
| 384 | |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 385 | __attribute__((format(printf, 1, 2))) |
| 386 | static void kmsg(const char *format, ...) |
Chris Wilson | de1e877 | 2014-07-28 08:58:32 +0100 | [diff] [blame] | 387 | #define KERN_EMER "<0>" |
| 388 | #define KERN_ALERT "<1>" |
| 389 | #define KERN_CRIT "<2>" |
| 390 | #define KERN_ERR "<3>" |
| 391 | #define KERN_WARNING "<4>" |
| 392 | #define KERN_NOTICE "<5>" |
| 393 | #define KERN_INFO "<6>" |
| 394 | #define KERN_DEBUG "<7>" |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 395 | { |
| 396 | va_list ap; |
| 397 | FILE *file; |
| 398 | |
| 399 | file = fopen("/dev/kmsg", "w"); |
| 400 | if (file == NULL) |
| 401 | return; |
| 402 | |
| 403 | va_start(ap, format); |
| 404 | vfprintf(file, format, ap); |
| 405 | va_end(ap); |
| 406 | |
| 407 | fclose(file); |
| 408 | } |
| 409 | |
Joonas Lahtinen | d84e624 | 2015-11-20 13:57:11 +0200 | [diff] [blame] | 410 | #define time_valid(ts) ((ts)->tv_sec || (ts)->tv_nsec) |
| 411 | |
| 412 | static double |
| 413 | time_elapsed(struct timespec *then, |
| 414 | struct timespec* now) |
| 415 | { |
| 416 | double elapsed = -1.; |
| 417 | |
| 418 | if (time_valid(then) && time_valid(now)) { |
| 419 | elapsed = now->tv_sec - then->tv_sec; |
| 420 | elapsed += (now->tv_nsec - then->tv_nsec) * 1e-9; |
| 421 | } |
| 422 | |
| 423 | return elapsed; |
| 424 | } |
| 425 | |
| 426 | static int gettime(struct timespec *ts) |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 427 | { |
| 428 | memset(ts, 0, sizeof(*ts)); |
Joonas Lahtinen | d84e624 | 2015-11-20 13:57:11 +0200 | [diff] [blame] | 429 | errno = 0; |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 430 | |
Joonas Lahtinen | d84e624 | 2015-11-20 13:57:11 +0200 | [diff] [blame] | 431 | /* Stay on the same clock for consistency. */ |
| 432 | if (igt_clock != (clockid_t)-1) { |
| 433 | if (clock_gettime(igt_clock, ts)) |
| 434 | goto error; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | #ifdef CLOCK_MONOTONIC_RAW |
| 439 | if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_RAW, ts)) |
| 440 | return 0; |
Alan Coopersmith | 843beb1 | 2014-12-23 19:07:13 -0800 | [diff] [blame] | 441 | #endif |
Joonas Lahtinen | d84e624 | 2015-11-20 13:57:11 +0200 | [diff] [blame] | 442 | #ifdef CLOCK_MONOTONIC_COARSE |
| 443 | if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_COARSE, ts)) |
| 444 | return 0; |
| 445 | #endif |
| 446 | if (!clock_gettime(igt_clock = CLOCK_MONOTONIC, ts)) |
| 447 | return 0; |
| 448 | error: |
| 449 | igt_warn("Could not read monotonic time: %s\n", |
| 450 | strerror(errno)); |
| 451 | |
| 452 | return -errno; |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Chris Wilson | dcb39b5 | 2016-03-18 09:04:07 +0000 | [diff] [blame] | 455 | uint64_t igt_nsec_elapsed(struct timespec *start) |
| 456 | { |
| 457 | struct timespec now; |
| 458 | |
| 459 | gettime(&now); |
| 460 | if ((start->tv_sec | start->tv_nsec) == 0) { |
| 461 | *start = now; |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | return ((now.tv_nsec - start->tv_nsec) + |
Chris Wilson | cf07aa2 | 2016-05-02 15:35:56 +0100 | [diff] [blame] | 466 | (uint64_t)NSEC_PER_SEC*(now.tv_sec - start->tv_sec)); |
Chris Wilson | dcb39b5 | 2016-03-18 09:04:07 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 469 | bool __igt_fixture(void) |
| 470 | { |
| 471 | assert(!in_fixture); |
| 472 | |
| 473 | if (igt_only_list_subtests()) |
| 474 | return false; |
| 475 | |
| 476 | if (skip_subtests_henceforth) |
| 477 | return false; |
| 478 | |
| 479 | in_fixture = true; |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | void __igt_fixture_complete(void) |
| 484 | { |
| 485 | assert(in_fixture); |
| 486 | |
| 487 | in_fixture = false; |
| 488 | } |
| 489 | |
| 490 | void __igt_fixture_end(void) |
| 491 | { |
| 492 | assert(in_fixture); |
| 493 | |
| 494 | in_fixture = false; |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 495 | siglongjmp(igt_subtest_jmpbuf, 1); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 498 | /* |
| 499 | * Some of the IGT tests put quite a lot of pressure on memory and when |
| 500 | * running on Android they are sometimes killed by the Android low memory killer. |
| 501 | * This seems to be due to some incompatibility between the kswapd free memory |
| 502 | * targets and the way the lowmemorykiller assesses free memory. |
| 503 | * The low memory killer really isn't usefull in this context and has no |
| 504 | * interaction with the gpu driver that we are testing, so the following |
| 505 | * function is used to disable it by modifying one of its module parameters. |
| 506 | * We still have the normal linux oom killer to protect the kernel. |
| 507 | * Apparently it is also possible for the lowmemorykiller to get included |
| 508 | * in some linux distributions; so rather than check for Android we directly |
| 509 | * check for the existence of the module parameter we want to adjust. |
| 510 | * |
| 511 | * In future, if we can get the lowmemorykiller to play nicely then we can |
| 512 | * remove this hack. |
| 513 | */ |
| 514 | static void low_mem_killer_disable(bool disable) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 515 | { |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 516 | static const char* adj_fname="/sys/module/lowmemorykiller/parameters/adj"; |
| 517 | static const char no_lowmem_killer[] = "9999"; |
| 518 | int fd; |
| 519 | struct stat buf; |
| 520 | /* The following must persist across invocations */ |
| 521 | static char prev_adj_scores[256]; |
| 522 | static int adj_scores_len = 0; |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 523 | static bool is_disabled = false; |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 524 | |
| 525 | /* capture the permissions bits for the lowmemkiller adj pseudo-file. |
| 526 | * Bail out if the stat fails; it probably means that there is no |
| 527 | * lowmemorykiller, but in any case we're doomed. */ |
| 528 | if (stat(adj_fname, &buf)) { |
| 529 | igt_assert(errno == ENOENT); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | /* make sure the file can be read/written - by default it is write-only */ |
| 534 | chmod(adj_fname, S_IRUSR | S_IWUSR); |
| 535 | |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 536 | if (disable && !is_disabled) { |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 537 | /* read the current oom adj parameters for lowmemorykiller */ |
| 538 | fd = open(adj_fname, O_RDWR); |
| 539 | igt_assert(fd != -1); |
| 540 | adj_scores_len = read(fd, (void*)prev_adj_scores, 255); |
| 541 | igt_assert(adj_scores_len > 0); |
| 542 | |
| 543 | /* writing 9999 to this module parameter effectively diables the |
| 544 | * low memory killer. This is not a real file, so we dont need to |
| 545 | * seek to the start or truncate it */ |
Thomas Wood | 47f6b13 | 2015-03-25 16:42:57 +0000 | [diff] [blame] | 546 | igt_assert_eq(write(fd, no_lowmem_killer, sizeof(no_lowmem_killer)), |
| 547 | sizeof(no_lowmem_killer)); |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 548 | close(fd); |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 549 | is_disabled = true; |
| 550 | } else if (is_disabled) { |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 551 | /* just re-enstate the original settings */ |
| 552 | fd = open(adj_fname, O_WRONLY); |
| 553 | igt_assert(fd != -1); |
Thomas Wood | 47f6b13 | 2015-03-25 16:42:57 +0000 | [diff] [blame] | 554 | igt_assert_eq(write(fd, prev_adj_scores, adj_scores_len), |
| 555 | adj_scores_len); |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 556 | close(fd); |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 557 | is_disabled = false; |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* re-enstate the file permissions */ |
| 561 | chmod(adj_fname, buf.st_mode); |
| 562 | } |
| 563 | |
| 564 | bool igt_exit_called; |
| 565 | static void common_exit_handler(int sig) |
| 566 | { |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 567 | if (!igt_only_list_subtests()) { |
| 568 | low_mem_killer_disable(false); |
Chris Wilson | 9c4114e | 2016-12-21 16:37:46 +0000 | [diff] [blame] | 569 | kick_fbcon(true); |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 570 | } |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 571 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 572 | /* When not killed by a signal check that igt_exit() has been properly |
| 573 | * called. */ |
| 574 | assert(sig != 0 || igt_exit_called); |
| 575 | } |
| 576 | |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 577 | static void print_test_description(void) |
| 578 | { |
| 579 | if (&__igt_test_description) |
| 580 | printf("%s\n", __igt_test_description); |
| 581 | } |
| 582 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 583 | static void print_version(void) |
| 584 | { |
| 585 | struct utsname uts; |
| 586 | |
| 587 | if (list_subtests) |
| 588 | return; |
| 589 | |
| 590 | uname(&uts); |
| 591 | |
Daniel Vetter | 80cc540 | 2017-08-23 15:30:44 +0200 | [diff] [blame] | 592 | igt_info("IGT-Version: %s-%s (%s) (%s: %s %s)\n", PACKAGE_VERSION, |
| 593 | IGT_GIT_SHA1, TARGET_CPU_PLATFORM, |
| 594 | uts.sysname, uts.release, uts.machine); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Chris Wilson | 52915fc | 2014-07-24 12:46:00 +0100 | [diff] [blame] | 597 | static void print_usage(const char *help_str, bool output_on_stderr) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 598 | { |
| 599 | FILE *f = output_on_stderr ? stderr : stdout; |
| 600 | |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 601 | fprintf(f, "Usage: %s [OPTIONS]\n", command_str); |
| 602 | fprintf(f, " --list-subtests\n" |
Thomas Wood | 561d519 | 2014-07-23 10:38:24 +0100 | [diff] [blame] | 603 | " --run-subtest <pattern>\n" |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 604 | " --debug[=log-domain]\n" |
Rodrigo Vivi | 3d65ff7 | 2015-01-12 10:21:58 -0800 | [diff] [blame] | 605 | " --interactive-debug[=domain]\n" |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 606 | " --help-description\n" |
| 607 | " --help\n"); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 608 | if (help_str) |
| 609 | fprintf(f, "%s\n", help_str); |
| 610 | } |
| 611 | |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 612 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 613 | static void oom_adjust_for_doom(void) |
| 614 | { |
| 615 | int fd; |
| 616 | const char always_kill[] = "1000"; |
| 617 | |
| 618 | fd = open("/proc/self/oom_score_adj", O_WRONLY); |
| 619 | igt_assert(fd != -1); |
| 620 | igt_assert(write(fd, always_kill, sizeof(always_kill)) == sizeof(always_kill)); |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 621 | close(fd); |
| 622 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 625 | #ifdef HAVE_GLIB |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 626 | static void common_init_config(void) |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 627 | { |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 628 | char *key_file_env = NULL; |
| 629 | char *key_file_loc = NULL; |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 630 | GError *error = NULL; |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 631 | int ret; |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 632 | |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 633 | /* Determine igt config path */ |
| 634 | key_file_env = getenv("IGT_CONFIG_PATH"); |
| 635 | if (key_file_env) { |
| 636 | key_file_loc = key_file_env; |
| 637 | } else { |
| 638 | key_file_loc = malloc(100); |
| 639 | snprintf(key_file_loc, 100, "%s/.igtrc", g_get_home_dir()); |
| 640 | } |
| 641 | |
| 642 | /* Load igt config file */ |
| 643 | igt_key_file = g_key_file_new(); |
| 644 | ret = g_key_file_load_from_file(igt_key_file, key_file_loc, |
| 645 | G_KEY_FILE_NONE, &error); |
| 646 | if (error && error->code == G_KEY_FILE_ERROR) { |
| 647 | g_error_free(error); |
| 648 | g_key_file_free(igt_key_file); |
| 649 | igt_key_file = NULL; |
| 650 | |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | g_clear_error(&error); |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 655 | |
Daniel Vetter | a3a3e0f | 2017-09-05 14:36:08 +0200 | [diff] [blame] | 656 | if (!igt_frame_dump_path) |
| 657 | igt_frame_dump_path = |
| 658 | g_key_file_get_string(igt_key_file, "Common", |
| 659 | "FrameDumpPath", &error); |
Paul Kocialkowski | ee31e0b | 2017-07-19 16:46:07 +0300 | [diff] [blame] | 660 | |
Paul Kocialkowski | a865fee | 2017-07-20 11:07:42 +0300 | [diff] [blame] | 661 | g_clear_error(&error); |
| 662 | |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 663 | ret = g_key_file_get_integer(igt_key_file, "DUT", "SuspendResumeDelay", |
| 664 | &error); |
| 665 | assert(!error || error->code != G_KEY_FILE_ERROR_INVALID_VALUE); |
Paul Kocialkowski | a865fee | 2017-07-20 11:07:42 +0300 | [diff] [blame] | 666 | |
| 667 | g_clear_error(&error); |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 668 | |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 669 | if (ret != 0) |
| 670 | igt_set_autoresume_delay(ret); |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 671 | |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 672 | out: |
| 673 | if (!key_file_env && key_file_loc) |
| 674 | free(key_file_loc); |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 675 | } |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 676 | #endif |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 677 | |
Paul Kocialkowski | 493151b | 2017-07-20 17:11:27 +0300 | [diff] [blame] | 678 | static void common_init_env(void) |
| 679 | { |
| 680 | const char *env; |
| 681 | |
| 682 | if (!isatty(STDOUT_FILENO) || getenv("IGT_PLAIN_OUTPUT")) |
| 683 | __igt_plain_output = true; |
| 684 | |
| 685 | if (!__igt_plain_output) |
| 686 | setlocale(LC_ALL, ""); |
| 687 | |
| 688 | env = getenv("IGT_LOG_LEVEL"); |
| 689 | if (env) { |
| 690 | if (strcmp(env, "debug") == 0) |
| 691 | igt_log_level = IGT_LOG_DEBUG; |
| 692 | else if (strcmp(env, "info") == 0) |
| 693 | igt_log_level = IGT_LOG_INFO; |
| 694 | else if (strcmp(env, "warn") == 0) |
| 695 | igt_log_level = IGT_LOG_WARN; |
| 696 | else if (strcmp(env, "none") == 0) |
| 697 | igt_log_level = IGT_LOG_NONE; |
| 698 | } |
| 699 | |
Daniel Vetter | a3a3e0f | 2017-09-05 14:36:08 +0200 | [diff] [blame] | 700 | igt_frame_dump_path = getenv("IGT_FRAME_DUMP_PATH"); |
Paul Kocialkowski | 493151b | 2017-07-20 17:11:27 +0300 | [diff] [blame] | 701 | } |
| 702 | |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 703 | static int common_init(int *argc, char **argv, |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 704 | const char *extra_short_opts, |
Ville Syrjälä | 793aff1 | 2015-12-18 19:25:45 +0200 | [diff] [blame] | 705 | const struct option *extra_long_opts, |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 706 | const char *help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 707 | igt_opt_handler_t extra_opt_handler, |
| 708 | void *handler_data) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 709 | { |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 710 | int c, option_index = 0, i, x; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 711 | static struct option long_options[] = { |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 712 | {"list-subtests", 0, 0, OPT_LIST_SUBTESTS}, |
| 713 | {"run-subtest", 1, 0, OPT_RUN_SUBTEST}, |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 714 | {"help-description", 0, 0, OPT_DESCRIPTION}, |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 715 | {"debug", optional_argument, 0, OPT_DEBUG}, |
Rodrigo Vivi | 3d65ff7 | 2015-01-12 10:21:58 -0800 | [diff] [blame] | 716 | {"interactive-debug", optional_argument, 0, OPT_INTERACTIVE_DEBUG}, |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 717 | {"help", 0, 0, OPT_HELP}, |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 718 | {0, 0, 0, 0} |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 719 | }; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 720 | char *short_opts; |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 721 | const char *std_short_opts = "h"; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 722 | struct option *combined_opts; |
| 723 | int extra_opt_count; |
| 724 | int all_opt_count; |
| 725 | int ret = 0; |
| 726 | |
Paul Kocialkowski | 493151b | 2017-07-20 17:11:27 +0300 | [diff] [blame] | 727 | common_init_env(); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 728 | |
| 729 | command_str = argv[0]; |
| 730 | if (strrchr(command_str, '/')) |
| 731 | command_str = strrchr(command_str, '/') + 1; |
| 732 | |
| 733 | /* First calculate space for all passed-in extra long options */ |
| 734 | all_opt_count = 0; |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 735 | while (extra_long_opts && extra_long_opts[all_opt_count].name) { |
| 736 | |
| 737 | /* check for conflicts with standard long option values */ |
| 738 | for (i = 0; long_options[i].name; i++) |
| 739 | if (extra_long_opts[all_opt_count].val == long_options[i].val) |
| 740 | igt_warn("Conflicting long option values between --%s and --%s\n", |
| 741 | extra_long_opts[all_opt_count].name, |
| 742 | long_options[i].name); |
| 743 | |
| 744 | /* check for conflicts with short options */ |
| 745 | if (extra_long_opts[all_opt_count].val != ':' |
| 746 | && strchr(std_short_opts, extra_long_opts[all_opt_count].val)) { |
| 747 | igt_warn("Conflicting long and short option values between --%s and -%s\n", |
| 748 | extra_long_opts[all_opt_count].name, |
| 749 | long_options[i].name); |
| 750 | } |
| 751 | |
| 752 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 753 | all_opt_count++; |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 754 | } |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 755 | extra_opt_count = all_opt_count; |
| 756 | |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 757 | /* check for conflicts in extra short options*/ |
| 758 | for (i = 0; extra_short_opts && extra_short_opts[i]; i++) { |
| 759 | |
| 760 | if (extra_short_opts[i] == ':') |
| 761 | continue; |
| 762 | |
| 763 | /* check for conflicts with standard short options */ |
| 764 | if (strchr(std_short_opts, extra_short_opts[i])) |
| 765 | igt_warn("Conflicting short option: -%c\n", std_short_opts[i]); |
| 766 | |
| 767 | /* check for conflicts with standard long option values */ |
| 768 | for (x = 0; long_options[x].name; x++) |
| 769 | if (long_options[x].val == extra_short_opts[i]) |
| 770 | igt_warn("Conflicting short option and long option value: --%s and -%c\n", |
| 771 | long_options[x].name, extra_short_opts[i]); |
| 772 | } |
| 773 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 774 | all_opt_count += ARRAY_SIZE(long_options); |
| 775 | |
| 776 | combined_opts = malloc(all_opt_count * sizeof(*combined_opts)); |
| 777 | memcpy(combined_opts, extra_long_opts, |
| 778 | extra_opt_count * sizeof(*combined_opts)); |
| 779 | |
| 780 | /* Copy the subtest long options (and the final NULL entry) */ |
| 781 | memcpy(&combined_opts[extra_opt_count], long_options, |
| 782 | ARRAY_SIZE(long_options) * sizeof(*combined_opts)); |
| 783 | |
Thomas Wood | c3ba774 | 2014-07-28 15:37:16 +0100 | [diff] [blame] | 784 | ret = asprintf(&short_opts, "%s%s", |
| 785 | extra_short_opts ? extra_short_opts : "", |
| 786 | std_short_opts); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 787 | assert(ret >= 0); |
| 788 | |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 789 | while ((c = getopt_long(*argc, argv, short_opts, combined_opts, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 790 | &option_index)) != -1) { |
| 791 | switch(c) { |
Rodrigo Vivi | 3d65ff7 | 2015-01-12 10:21:58 -0800 | [diff] [blame] | 792 | case OPT_INTERACTIVE_DEBUG: |
| 793 | if (optarg && strlen(optarg) > 0) |
| 794 | igt_interactive_debug = strdup(optarg); |
| 795 | else |
| 796 | igt_interactive_debug = "all"; |
| 797 | break; |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 798 | case OPT_DEBUG: |
Thomas Wood | 561d519 | 2014-07-23 10:38:24 +0100 | [diff] [blame] | 799 | igt_log_level = IGT_LOG_DEBUG; |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 800 | if (optarg && strlen(optarg) > 0) |
| 801 | igt_log_domain_filter = strdup(optarg); |
Thomas Wood | 561d519 | 2014-07-23 10:38:24 +0100 | [diff] [blame] | 802 | break; |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 803 | case OPT_LIST_SUBTESTS: |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 804 | if (!run_single_subtest) |
| 805 | list_subtests = true; |
| 806 | break; |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 807 | case OPT_RUN_SUBTEST: |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 808 | if (!list_subtests) |
| 809 | run_single_subtest = strdup(optarg); |
| 810 | break; |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 811 | case OPT_DESCRIPTION: |
| 812 | print_test_description(); |
| 813 | ret = -1; |
| 814 | goto out; |
Thomas Wood | fc5e257 | 2014-07-25 16:54:41 +0100 | [diff] [blame] | 815 | case OPT_HELP: |
Chris Wilson | 52915fc | 2014-07-24 12:46:00 +0100 | [diff] [blame] | 816 | print_usage(help_str, false); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 817 | ret = -1; |
| 818 | goto out; |
| 819 | case '?': |
Chris Wilson | 52915fc | 2014-07-24 12:46:00 +0100 | [diff] [blame] | 820 | print_usage(help_str, true); |
Thomas Wood | c584d5c | 2014-07-21 16:17:06 +0100 | [diff] [blame] | 821 | ret = -2; |
| 822 | goto out; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 823 | default: |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 824 | ret = extra_opt_handler(c, option_index, handler_data); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 825 | if (ret) |
| 826 | goto out; |
| 827 | } |
| 828 | } |
| 829 | |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 830 | #ifdef HAVE_GLIB |
Paul Kocialkowski | f39be72 | 2017-07-20 17:11:26 +0300 | [diff] [blame] | 831 | common_init_config(); |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 832 | #endif |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 833 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 834 | out: |
| 835 | free(short_opts); |
| 836 | free(combined_opts); |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 837 | |
| 838 | /* exit immediately if this test has no subtests and a subtest or the |
| 839 | * list of subtests has been requested */ |
| 840 | if (!test_with_subtests) { |
| 841 | if (run_single_subtest) { |
| 842 | igt_warn("Unknown subtest: %s\n", run_single_subtest); |
Thomas Wood | 784344e | 2014-07-23 16:29:39 +0100 | [diff] [blame] | 843 | exit(IGT_EXIT_INVALID); |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 844 | } |
| 845 | if (list_subtests) |
Thomas Wood | 784344e | 2014-07-23 16:29:39 +0100 | [diff] [blame] | 846 | exit(IGT_EXIT_INVALID); |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (ret < 0) |
| 850 | /* exit with no error for -h/--help */ |
Thomas Wood | 784344e | 2014-07-23 16:29:39 +0100 | [diff] [blame] | 851 | exit(ret == -1 ? 0 : IGT_EXIT_INVALID); |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 852 | |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 853 | if (!list_subtests) { |
Chris Wilson | 9c4114e | 2016-12-21 16:37:46 +0000 | [diff] [blame] | 854 | kick_fbcon(false); |
Jani Nikula | 230df2d | 2016-10-14 12:52:00 +0300 | [diff] [blame] | 855 | kmsg(KERN_INFO "[IGT] %s: executing\n", command_str); |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 856 | print_version(); |
| 857 | |
Tvrtko Ursulin | 00da3d1 | 2017-09-05 11:38:58 +0100 | [diff] [blame] | 858 | sync(); |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 859 | oom_adjust_for_doom(); |
Tim Gore | 48a27bc | 2015-04-27 16:17:50 +0100 | [diff] [blame] | 860 | low_mem_killer_disable(true); |
Chris Wilson | b9e7637 | 2014-07-25 14:29:47 +0100 | [diff] [blame] | 861 | } |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 862 | |
Tim Gore | 5782eca | 2014-10-01 13:25:20 +0100 | [diff] [blame] | 863 | /* install exit handler, to ensure we clean up */ |
| 864 | igt_install_exit_handler(common_exit_handler); |
| 865 | |
Thomas Wood | 16cfa37 | 2015-01-27 14:12:27 +0000 | [diff] [blame] | 866 | if (!test_with_subtests) |
| 867 | gettime(&subtest_time); |
| 868 | |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 869 | for (i = 0; (optind + i) < *argc; i++) |
| 870 | argv[i + 1] = argv[optind + i]; |
| 871 | |
| 872 | *argc = *argc - optind + 1; |
| 873 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 874 | return ret; |
| 875 | } |
| 876 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 877 | |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 878 | /** |
| 879 | * igt_subtest_init_parse_opts: |
| 880 | * @argc: argc from the test's main() |
| 881 | * @argv: argv from the test's main() |
| 882 | * @extra_short_opts: getopt_long() compliant list with additional short options |
| 883 | * @extra_long_opts: getopt_long() compliant list with additional long options |
| 884 | * @help_str: help string for the additional options |
| 885 | * @extra_opt_handler: handler for the additional options |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 886 | * @handler_data: user data given to @extra_opt_handler when invoked |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 887 | * |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 888 | * This function handles the subtest related command line options and allows an |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 889 | * arbitrary set of additional options. This is useful for tests which have |
| 890 | * additional knobs to tune when run manually like the number of rounds execute |
| 891 | * or the size of the allocated buffer objects. |
| 892 | * |
| 893 | * Tests without special needs should just use igt_subtest_init() or use |
| 894 | * #igt_main directly instead of their own main() function. |
| 895 | * |
| 896 | * Returns: Forwards any option parsing errors from getopt_long. |
| 897 | */ |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 898 | int igt_subtest_init_parse_opts(int *argc, char **argv, |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 899 | const char *extra_short_opts, |
Ville Syrjälä | 793aff1 | 2015-12-18 19:25:45 +0200 | [diff] [blame] | 900 | const struct option *extra_long_opts, |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 901 | const char *help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 902 | igt_opt_handler_t extra_opt_handler, |
| 903 | void *handler_data) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 904 | { |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 905 | int ret; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 906 | |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 907 | test_with_subtests = true; |
| 908 | ret = common_init(argc, argv, extra_short_opts, extra_long_opts, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 909 | help_str, extra_opt_handler, handler_data); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 910 | |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 911 | return ret; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Thomas Wood | 94ac74e | 2014-07-18 16:24:34 +0100 | [diff] [blame] | 914 | enum igt_log_level igt_log_level = IGT_LOG_INFO; |
| 915 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 916 | /** |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 917 | * igt_simple_init_parse_opts: |
| 918 | * @argc: argc from the test's main() |
| 919 | * @argv: argv from the test's main() |
| 920 | * @extra_short_opts: getopt_long() compliant list with additional short options |
| 921 | * @extra_long_opts: getopt_long() compliant list with additional long options |
| 922 | * @help_str: help string for the additional options |
| 923 | * @extra_opt_handler: handler for the additional options |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 924 | * @handler_data: user data given to @extra_opt_handler when invoked |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 925 | * |
| 926 | * This initializes a simple test without any support for subtests and allows |
| 927 | * an arbitrary set of additional options. |
| 928 | */ |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 929 | void igt_simple_init_parse_opts(int *argc, char **argv, |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 930 | const char *extra_short_opts, |
Ville Syrjälä | 793aff1 | 2015-12-18 19:25:45 +0200 | [diff] [blame] | 931 | const struct option *extra_long_opts, |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 932 | const char *help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 933 | igt_opt_handler_t extra_opt_handler, |
| 934 | void *handler_data) |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 935 | { |
| 936 | common_init(argc, argv, extra_short_opts, extra_long_opts, help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 937 | extra_opt_handler, handler_data); |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 940 | /* |
| 941 | * Note: Testcases which use these helpers MUST NOT output anything to stdout |
| 942 | * outside of places protected by igt_run_subtest checks - the piglit |
| 943 | * runner adds every line to the subtest list. |
| 944 | */ |
| 945 | bool __igt_run_subtest(const char *subtest_name) |
| 946 | { |
Thomas Wood | 2b05ecf | 2015-01-26 16:59:53 +0000 | [diff] [blame] | 947 | int i; |
| 948 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 949 | assert(!in_subtest); |
| 950 | assert(!in_fixture); |
Thomas Wood | dbac905 | 2014-11-25 14:10:04 +0000 | [diff] [blame] | 951 | assert(test_with_subtests); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 952 | |
Thomas Wood | 2b05ecf | 2015-01-26 16:59:53 +0000 | [diff] [blame] | 953 | /* check the subtest name only contains a-z, A-Z, 0-9, '-' and '_' */ |
| 954 | for (i = 0; subtest_name[i] != '\0'; i++) |
| 955 | if (subtest_name[i] != '_' && subtest_name[i] != '-' |
| 956 | && !isalnum(subtest_name[i])) { |
| 957 | igt_critical("Invalid subtest name \"%s\".\n", |
| 958 | subtest_name); |
| 959 | igt_exit(); |
| 960 | } |
| 961 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 962 | if (list_subtests) { |
| 963 | printf("%s\n", subtest_name); |
| 964 | return false; |
| 965 | } |
| 966 | |
Thomas Wood | e057b86 | 2014-07-18 14:10:27 +0100 | [diff] [blame] | 967 | if (run_single_subtest) { |
Derek Morton | ef724ab | 2016-02-04 12:06:57 +0000 | [diff] [blame] | 968 | if (uwildmat(subtest_name, run_single_subtest) == 0) |
Thomas Wood | e057b86 | 2014-07-18 14:10:27 +0100 | [diff] [blame] | 969 | return false; |
| 970 | else |
| 971 | run_single_subtest_found = true; |
| 972 | } |
| 973 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 974 | if (skip_subtests_henceforth) { |
Thomas Wood | fca7b1b | 2015-10-29 17:01:17 +0000 | [diff] [blame] | 975 | printf("%sSubtest %s: %s%s\n", |
Thomas Wood | 790f1f8 | 2015-11-09 17:17:24 +0000 | [diff] [blame] | 976 | (!__igt_plain_output) ? "\x1b[1m" : "", subtest_name, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 977 | skip_subtests_henceforth == SKIP ? |
Thomas Wood | 790f1f8 | 2015-11-09 17:17:24 +0000 | [diff] [blame] | 978 | "SKIP" : "FAIL", (!__igt_plain_output) ? "\x1b[0m" : ""); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 979 | return false; |
| 980 | } |
| 981 | |
Jani Nikula | 230df2d | 2016-10-14 12:52:00 +0300 | [diff] [blame] | 982 | kmsg(KERN_INFO "[IGT] %s: starting subtest %s\n", command_str, subtest_name); |
Thomas Wood | 9942a40 | 2015-01-27 14:10:15 +0000 | [diff] [blame] | 983 | igt_debug("Starting subtest: %s\n", subtest_name); |
Chris Wilson | 52915fc | 2014-07-24 12:46:00 +0100 | [diff] [blame] | 984 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 985 | _igt_log_buffer_reset(); |
| 986 | |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 987 | gettime(&subtest_time); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 988 | return (in_subtest = subtest_name); |
| 989 | } |
| 990 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 991 | /** |
| 992 | * igt_subtest_name: |
| 993 | * |
| 994 | * Returns: The name of the currently executed subtest or NULL if called from |
| 995 | * outside a subtest block. |
| 996 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 997 | const char *igt_subtest_name(void) |
| 998 | { |
| 999 | return in_subtest; |
| 1000 | } |
| 1001 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * igt_only_list_subtests: |
| 1004 | * |
| 1005 | * Returns: Returns true if only subtest should be listed and any setup code |
| 1006 | * must be skipped, false otherwise. |
| 1007 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1008 | bool igt_only_list_subtests(void) |
| 1009 | { |
| 1010 | return list_subtests; |
| 1011 | } |
| 1012 | |
Daniel Vetter | 72d04b8 | 2016-03-18 21:46:54 +0100 | [diff] [blame] | 1013 | void __igt_subtest_group_save(int *save) |
| 1014 | { |
| 1015 | *save = skip_subtests_henceforth; |
| 1016 | } |
| 1017 | |
| 1018 | void __igt_subtest_group_restore(int save) |
| 1019 | { |
| 1020 | skip_subtests_henceforth = save; |
| 1021 | } |
| 1022 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1023 | static bool skipped_one = false; |
| 1024 | static bool succeeded_one = false; |
| 1025 | static bool failed_one = false; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1026 | |
| 1027 | static void exit_subtest(const char *) __attribute__((noreturn)); |
| 1028 | static void exit_subtest(const char *result) |
| 1029 | { |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 1030 | struct timespec now; |
Chris Wilson | 91a4327 | 2014-09-06 08:40:04 +0100 | [diff] [blame] | 1031 | |
| 1032 | gettime(&now); |
Daniel Vetter | 80cc540 | 2017-08-23 15:30:44 +0200 | [diff] [blame] | 1033 | igt_info("%sSubtest %s: %s (%.3fs)%s\n", |
| 1034 | (!__igt_plain_output) ? "\x1b[1m" : "", |
| 1035 | in_subtest, result, time_elapsed(&subtest_time, &now), |
| 1036 | (!__igt_plain_output) ? "\x1b[0m" : ""); |
Paulo Zanoni | 6cc553a | 2015-06-25 14:12:56 -0300 | [diff] [blame] | 1037 | fflush(stdout); |
| 1038 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1039 | in_subtest = NULL; |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 1040 | siglongjmp(igt_subtest_jmpbuf, 1); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1043 | /** |
| 1044 | * igt_skip: |
| 1045 | * @f: format string |
| 1046 | * @...: optional arguments used in the format string |
| 1047 | * |
| 1048 | * Subtest aware test skipping. The format string is printed to stderr as the |
| 1049 | * reason why the test skipped. |
| 1050 | * |
| 1051 | * For tests with subtests this will either bail out of the current subtest or |
| 1052 | * mark all subsequent subtests as SKIP (presuming some global setup code |
| 1053 | * failed). |
| 1054 | * |
| 1055 | * For normal tests without subtest it will directly exit. |
| 1056 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1057 | void igt_skip(const char *f, ...) |
| 1058 | { |
| 1059 | va_list args; |
| 1060 | skipped_one = true; |
| 1061 | |
| 1062 | assert(!test_child); |
| 1063 | |
| 1064 | if (!igt_only_list_subtests()) { |
| 1065 | va_start(args, f); |
| 1066 | vprintf(f, args); |
| 1067 | va_end(args); |
| 1068 | } |
| 1069 | |
| 1070 | if (in_subtest) { |
| 1071 | exit_subtest("SKIP"); |
| 1072 | } else if (test_with_subtests) { |
| 1073 | skip_subtests_henceforth = SKIP; |
| 1074 | assert(in_fixture); |
| 1075 | __igt_fixture_end(); |
| 1076 | } else { |
Tim Gore | f33fa71 | 2014-09-29 13:34:30 +0100 | [diff] [blame] | 1077 | igt_exitcode = IGT_EXIT_SKIP; |
| 1078 | igt_exit(); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | void __igt_skip_check(const char *file, const int line, |
| 1083 | const char *func, const char *check, |
| 1084 | const char *f, ...) |
| 1085 | { |
| 1086 | va_list args; |
| 1087 | int err = errno; |
Damien Lespiau | 4145462 | 2014-07-09 11:36:27 +0100 | [diff] [blame] | 1088 | char *err_str = NULL; |
| 1089 | |
| 1090 | if (err) |
Thomas Wood | 47f6b13 | 2015-03-25 16:42:57 +0000 | [diff] [blame] | 1091 | igt_assert_neq(asprintf(&err_str, "Last errno: %i, %s\n", err, strerror(err)), |
| 1092 | -1); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1093 | |
| 1094 | if (f) { |
| 1095 | static char *buf; |
| 1096 | |
| 1097 | /* igt_skip never returns, so try to not leak too badly. */ |
| 1098 | if (buf) |
| 1099 | free(buf); |
| 1100 | |
| 1101 | va_start(args, f); |
Thomas Wood | 47f6b13 | 2015-03-25 16:42:57 +0000 | [diff] [blame] | 1102 | igt_assert_neq(vasprintf(&buf, f, args), -1); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1103 | va_end(args); |
| 1104 | |
| 1105 | igt_skip("Test requirement not met in function %s, file %s:%i:\n" |
Chris Wilson | e0dffbd | 2014-07-25 14:28:51 +0100 | [diff] [blame] | 1106 | "Test requirement: %s\n%s" |
Damien Lespiau | 4145462 | 2014-07-09 11:36:27 +0100 | [diff] [blame] | 1107 | "%s", |
| 1108 | func, file, line, check, buf, err_str ?: ""); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1109 | } else { |
| 1110 | igt_skip("Test requirement not met in function %s, file %s:%i:\n" |
Chris Wilson | e0dffbd | 2014-07-25 14:28:51 +0100 | [diff] [blame] | 1111 | "Test requirement: %s\n" |
Damien Lespiau | 4145462 | 2014-07-09 11:36:27 +0100 | [diff] [blame] | 1112 | "%s", |
| 1113 | func, file, line, check, err_str ?: ""); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1117 | /** |
| 1118 | * igt_success: |
| 1119 | * |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 1120 | * Complete a (subtest) as successful |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1121 | * |
| 1122 | * This bails out of a subtests and marks it as successful. For global tests it |
| 1123 | * it won't bail out of anything. |
| 1124 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1125 | void igt_success(void) |
| 1126 | { |
| 1127 | succeeded_one = true; |
| 1128 | if (in_subtest) |
| 1129 | exit_subtest("SUCCESS"); |
| 1130 | } |
| 1131 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1132 | /** |
| 1133 | * igt_fail: |
| 1134 | * @exitcode: exitcode |
| 1135 | * |
| 1136 | * Fail a testcase. The exitcode is used as the exit code of the test process. |
| 1137 | * It may not be 0 (which indicates success) or 77 (which indicates a skipped |
| 1138 | * test). |
| 1139 | * |
| 1140 | * For tests with subtests this will either bail out of the current subtest or |
| 1141 | * mark all subsequent subtests as FAIL (presuming some global setup code |
| 1142 | * failed). |
| 1143 | * |
| 1144 | * For normal tests without subtest it will directly exit with the given |
| 1145 | * exitcode. |
| 1146 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1147 | void igt_fail(int exitcode) |
| 1148 | { |
Thomas Wood | 17eb062 | 2014-05-13 15:22:52 +0100 | [diff] [blame] | 1149 | assert(exitcode != IGT_EXIT_SUCCESS && exitcode != IGT_EXIT_SKIP); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1150 | |
Daniel Vetter | 4fec18e | 2015-02-27 20:24:15 +0100 | [diff] [blame] | 1151 | igt_debug_wait_for_keypress("failure"); |
| 1152 | |
Thomas Wood | e65c69b | 2015-07-16 17:39:59 +0100 | [diff] [blame] | 1153 | /* Exit immediately if the test is already exiting and igt_fail is |
| 1154 | * called. This can happen if an igt_assert fails in an exit handler */ |
| 1155 | if (in_atexit_handler) |
| 1156 | _exit(IGT_EXIT_FAILURE); |
| 1157 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1158 | if (!failed_one) |
| 1159 | igt_exitcode = exitcode; |
| 1160 | |
| 1161 | failed_one = true; |
| 1162 | |
| 1163 | /* Silent exit, parent will do the yelling. */ |
| 1164 | if (test_child) |
| 1165 | exit(exitcode); |
| 1166 | |
Thomas Wood | f8fd4c9 | 2015-01-20 15:31:51 +0000 | [diff] [blame] | 1167 | _igt_log_buffer_dump(); |
| 1168 | |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 1169 | if (in_subtest) { |
Thomas Wood | 17eb062 | 2014-05-13 15:22:52 +0100 | [diff] [blame] | 1170 | if (exitcode == IGT_EXIT_TIMEOUT) |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 1171 | exit_subtest("TIMEOUT"); |
| 1172 | else |
| 1173 | exit_subtest("FAIL"); |
| 1174 | } else { |
Daniel Vetter | 2459b80 | 2017-08-14 11:32:04 +0200 | [diff] [blame] | 1175 | assert(igt_can_fail()); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1176 | |
| 1177 | if (in_fixture) { |
| 1178 | skip_subtests_henceforth = FAIL; |
| 1179 | __igt_fixture_end(); |
| 1180 | } |
| 1181 | |
Tim Gore | f33fa71 | 2014-09-29 13:34:30 +0100 | [diff] [blame] | 1182 | igt_exit(); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
Daniel Vetter | 2459b80 | 2017-08-14 11:32:04 +0200 | [diff] [blame] | 1186 | /** |
| 1187 | * igt_can_fail: |
| 1188 | * |
| 1189 | * Returns true if called from either an #igt_fixture, #igt_subtest or a |
| 1190 | * testcase without subtests, i.e. #igt_simple_main. Returns false otherwise. In |
| 1191 | * other words, it checks whether it's legal to call igt_fail(), igt_skip_on() |
| 1192 | * and all the convenience macros build around those. |
| 1193 | * |
| 1194 | * This is useful to make sure that library code is called from the right |
| 1195 | * places. |
| 1196 | */ |
| 1197 | bool igt_can_fail(void) |
| 1198 | { |
| 1199 | return !test_with_subtests || in_fixture || in_subtest; |
| 1200 | } |
| 1201 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1202 | static bool run_under_gdb(void) |
| 1203 | { |
| 1204 | char buf[1024]; |
| 1205 | |
| 1206 | sprintf(buf, "/proc/%d/exe", getppid()); |
| 1207 | return (readlink (buf, buf, sizeof (buf)) != -1 && |
| 1208 | strncmp(basename(buf), "gdb", 3) == 0); |
| 1209 | } |
| 1210 | |
Thomas Wood | 277ca2b | 2015-03-13 17:02:12 +0000 | [diff] [blame] | 1211 | #ifdef HAVE_LIBUNWIND |
| 1212 | #define UNW_LOCAL_ONLY |
| 1213 | #include <libunwind.h> |
| 1214 | |
| 1215 | static void print_backtrace(void) |
| 1216 | { |
| 1217 | unw_cursor_t cursor; |
| 1218 | unw_context_t uc; |
| 1219 | int stack_num = 0; |
| 1220 | |
Daniel Vetter | 80cc540 | 2017-08-23 15:30:44 +0200 | [diff] [blame] | 1221 | igt_info("Stack trace:\n"); |
Thomas Wood | 277ca2b | 2015-03-13 17:02:12 +0000 | [diff] [blame] | 1222 | |
| 1223 | unw_getcontext(&uc); |
| 1224 | unw_init_local(&cursor, &uc); |
| 1225 | while (unw_step(&cursor) > 0) { |
| 1226 | char name[255]; |
| 1227 | unw_word_t off; |
| 1228 | |
| 1229 | if (unw_get_proc_name(&cursor, name, 255, &off) < 0) |
| 1230 | strcpy(name, "<unknown>"); |
| 1231 | |
Daniel Vetter | 80cc540 | 2017-08-23 15:30:44 +0200 | [diff] [blame] | 1232 | igt_info(" #%d [%s+0x%x]\n", stack_num++, name, |
| 1233 | (unsigned int) off); |
Thomas Wood | 277ca2b | 2015-03-13 17:02:12 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1236 | |
| 1237 | static const char hex[] = "0123456789abcdef"; |
| 1238 | |
| 1239 | static void |
| 1240 | xputch(int c) |
| 1241 | { |
| 1242 | write(STDERR_FILENO, (const void *) &c, 1); |
| 1243 | } |
| 1244 | |
| 1245 | static int |
| 1246 | xpow(int base, int pow) |
| 1247 | { |
| 1248 | int i, r = 1; |
| 1249 | |
| 1250 | for (i = 0; i < pow; i++) |
| 1251 | r *= base; |
| 1252 | |
| 1253 | return r; |
| 1254 | } |
| 1255 | |
| 1256 | static void |
| 1257 | printnum(unsigned long long num, unsigned base) |
| 1258 | { |
| 1259 | int i = 0; |
Marius Vlad | 75cba29 | 2016-09-19 18:38:56 +0300 | [diff] [blame] | 1260 | unsigned long long __num = num; |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1261 | |
| 1262 | /* determine from where we should start dividing */ |
| 1263 | do { |
| 1264 | __num /= base; |
| 1265 | i++; |
| 1266 | } while (__num); |
| 1267 | |
| 1268 | while (i--) |
| 1269 | xputch(hex[num / xpow(base, i) % base]); |
| 1270 | } |
| 1271 | |
| 1272 | static size_t |
| 1273 | xstrlcpy(char *dst, const char *src, size_t size) |
| 1274 | { |
| 1275 | char *dst_in; |
| 1276 | |
| 1277 | dst_in = dst; |
| 1278 | if (size > 0) { |
| 1279 | while (--size > 0 && *src != '\0') |
| 1280 | *dst++ = *src++; |
| 1281 | *dst = '\0'; |
| 1282 | } |
| 1283 | |
| 1284 | return dst - dst_in; |
| 1285 | } |
| 1286 | |
| 1287 | static void |
| 1288 | xprintfmt(const char *fmt, va_list ap) |
| 1289 | { |
| 1290 | const char *p; |
| 1291 | int ch, base; |
| 1292 | unsigned long long num; |
| 1293 | |
| 1294 | while (1) { |
Chris Wilson | dc3fcd9 | 2016-11-30 19:37:37 +0000 | [diff] [blame] | 1295 | while ((ch = *(const unsigned char *) fmt++) != '%') { |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1296 | if (ch == '\0') { |
| 1297 | return; |
| 1298 | } |
| 1299 | xputch(ch); |
| 1300 | } |
| 1301 | |
Chris Wilson | dc3fcd9 | 2016-11-30 19:37:37 +0000 | [diff] [blame] | 1302 | ch = *(const unsigned char *) fmt++; |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1303 | switch (ch) { |
| 1304 | /* character */ |
| 1305 | case 'c': |
| 1306 | xputch(va_arg(ap, int)); |
| 1307 | break; |
| 1308 | /* string */ |
| 1309 | case 's': |
| 1310 | if ((p = va_arg(ap, char *)) == NULL) { |
| 1311 | p = "(null)"; |
| 1312 | } |
| 1313 | |
| 1314 | for (; (ch = *p++) != '\0';) { |
| 1315 | if (ch < ' ' || ch > '~') { |
| 1316 | xputch('?'); |
| 1317 | } else { |
| 1318 | xputch(ch); |
| 1319 | } |
| 1320 | } |
| 1321 | break; |
| 1322 | /* (signed) decimal */ |
| 1323 | case 'd': |
| 1324 | num = va_arg(ap, int); |
| 1325 | if ((long long) num < 0) { |
| 1326 | xputch('-'); |
| 1327 | num = -(long long) num; |
| 1328 | } |
| 1329 | base = 10; |
| 1330 | goto number; |
| 1331 | /* unsigned decimal */ |
| 1332 | case 'u': |
| 1333 | num = va_arg(ap, unsigned int); |
| 1334 | base = 10; |
| 1335 | goto number; |
| 1336 | /* (unsigned) hexadecimal */ |
| 1337 | case 'x': |
| 1338 | num = va_arg(ap, unsigned int); |
| 1339 | base = 16; |
| 1340 | number: |
| 1341 | printnum(num, base); |
| 1342 | break; |
| 1343 | |
| 1344 | /* The following are not implemented */ |
| 1345 | |
| 1346 | /* width field */ |
| 1347 | case '1': case '2': |
| 1348 | case '3': case '4': |
| 1349 | case '5': case '6': |
| 1350 | case '7': case '8': |
| 1351 | case '9': |
| 1352 | case '.': case '#': |
| 1353 | /* long */ |
| 1354 | case 'l': |
| 1355 | /* octal */ |
| 1356 | case 'o': |
| 1357 | /* pointer */ |
| 1358 | case 'p': |
| 1359 | /* float */ |
| 1360 | case 'f': |
| 1361 | abort(); |
| 1362 | /* escaped '%' character */ |
| 1363 | case '%': |
| 1364 | xputch(ch); |
| 1365 | break; |
| 1366 | /* unrecognized escape sequence - just print it literally */ |
| 1367 | default: |
| 1368 | xputch('%'); |
| 1369 | for (fmt--; fmt[-1] != '%'; fmt--) |
| 1370 | ; /* do nothing */ |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /* async-safe printf */ |
| 1377 | static void |
| 1378 | xprintf(const char *fmt, ...) |
| 1379 | { |
| 1380 | va_list ap; |
| 1381 | |
| 1382 | va_start(ap, fmt); |
| 1383 | xprintfmt(fmt, ap); |
| 1384 | va_end(ap); |
| 1385 | } |
| 1386 | |
| 1387 | static void print_backtrace_sig_safe(void) |
| 1388 | { |
| 1389 | unw_cursor_t cursor; |
| 1390 | unw_context_t uc; |
| 1391 | int stack_num = 0; |
| 1392 | |
| 1393 | write(STDERR_FILENO, "Stack trace: \n", 15); |
| 1394 | |
| 1395 | unw_getcontext(&uc); |
| 1396 | unw_init_local(&cursor, &uc); |
| 1397 | while (unw_step(&cursor) > 0) { |
| 1398 | char name[255]; |
| 1399 | unw_word_t off; |
| 1400 | |
| 1401 | if (unw_get_proc_name(&cursor, name, 255, &off) < 0) |
Marius Vlad | 75cba29 | 2016-09-19 18:38:56 +0300 | [diff] [blame] | 1402 | xstrlcpy(name, "<unknown>", 10); |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1403 | |
| 1404 | xprintf(" #%d [%s+0x%x]\n", stack_num++, name, |
| 1405 | (unsigned int) off); |
| 1406 | |
| 1407 | } |
| 1408 | } |
Thomas Wood | 277ca2b | 2015-03-13 17:02:12 +0000 | [diff] [blame] | 1409 | #endif |
| 1410 | |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 1411 | void __igt_fail_assert(const char *domain, const char *file, const int line, |
| 1412 | const char *func, const char *assertion, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1413 | const char *f, ...) |
| 1414 | { |
| 1415 | va_list args; |
| 1416 | int err = errno; |
Damien Lespiau | e16d4a2 | 2014-07-09 11:40:16 +0100 | [diff] [blame] | 1417 | |
Thomas Wood | 0167619 | 2014-12-17 11:37:31 +0000 | [diff] [blame] | 1418 | igt_log(domain, IGT_LOG_CRITICAL, |
| 1419 | "Test assertion failure function %s, file %s:%i:\n", func, file, |
| 1420 | line); |
| 1421 | igt_log(domain, IGT_LOG_CRITICAL, "Failed assertion: %s\n", assertion); |
Damien Lespiau | e16d4a2 | 2014-07-09 11:40:16 +0100 | [diff] [blame] | 1422 | if (err) |
Thomas Wood | 0167619 | 2014-12-17 11:37:31 +0000 | [diff] [blame] | 1423 | igt_log(domain, IGT_LOG_CRITICAL, "Last errno: %i, %s\n", err, |
| 1424 | strerror(err)); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1425 | |
| 1426 | if (f) { |
| 1427 | va_start(args, f); |
Thomas Wood | 0167619 | 2014-12-17 11:37:31 +0000 | [diff] [blame] | 1428 | igt_vlog(domain, IGT_LOG_CRITICAL, f, args); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1429 | va_end(args); |
| 1430 | } |
| 1431 | |
Thomas Wood | 277ca2b | 2015-03-13 17:02:12 +0000 | [diff] [blame] | 1432 | #ifdef HAVE_LIBUNWIND |
| 1433 | print_backtrace(); |
| 1434 | #endif |
| 1435 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1436 | if (run_under_gdb()) |
| 1437 | abort(); |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 1438 | igt_fail(IGT_EXIT_FAILURE); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1441 | /** |
| 1442 | * igt_exit: |
| 1443 | * |
| 1444 | * exit() for both types (simple and with subtests) of i-g-t tests. |
| 1445 | * |
| 1446 | * This will exit the test with the right exit code when subtests have been |
| 1447 | * skipped. For normal tests it exits with a successful exit code, presuming |
| 1448 | * everything has worked out. For subtests it also checks that at least one |
| 1449 | * subtest has been run (save when only listing subtests. |
| 1450 | * |
Tim Gore | f33fa71 | 2014-09-29 13:34:30 +0100 | [diff] [blame] | 1451 | * It is an error to normally exit a test calling igt_exit() - without it the |
| 1452 | * result reporting will be wrong. To avoid such issues it is highly recommended |
| 1453 | * to use #igt_main or #igt_simple_main instead of a hand-rolled main() function. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1454 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1455 | void igt_exit(void) |
| 1456 | { |
| 1457 | igt_exit_called = true; |
| 1458 | |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 1459 | #ifdef HAVE_GLIB |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 1460 | if (igt_key_file) |
| 1461 | g_key_file_free(igt_key_file); |
Lyude | de99e15 | 2017-07-19 13:46:01 -0400 | [diff] [blame] | 1462 | #endif |
Paul Kocialkowski | ebd6eb6 | 2017-07-04 16:33:19 +0300 | [diff] [blame] | 1463 | |
Thomas Wood | e057b86 | 2014-07-18 14:10:27 +0100 | [diff] [blame] | 1464 | if (run_single_subtest && !run_single_subtest_found) { |
| 1465 | igt_warn("Unknown subtest: %s\n", run_single_subtest); |
Thomas Wood | 784344e | 2014-07-23 16:29:39 +0100 | [diff] [blame] | 1466 | exit(IGT_EXIT_INVALID); |
Thomas Wood | e057b86 | 2014-07-18 14:10:27 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1469 | if (igt_only_list_subtests()) |
Thomas Wood | 17eb062 | 2014-05-13 15:22:52 +0100 | [diff] [blame] | 1470 | exit(IGT_EXIT_SUCCESS); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1471 | |
Chris Wilson | f201495 | 2016-05-03 11:04:00 +0100 | [diff] [blame] | 1472 | /* Calling this without calling one of the above is a failure */ |
| 1473 | assert(!test_with_subtests || |
| 1474 | skipped_one || |
| 1475 | succeeded_one || |
| 1476 | failed_one); |
| 1477 | |
| 1478 | if (test_with_subtests && !failed_one) { |
| 1479 | if (succeeded_one) |
| 1480 | igt_exitcode = IGT_EXIT_SUCCESS; |
| 1481 | else |
| 1482 | igt_exitcode = IGT_EXIT_SKIP; |
| 1483 | } |
| 1484 | |
Chris Wilson | 362da34 | 2016-06-24 08:30:09 +0100 | [diff] [blame] | 1485 | if (command_str) |
Jani Nikula | 230df2d | 2016-10-14 12:52:00 +0300 | [diff] [blame] | 1486 | kmsg(KERN_INFO "[IGT] %s: exiting, ret=%d\n", |
Chris Wilson | 362da34 | 2016-06-24 08:30:09 +0100 | [diff] [blame] | 1487 | command_str, igt_exitcode); |
Thomas Wood | 9942a40 | 2015-01-27 14:10:15 +0000 | [diff] [blame] | 1488 | igt_debug("Exiting with status code %d\n", igt_exitcode); |
Chris Wilson | d8bf7e1 | 2015-01-05 14:07:31 +0000 | [diff] [blame] | 1489 | |
Chris Wilson | 7548763 | 2016-02-26 22:11:10 +0000 | [diff] [blame] | 1490 | for (int c = 0; c < num_test_children; c++) |
| 1491 | kill(test_children[c], SIGKILL); |
| 1492 | |
Thomas Wood | 16cfa37 | 2015-01-27 14:12:27 +0000 | [diff] [blame] | 1493 | if (!test_with_subtests) { |
| 1494 | struct timespec now; |
Thomas Wood | 16cfa37 | 2015-01-27 14:12:27 +0000 | [diff] [blame] | 1495 | const char *result; |
| 1496 | |
| 1497 | gettime(&now); |
Thomas Wood | 16cfa37 | 2015-01-27 14:12:27 +0000 | [diff] [blame] | 1498 | |
| 1499 | switch (igt_exitcode) { |
| 1500 | case IGT_EXIT_SUCCESS: |
| 1501 | result = "SUCCESS"; |
| 1502 | break; |
| 1503 | case IGT_EXIT_TIMEOUT: |
| 1504 | result = "TIMEOUT"; |
| 1505 | break; |
| 1506 | case IGT_EXIT_SKIP: |
| 1507 | result = "SKIP"; |
| 1508 | break; |
| 1509 | default: |
| 1510 | result = "FAIL"; |
| 1511 | } |
| 1512 | |
Chris Wilson | f201495 | 2016-05-03 11:04:00 +0100 | [diff] [blame] | 1513 | printf("%s (%.3fs)\n", |
| 1514 | result, time_elapsed(&subtest_time, &now)); |
Thomas Wood | 16cfa37 | 2015-01-27 14:12:27 +0000 | [diff] [blame] | 1515 | } |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1516 | |
Chris Wilson | f201495 | 2016-05-03 11:04:00 +0100 | [diff] [blame] | 1517 | exit(igt_exitcode); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* fork support code */ |
| 1521 | static int helper_process_count; |
| 1522 | static pid_t helper_process_pids[] = |
| 1523 | { -1, -1, -1, -1}; |
| 1524 | |
| 1525 | static void reset_helper_process_list(void) |
| 1526 | { |
| 1527 | for (int i = 0; i < ARRAY_SIZE(helper_process_pids); i++) |
| 1528 | helper_process_pids[i] = -1; |
| 1529 | helper_process_count = 0; |
| 1530 | } |
| 1531 | |
Chris Wilson | 66e4663 | 2014-07-24 12:00:57 +0100 | [diff] [blame] | 1532 | static int __waitpid(pid_t pid) |
| 1533 | { |
| 1534 | int status = -1; |
| 1535 | while (waitpid(pid, &status, 0) == -1 && |
| 1536 | errno == EINTR) |
| 1537 | ; |
| 1538 | |
| 1539 | return status; |
| 1540 | } |
| 1541 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1542 | static void fork_helper_exit_handler(int sig) |
| 1543 | { |
Chris Wilson | bd3cf81 | 2014-07-24 11:41:59 +0100 | [diff] [blame] | 1544 | /* Inside a signal handler, play safe */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1545 | for (int i = 0; i < ARRAY_SIZE(helper_process_pids); i++) { |
| 1546 | pid_t pid = helper_process_pids[i]; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1547 | if (pid != -1) { |
Chris Wilson | bd3cf81 | 2014-07-24 11:41:59 +0100 | [diff] [blame] | 1548 | kill(pid, SIGTERM); |
Chris Wilson | 66e4663 | 2014-07-24 12:00:57 +0100 | [diff] [blame] | 1549 | __waitpid(pid); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1550 | helper_process_count--; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | assert(helper_process_count == 0); |
| 1555 | } |
| 1556 | |
| 1557 | bool __igt_fork_helper(struct igt_helper_process *proc) |
| 1558 | { |
| 1559 | pid_t pid; |
| 1560 | int id; |
Paulo Zanoni | a16ebcc | 2014-09-03 14:47:21 -0300 | [diff] [blame] | 1561 | int tmp_count; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1562 | |
| 1563 | assert(!proc->running); |
| 1564 | assert(helper_process_count < ARRAY_SIZE(helper_process_pids)); |
| 1565 | |
| 1566 | for (id = 0; helper_process_pids[id] != -1; id++) |
| 1567 | ; |
| 1568 | |
| 1569 | igt_install_exit_handler(fork_helper_exit_handler); |
| 1570 | |
Paulo Zanoni | a16ebcc | 2014-09-03 14:47:21 -0300 | [diff] [blame] | 1571 | /* |
| 1572 | * Avoid races when the parent stops the child before the setup code |
| 1573 | * had a chance to run. This happens e.g. when skipping tests wrapped in |
| 1574 | * the signal helper. |
| 1575 | */ |
| 1576 | tmp_count = exit_handler_count; |
| 1577 | exit_handler_count = 0; |
Thomas Wood | 756764a | 2014-09-26 15:16:21 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* ensure any buffers are flushed before fork */ |
| 1580 | fflush(NULL); |
| 1581 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1582 | switch (pid = fork()) { |
| 1583 | case -1: |
Paulo Zanoni | a16ebcc | 2014-09-03 14:47:21 -0300 | [diff] [blame] | 1584 | exit_handler_count = tmp_count; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1585 | igt_assert(0); |
| 1586 | case 0: |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1587 | reset_helper_process_list(); |
| 1588 | oom_adjust_for_doom(); |
| 1589 | |
| 1590 | return true; |
| 1591 | default: |
Paulo Zanoni | a16ebcc | 2014-09-03 14:47:21 -0300 | [diff] [blame] | 1592 | exit_handler_count = tmp_count; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1593 | proc->running = true; |
| 1594 | proc->pid = pid; |
| 1595 | proc->id = id; |
| 1596 | helper_process_pids[id] = pid; |
| 1597 | helper_process_count++; |
| 1598 | |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
| 1602 | } |
| 1603 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1604 | /** |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1605 | * igt_wait_helper: |
| 1606 | * @proc: #igt_helper_process structure |
| 1607 | * |
| 1608 | * Joins a helper process. It is an error to call this on a helper process which |
| 1609 | * hasn't been spawned yet. |
| 1610 | */ |
| 1611 | int igt_wait_helper(struct igt_helper_process *proc) |
| 1612 | { |
Chris Wilson | 66e4663 | 2014-07-24 12:00:57 +0100 | [diff] [blame] | 1613 | int status; |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1614 | |
| 1615 | assert(proc->running); |
| 1616 | |
Chris Wilson | 66e4663 | 2014-07-24 12:00:57 +0100 | [diff] [blame] | 1617 | status = __waitpid(proc->pid); |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1618 | |
| 1619 | proc->running = false; |
| 1620 | |
| 1621 | helper_process_pids[proc->id] = -1; |
| 1622 | helper_process_count--; |
| 1623 | |
| 1624 | return status; |
| 1625 | } |
| 1626 | |
Chris Wilson | 2b804c3 | 2016-03-29 17:19:31 +0100 | [diff] [blame] | 1627 | static bool helper_was_alive(struct igt_helper_process *proc, |
| 1628 | int status) |
| 1629 | { |
| 1630 | return (WIFSIGNALED(status) && |
| 1631 | WTERMSIG(status) == (proc->use_SIGKILL ? SIGKILL : SIGTERM)); |
| 1632 | } |
| 1633 | |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1634 | /** |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1635 | * igt_stop_helper: |
| 1636 | * @proc: #igt_helper_process structure |
| 1637 | * |
| 1638 | * Terminates a helper process. It is an error to call this on a helper process |
| 1639 | * which hasn't been spawned yet. |
| 1640 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1641 | void igt_stop_helper(struct igt_helper_process *proc) |
| 1642 | { |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1643 | int status; |
| 1644 | |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1645 | /* failure here means the pid is already dead and so waiting is safe */ |
| 1646 | kill(proc->pid, proc->use_SIGKILL ? SIGKILL : SIGTERM); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1647 | |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 1648 | status = igt_wait_helper(proc); |
Chris Wilson | 2b804c3 | 2016-03-29 17:19:31 +0100 | [diff] [blame] | 1649 | if (!helper_was_alive(proc, status)) |
| 1650 | igt_debug("Helper died too early with status=%d\n", status); |
| 1651 | assert(helper_was_alive(proc, status)); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static void children_exit_handler(int sig) |
| 1655 | { |
Chris Wilson | bd3cf81 | 2014-07-24 11:41:59 +0100 | [diff] [blame] | 1656 | int status; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1657 | |
Chris Wilson | bd3cf81 | 2014-07-24 11:41:59 +0100 | [diff] [blame] | 1658 | /* The exit handler can be called from a fatal signal, so play safe */ |
| 1659 | while (num_test_children-- && wait(&status)) |
| 1660 | ; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | bool __igt_fork(void) |
| 1664 | { |
| 1665 | assert(!test_with_subtests || in_subtest); |
| 1666 | assert(!test_child); |
| 1667 | |
| 1668 | igt_install_exit_handler(children_exit_handler); |
| 1669 | |
| 1670 | if (num_test_children >= test_children_sz) { |
| 1671 | if (!test_children_sz) |
| 1672 | test_children_sz = 4; |
| 1673 | else |
| 1674 | test_children_sz *= 2; |
| 1675 | |
| 1676 | test_children = realloc(test_children, |
| 1677 | sizeof(pid_t)*test_children_sz); |
| 1678 | igt_assert(test_children); |
| 1679 | } |
| 1680 | |
Thomas Wood | 756764a | 2014-09-26 15:16:21 +0100 | [diff] [blame] | 1681 | /* ensure any buffers are flushed before fork */ |
| 1682 | fflush(NULL); |
| 1683 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1684 | switch (test_children[num_test_children++] = fork()) { |
| 1685 | case -1: |
| 1686 | igt_assert(0); |
| 1687 | case 0: |
| 1688 | test_child = true; |
| 1689 | exit_handler_count = 0; |
| 1690 | reset_helper_process_list(); |
| 1691 | oom_adjust_for_doom(); |
| 1692 | |
| 1693 | return true; |
| 1694 | default: |
| 1695 | return false; |
| 1696 | } |
| 1697 | |
| 1698 | } |
| 1699 | |
| 1700 | /** |
Tvrtko Ursulin | 054eb1a | 2017-03-30 14:32:29 +0100 | [diff] [blame] | 1701 | * igt_child_done: |
| 1702 | * |
| 1703 | * Lets the IGT core know that one of the children has exited. |
| 1704 | */ |
| 1705 | void igt_child_done(pid_t pid) |
| 1706 | { |
| 1707 | int i = 0; |
| 1708 | int found = -1; |
| 1709 | |
| 1710 | igt_assert(num_test_children > 1); |
| 1711 | |
| 1712 | for (i = 0; i < num_test_children; i++) { |
| 1713 | if (pid == test_children[i]) { |
| 1714 | found = i; |
| 1715 | break; |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | igt_assert(found >= 0); |
| 1720 | |
| 1721 | num_test_children--; |
| 1722 | for (i = found; i < num_test_children; i++) |
| 1723 | test_children[i] = test_children[i + 1]; |
| 1724 | } |
| 1725 | |
| 1726 | /** |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1727 | * igt_waitchildren: |
| 1728 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1729 | * Wait for all children forked with igt_fork. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1730 | * |
| 1731 | * The magic here is that exit codes from children will be correctly propagated |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 1732 | * to the main thread, including the relevant exit code if a child thread failed. |
| 1733 | * Of course if multiple children failed with different exit codes the resulting |
| 1734 | * exit code will be non-deterministic. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1735 | * |
| 1736 | * Note that igt_skip() will not be forwarded, feature tests need to be done |
| 1737 | * before spawning threads with igt_fork(). |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1738 | */ |
| 1739 | void igt_waitchildren(void) |
| 1740 | { |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1741 | int err = 0; |
| 1742 | int count; |
| 1743 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1744 | assert(!test_child); |
| 1745 | |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1746 | count = 0; |
| 1747 | while (count < num_test_children) { |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1748 | int status = -1; |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1749 | pid_t pid; |
| 1750 | int c; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1751 | |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1752 | pid = wait(&status); |
| 1753 | if (pid == -1) |
| 1754 | continue; |
| 1755 | |
| 1756 | for (c = 0; c < num_test_children; c++) |
| 1757 | if (pid == test_children[c]) |
| 1758 | break; |
| 1759 | if (c == num_test_children) |
| 1760 | continue; |
| 1761 | |
| 1762 | if (err == 0 && status != 0) { |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1763 | if (WIFEXITED(status)) { |
| 1764 | printf("child %i failed with exit status %i\n", |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1765 | c, WEXITSTATUS(status)); |
| 1766 | err = WEXITSTATUS(status); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1767 | } else if (WIFSIGNALED(status)) { |
| 1768 | printf("child %i died with signal %i, %s\n", |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1769 | c, WTERMSIG(status), |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1770 | strsignal(WTERMSIG(status))); |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1771 | err = 128 + WTERMSIG(status); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1772 | } else { |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1773 | printf("Unhandled failure [%d] in child %i\n", status, c); |
| 1774 | err = 256; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1775 | } |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1776 | |
| 1777 | for (c = 0; c < num_test_children; c++) |
| 1778 | kill(test_children[c], SIGKILL); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1779 | } |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1780 | |
| 1781 | count++; |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | num_test_children = 0; |
Chris Wilson | f47ee31 | 2014-07-11 10:15:19 +0100 | [diff] [blame] | 1785 | if (err) |
| 1786 | igt_fail(err); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1787 | } |
| 1788 | |
Chris Wilson | 7548763 | 2016-02-26 22:11:10 +0000 | [diff] [blame] | 1789 | /** |
| 1790 | * igt_waitchildren_timeout: |
Daniel Vetter | 483d1c0 | 2016-03-19 12:12:47 +0100 | [diff] [blame] | 1791 | * @seconds: timeout in seconds to wait |
Daniel Vetter | 2869921 | 2016-03-19 15:14:19 +0100 | [diff] [blame] | 1792 | * @reason: debug string explaining what timedout |
Chris Wilson | 7548763 | 2016-02-26 22:11:10 +0000 | [diff] [blame] | 1793 | * |
| 1794 | * Wait for all children forked with igt_fork, for a maximum of @seconds. |
| 1795 | * |
| 1796 | * Wraps igt_waitchildren() and igt_set_timeout() |
| 1797 | */ |
| 1798 | void igt_waitchildren_timeout(int seconds, const char *reason) |
| 1799 | { |
| 1800 | igt_set_timeout(seconds, reason); |
| 1801 | igt_waitchildren(); |
| 1802 | igt_reset_timeout(); |
| 1803 | } |
| 1804 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1805 | /* exit handler code */ |
| 1806 | #define MAX_SIGNALS 32 |
Thomas Wood | 8a3853c | 2014-07-17 11:19:04 +0100 | [diff] [blame] | 1807 | #define MAX_EXIT_HANDLERS 10 |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1808 | |
Alan Coopersmith | 95ea5d5 | 2014-12-23 19:07:12 -0800 | [diff] [blame] | 1809 | #ifndef HAVE_SIGHANDLER_T |
| 1810 | typedef void (*sighandler_t)(int); |
| 1811 | #endif |
| 1812 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1813 | static struct { |
| 1814 | sighandler_t handler; |
| 1815 | bool installed; |
| 1816 | } orig_sig[MAX_SIGNALS]; |
| 1817 | |
| 1818 | static igt_exit_handler_t exit_handler_fn[MAX_EXIT_HANDLERS]; |
| 1819 | static bool exit_handler_disabled; |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1820 | #define SIGDEF(x) { x, #x, sizeof(#x) - 1 } |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1821 | #define SILENT(x) { x, NULL, 0 } |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1822 | static const struct { int number; const char *name; size_t name_len; } handled_signals[] = |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1823 | { SILENT(SIGINT), SILENT(SIGHUP), SILENT(SIGTERM), SILENT(SIGQUIT), |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1824 | SILENT(SIGPIPE), SIGDEF(SIGABRT), SIGDEF(SIGSEGV), SIGDEF(SIGBUS), |
| 1825 | SIGDEF(SIGFPE) }; |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1826 | #undef SILENT |
| 1827 | #undef SIGDEF |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1828 | |
| 1829 | static int install_sig_handler(int sig_num, sighandler_t handler) |
| 1830 | { |
| 1831 | orig_sig[sig_num].handler = signal(sig_num, handler); |
| 1832 | |
| 1833 | if (orig_sig[sig_num].handler == SIG_ERR) |
| 1834 | return -1; |
| 1835 | |
| 1836 | orig_sig[sig_num].installed = true; |
| 1837 | |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | static void restore_sig_handler(int sig_num) |
| 1842 | { |
| 1843 | /* Just restore the default so that we properly fall over. */ |
| 1844 | signal(sig_num, SIG_DFL); |
| 1845 | } |
| 1846 | |
| 1847 | static void restore_all_sig_handler(void) |
| 1848 | { |
| 1849 | int i; |
| 1850 | |
| 1851 | for (i = 0; i < ARRAY_SIZE(orig_sig); i++) |
| 1852 | restore_sig_handler(i); |
| 1853 | } |
| 1854 | |
| 1855 | static void call_exit_handlers(int sig) |
| 1856 | { |
| 1857 | int i; |
| 1858 | |
| 1859 | if (!exit_handler_count) { |
| 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | for (i = exit_handler_count - 1; i >= 0; i--) |
| 1864 | exit_handler_fn[i](sig); |
| 1865 | |
| 1866 | /* ensure we don't get called twice */ |
| 1867 | exit_handler_count = 0; |
| 1868 | } |
| 1869 | |
| 1870 | static void igt_atexit_handler(void) |
| 1871 | { |
Thomas Wood | e65c69b | 2015-07-16 17:39:59 +0100 | [diff] [blame] | 1872 | in_atexit_handler = true; |
| 1873 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1874 | restore_all_sig_handler(); |
| 1875 | |
| 1876 | if (!exit_handler_disabled) |
| 1877 | call_exit_handlers(0); |
| 1878 | } |
| 1879 | |
Chris Wilson | 9b0a32d | 2015-05-10 09:09:54 +0100 | [diff] [blame] | 1880 | static bool crash_signal(int sig) |
| 1881 | { |
| 1882 | switch (sig) { |
| 1883 | case SIGILL: |
| 1884 | case SIGBUS: |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1885 | case SIGFPE: |
Chris Wilson | 9b0a32d | 2015-05-10 09:09:54 +0100 | [diff] [blame] | 1886 | case SIGSEGV: |
| 1887 | return true; |
| 1888 | default: |
| 1889 | return false; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1893 | static void fatal_sig_handler(int sig) |
| 1894 | { |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1895 | int i; |
| 1896 | |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1897 | for (i = 0; i < ARRAY_SIZE(handled_signals); i++) { |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1898 | if (handled_signals[i].number != sig) |
| 1899 | continue; |
| 1900 | |
| 1901 | if (handled_signals[i].name_len) { |
Thomas Wood | 47f6b13 | 2015-03-25 16:42:57 +0000 | [diff] [blame] | 1902 | igt_assert_eq(write(STDERR_FILENO, "Received signal ", 16), |
| 1903 | 16); |
| 1904 | igt_assert_eq(write(STDERR_FILENO, handled_signals[i].name, handled_signals[i].name_len), |
| 1905 | handled_signals[i].name_len); |
| 1906 | igt_assert_eq(write(STDERR_FILENO, ".\n", 2), 2); |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1907 | } |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1908 | |
Marius Vlad | 75cba29 | 2016-09-19 18:38:56 +0300 | [diff] [blame] | 1909 | if (crash_signal(sig)) { |
Derek Morton | f497238 | 2015-05-18 14:37:31 +0100 | [diff] [blame] | 1910 | /* Linux standard to return exit code as 128 + signal */ |
| 1911 | if (!failed_one) |
| 1912 | igt_exitcode = 128 + sig; |
| 1913 | |
| 1914 | failed_one = true; |
Marius Vlad | 8da2b54 | 2016-09-02 14:34:46 +0300 | [diff] [blame] | 1915 | #ifdef HAVE_LIBUNWIND |
marius vlad | 1ae41a5 | 2016-08-24 11:44:07 +0300 | [diff] [blame] | 1916 | print_backtrace_sig_safe(); |
Marius Vlad | 8da2b54 | 2016-09-02 14:34:46 +0300 | [diff] [blame] | 1917 | #endif |
Marius Vlad | 75cba29 | 2016-09-19 18:38:56 +0300 | [diff] [blame] | 1918 | if (in_subtest) |
| 1919 | exit_subtest("CRASH"); |
Derek Morton | f497238 | 2015-05-18 14:37:31 +0100 | [diff] [blame] | 1920 | } |
Chris Wilson | 2fa35c7 | 2015-03-19 08:29:51 +0000 | [diff] [blame] | 1921 | break; |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 1924 | restore_all_sig_handler(); |
| 1925 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1926 | /* |
| 1927 | * exit_handler_disabled is always false here, since when we set it |
| 1928 | * we also block signals. |
| 1929 | */ |
| 1930 | call_exit_handlers(sig); |
| 1931 | |
Alan Coopersmith | 99e6def | 2014-12-23 19:07:10 -0800 | [diff] [blame] | 1932 | { |
| 1933 | #ifdef __linux__ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1934 | /* Workaround cached PID and TID races on glibc and Bionic libc. */ |
Alan Coopersmith | 99e6def | 2014-12-23 19:07:10 -0800 | [diff] [blame] | 1935 | pid_t pid = syscall(SYS_getpid); |
| 1936 | pid_t tid = syscall(SYS_gettid); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1937 | |
Alan Coopersmith | 99e6def | 2014-12-23 19:07:10 -0800 | [diff] [blame] | 1938 | syscall(SYS_tgkill, pid, tid, sig); |
| 1939 | #else |
| 1940 | pthread_t tid = pthread_self(); |
| 1941 | union sigval value = { .sival_ptr = NULL }; |
| 1942 | |
| 1943 | pthread_sigqueue(tid, sig, value); |
| 1944 | #endif |
| 1945 | } |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1948 | /** |
| 1949 | * igt_install_exit_handler: |
| 1950 | * @fn: exit handler function |
| 1951 | * |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1952 | * Set a handler that will be called either when the process calls exit() or |
Thomas Wood | 52a3a2e | 2014-06-09 17:23:21 +0100 | [diff] [blame] | 1953 | * <!-- -->returns from the main function, or one of the signals in |
| 1954 | * 'handled_signals' is raised. MAX_EXIT_HANDLERS handlers can be installed, |
| 1955 | * each of which will be called only once, even if a subsequent signal is |
| 1956 | * raised. If the exit handlers are called due to a signal, the signal will be |
| 1957 | * re-raised with the original signal disposition after all handlers returned. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1958 | * |
| 1959 | * The handler will be passed the signal number if called due to a signal, or |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1960 | * 0 otherwise. Exit handlers can also be used from test children spawned with |
| 1961 | * igt_fork(), but not from within helper processes spawned with |
Daniel Vetter | 3ea97f2 | 2014-03-16 18:56:14 +0100 | [diff] [blame] | 1962 | * igt_fork_helper(). The list of exit handlers is reset when forking to |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1963 | * avoid issues with children cleanup up the parent's state too early. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1964 | */ |
| 1965 | void igt_install_exit_handler(igt_exit_handler_t fn) |
| 1966 | { |
| 1967 | int i; |
| 1968 | |
| 1969 | for (i = 0; i < exit_handler_count; i++) |
| 1970 | if (exit_handler_fn[i] == fn) |
| 1971 | return; |
| 1972 | |
| 1973 | igt_assert(exit_handler_count < MAX_EXIT_HANDLERS); |
| 1974 | |
| 1975 | exit_handler_fn[exit_handler_count] = fn; |
| 1976 | exit_handler_count++; |
| 1977 | |
| 1978 | if (exit_handler_count > 1) |
| 1979 | return; |
| 1980 | |
| 1981 | for (i = 0; i < ARRAY_SIZE(handled_signals); i++) { |
Thomas Wood | 4ff4ea6 | 2015-02-19 13:58:06 +0000 | [diff] [blame] | 1982 | if (install_sig_handler(handled_signals[i].number, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1983 | fatal_sig_handler)) |
| 1984 | goto err; |
| 1985 | } |
| 1986 | |
| 1987 | if (atexit(igt_atexit_handler)) |
| 1988 | goto err; |
| 1989 | |
| 1990 | return; |
| 1991 | err: |
| 1992 | restore_all_sig_handler(); |
| 1993 | exit_handler_count--; |
| 1994 | |
| 1995 | igt_assert_f(0, "failed to install the signal handler\n"); |
| 1996 | } |
| 1997 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1998 | /* simulation enviroment support */ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 1999 | |
| 2000 | /** |
| 2001 | * igt_run_in_simulation: |
| 2002 | * |
| 2003 | * This function can be used to select a reduced test set when running in |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 2004 | * simulation environments. This i-g-t mode is selected by setting the |
| 2005 | * INTEL_SIMULATION environment variable to 1. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2006 | * |
| 2007 | * Returns: True when run in simulation mode, false otherwise. |
| 2008 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2009 | bool igt_run_in_simulation(void) |
| 2010 | { |
| 2011 | static int simulation = -1; |
| 2012 | |
| 2013 | if (simulation == -1) |
Daniel Vetter | 8221fda | 2014-03-23 14:38:17 +0100 | [diff] [blame] | 2014 | simulation = igt_check_boolean_env_var("INTEL_SIMULATION", false); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2015 | |
| 2016 | return simulation; |
| 2017 | } |
| 2018 | |
| 2019 | /** |
| 2020 | * igt_skip_on_simulation: |
| 2021 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2022 | * Skip tests when INTEL_SIMULATION environment variable is set. It uses |
| 2023 | * igt_skip() internally and hence is fully subtest aware. |
Daniel Vetter | a388f3b | 2014-03-22 13:22:11 +0100 | [diff] [blame] | 2024 | * |
| 2025 | * Note that in contrast to all other functions which use igt_skip() internally |
| 2026 | * it is allowed to use this outside of an #igt_fixture block in a test with |
| 2027 | * subtests. This is because in contrast to most other test requirements, |
| 2028 | * checking for simulation mode doesn't depend upon the present hardware and it |
| 2029 | * so makes a lot of sense to have this check in the outermost #igt_main block. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2030 | */ |
| 2031 | void igt_skip_on_simulation(void) |
| 2032 | { |
| 2033 | if (igt_only_list_subtests()) |
| 2034 | return; |
| 2035 | |
Daniel Vetter | 2459b80 | 2017-08-14 11:32:04 +0200 | [diff] [blame] | 2036 | if (!igt_can_fail()) { |
Daniel Vetter | a388f3b | 2014-03-22 13:22:11 +0100 | [diff] [blame] | 2037 | igt_fixture |
| 2038 | igt_require(!igt_run_in_simulation()); |
| 2039 | } else |
| 2040 | igt_require(!igt_run_in_simulation()); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /* structured logging */ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2044 | |
| 2045 | /** |
| 2046 | * igt_log: |
Thomas Wood | f59bbf7 | 2014-12-12 14:18:07 +0000 | [diff] [blame] | 2047 | * @domain: the log domain, or NULL for no domain |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2048 | * @level: #igt_log_level |
| 2049 | * @format: format string |
| 2050 | * @...: optional arguments used in the format string |
| 2051 | * |
Daniel Vetter | 126b8ba | 2014-05-13 12:28:26 +0200 | [diff] [blame] | 2052 | * This is the generic structured logging helper function. i-g-t testcase should |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2053 | * output all normal message to stdout. Warning level message should be printed |
| 2054 | * to stderr and the test runner should treat this as an intermediate result |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 2055 | * between SUCCESS and FAILURE. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2056 | * |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 2057 | * The log level can be set through the IGT_LOG_LEVEL environment variable with |
Thomas Wood | df11a0f | 2014-12-16 15:18:20 +0000 | [diff] [blame] | 2058 | * values "debug", "info", "warn", "critical" and "none". By default verbose |
| 2059 | * debug message are disabled. "none" completely disables all output and is not |
| 2060 | * recommended since crucial issues only reported at the IGT_LOG_WARN level are |
| 2061 | * ignored. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 2062 | */ |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2063 | void igt_log(const char *domain, enum igt_log_level level, const char *format, ...) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2064 | { |
| 2065 | va_list args; |
| 2066 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2067 | va_start(args, format); |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2068 | igt_vlog(domain, level, format, args); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 2069 | va_end(args); |
| 2070 | } |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2071 | |
| 2072 | /** |
| 2073 | * igt_vlog: |
Thomas Wood | f59bbf7 | 2014-12-12 14:18:07 +0000 | [diff] [blame] | 2074 | * @domain: the log domain, or NULL for no domain |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2075 | * @level: #igt_log_level |
| 2076 | * @format: format string |
| 2077 | * @args: variable arguments lists |
| 2078 | * |
| 2079 | * This is the generic logging helper function using an explicit varargs |
| 2080 | * structure and hence useful to implement domain-specific logging |
| 2081 | * functions. |
| 2082 | * |
| 2083 | * If there is no need to wrap up a vararg list in the caller it is simpler to |
| 2084 | * just use igt_log(). |
| 2085 | */ |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2086 | void igt_vlog(const char *domain, enum igt_log_level level, const char *format, va_list args) |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2087 | { |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2088 | FILE *file; |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2089 | char *line, *formatted_line; |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2090 | const char *program_name; |
| 2091 | const char *igt_log_level_str[] = { |
| 2092 | "DEBUG", |
| 2093 | "INFO", |
| 2094 | "WARNING", |
Thomas Wood | df11a0f | 2014-12-16 15:18:20 +0000 | [diff] [blame] | 2095 | "CRITICAL", |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2096 | "NONE" |
| 2097 | }; |
Daniel Vetter | f45698d | 2015-02-27 19:32:33 +0100 | [diff] [blame] | 2098 | static bool line_continuation = false; |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2099 | |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2100 | assert(format); |
| 2101 | |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2102 | #ifdef __GLIBC__ |
| 2103 | program_name = program_invocation_short_name; |
| 2104 | #else |
| 2105 | program_name = command_str; |
| 2106 | #endif |
| 2107 | |
Thomas Wood | 6173790 | 2015-01-26 16:09:40 +0000 | [diff] [blame] | 2108 | if (list_subtests && level <= IGT_LOG_WARN) |
Daniel Vetter | bebcb58 | 2014-07-11 10:24:30 +0200 | [diff] [blame] | 2109 | return; |
| 2110 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2111 | if (vasprintf(&line, format, args) == -1) |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2112 | return; |
| 2113 | |
Daniel Vetter | f45698d | 2015-02-27 19:32:33 +0100 | [diff] [blame] | 2114 | if (line_continuation) { |
| 2115 | formatted_line = strdup(line); |
| 2116 | if (!formatted_line) |
| 2117 | goto out; |
| 2118 | } else if (asprintf(&formatted_line, "(%s:%d) %s%s%s: %s", program_name, |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2119 | getpid(), (domain) ? domain : "", (domain) ? "-" : "", |
| 2120 | igt_log_level_str[level], line) == -1) { |
| 2121 | goto out; |
| 2122 | } |
| 2123 | |
Derek Morton | 8528b48 | 2015-12-23 15:34:48 +0000 | [diff] [blame] | 2124 | line_continuation = line[strlen(line) - 1] != '\n'; |
Daniel Vetter | f45698d | 2015-02-27 19:32:33 +0100 | [diff] [blame] | 2125 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2126 | /* append log buffer */ |
| 2127 | _igt_log_buffer_append(formatted_line); |
| 2128 | |
| 2129 | /* check print log level */ |
| 2130 | if (igt_log_level > level) |
| 2131 | goto out; |
| 2132 | |
| 2133 | /* check domain filter */ |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 2134 | if (igt_log_domain_filter) { |
| 2135 | /* if null domain and filter is not "application", return */ |
| 2136 | if (!domain && strcmp(igt_log_domain_filter, "application")) |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2137 | goto out; |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 2138 | /* else if domain and filter do not match, return */ |
| 2139 | else if (domain && strcmp(igt_log_domain_filter, domain)) |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2140 | goto out; |
Thomas Wood | 5fe9c88 | 2014-12-02 11:37:26 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2143 | /* use stderr for warning messages and above */ |
Thomas Wood | a228223 | 2015-01-26 16:09:09 +0000 | [diff] [blame] | 2144 | if (level >= IGT_LOG_WARN) { |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2145 | file = stderr; |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2146 | fflush(stdout); |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 2147 | } |
| 2148 | else |
| 2149 | file = stdout; |
| 2150 | |
Thomas Wood | a5f2172 | 2015-01-19 15:27:51 +0000 | [diff] [blame] | 2151 | /* prepend all except information messages with process, domain and log |
| 2152 | * level information */ |
| 2153 | if (level != IGT_LOG_INFO) |
| 2154 | fwrite(formatted_line, sizeof(char), strlen(formatted_line), |
| 2155 | file); |
| 2156 | else |
| 2157 | fwrite(line, sizeof(char), strlen(line), file); |
| 2158 | |
| 2159 | out: |
| 2160 | free(line); |
Daniel Vetter | bd53d72 | 2014-03-13 17:27:47 +0100 | [diff] [blame] | 2161 | } |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2162 | |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 2163 | static const char *timeout_op; |
Thomas Wood | 36b8143 | 2015-11-02 10:02:08 +0000 | [diff] [blame] | 2164 | static void __attribute__((noreturn)) igt_alarm_handler(int signal) |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2165 | { |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 2166 | if (timeout_op) |
| 2167 | igt_info("Timed out: %s\n", timeout_op); |
| 2168 | else |
| 2169 | igt_info("Timed out\n"); |
Thomas Wood | cff102e | 2015-04-09 09:27:54 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* exit with failure status */ |
| 2172 | igt_fail(IGT_EXIT_FAILURE); |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * igt_set_timeout: |
| 2177 | * @seconds: number of seconds before timeout |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 2178 | * @op: Optional string to explain what operation has timed out in the debug log |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2179 | * |
Thomas Wood | cff102e | 2015-04-09 09:27:54 +0100 | [diff] [blame] | 2180 | * Fail a test and exit with #IGT_EXIT_FAILURE status after the specified |
Thomas Wood | ebab9f0 | 2014-11-13 11:47:19 +0000 | [diff] [blame] | 2181 | * number of seconds have elapsed. If the current test has subtests and the |
| 2182 | * timeout occurs outside a subtest, subsequent subtests will be skipped and |
| 2183 | * marked as failed. |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2184 | * |
Thomas Wood | ebab9f0 | 2014-11-13 11:47:19 +0000 | [diff] [blame] | 2185 | * Any previous timer is cancelled and no timeout is scheduled if @seconds is |
Daniel Vetter | 5b0a843 | 2015-08-07 19:12:07 +0200 | [diff] [blame] | 2186 | * zero. But for clarity the timeout set with this function should be cleared |
| 2187 | * with igt_reset_timeout(). |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2188 | */ |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 2189 | void igt_set_timeout(unsigned int seconds, |
| 2190 | const char *op) |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2191 | { |
| 2192 | struct sigaction sa; |
| 2193 | |
| 2194 | sa.sa_handler = igt_alarm_handler; |
| 2195 | sigemptyset(&sa.sa_mask); |
| 2196 | sa.sa_flags = 0; |
| 2197 | |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 2198 | timeout_op = op; |
| 2199 | |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 2200 | if (seconds == 0) |
| 2201 | sigaction(SIGALRM, NULL, NULL); |
| 2202 | else |
| 2203 | sigaction(SIGALRM, &sa, NULL); |
| 2204 | |
| 2205 | alarm(seconds); |
| 2206 | } |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 2207 | |
Daniel Vetter | 5b0a843 | 2015-08-07 19:12:07 +0200 | [diff] [blame] | 2208 | /** |
Thomas Wood | f0381d1 | 2015-09-07 09:26:01 +0100 | [diff] [blame] | 2209 | * igt_reset_timeout: |
Daniel Vetter | 5b0a843 | 2015-08-07 19:12:07 +0200 | [diff] [blame] | 2210 | * |
| 2211 | * This function resets a timeout set by igt_set_timeout() and disables any |
| 2212 | * timer set up by the former function. |
| 2213 | */ |
| 2214 | void igt_reset_timeout(void) |
| 2215 | { |
| 2216 | igt_set_timeout(0, NULL); |
| 2217 | } |
| 2218 | |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 2219 | FILE *__igt_fopen_data(const char* igt_srcdir, const char* igt_datadir, |
| 2220 | const char* filename) |
| 2221 | { |
| 2222 | char path[PATH_MAX]; |
| 2223 | FILE *fp; |
| 2224 | |
| 2225 | snprintf(path, sizeof(path), "%s/%s", igt_datadir, filename); |
| 2226 | fp = fopen(path, "r"); |
| 2227 | if (!fp) { |
| 2228 | snprintf(path, sizeof(path), "%s/%s", igt_srcdir, filename); |
| 2229 | fp = fopen(path, "r"); |
| 2230 | } |
Matt Roper | 303b380 | 2016-05-26 16:02:05 -0700 | [diff] [blame] | 2231 | if (!fp) { |
| 2232 | snprintf(path, sizeof(path), "./%s", filename); |
| 2233 | fp = fopen(path, "r"); |
| 2234 | } |
Thomas Wood | 90dadcd | 2015-04-13 12:09:48 +0100 | [diff] [blame] | 2235 | |
| 2236 | if (!fp) |
| 2237 | igt_critical("Could not open data file \"%s\": %s", filename, |
| 2238 | strerror(errno)); |
| 2239 | |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 2240 | return fp; |
| 2241 | } |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2242 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2243 | static void log_output(int *fd, enum igt_log_level level) |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2244 | { |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2245 | ssize_t len; |
| 2246 | char buf[PIPE_BUF]; |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2247 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2248 | if (*fd < 0) |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2249 | return; |
| 2250 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2251 | memset(buf, 0, sizeof(buf)); |
| 2252 | len = read(*fd, buf, sizeof(buf)); |
| 2253 | if (len <= 0) { |
| 2254 | close(*fd); |
| 2255 | *fd = -1; |
| 2256 | return; |
| 2257 | } |
| 2258 | |
| 2259 | igt_log(IGT_LOG_DOMAIN, level, "[cmd] %s", buf); |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | /** |
| 2263 | * igt_system: |
| 2264 | * |
| 2265 | * An improved replacement of the system() call. |
| 2266 | * |
| 2267 | * Executes the shell command specified in @command with the added feature of |
| 2268 | * concurrently capturing its stdout and stderr to igt_log and igt_warn |
| 2269 | * respectively. |
| 2270 | * |
| 2271 | * Returns: The exit status of the executed process. -1 for failure. |
| 2272 | */ |
| 2273 | int igt_system(const char *command) |
| 2274 | { |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2275 | int outpipe[2] = { -1, -1 }; |
| 2276 | int errpipe[2] = { -1, -1 }; |
| 2277 | int status; |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2278 | struct igt_helper_process process = {}; |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2279 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2280 | if (pipe(outpipe) < 0) |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2281 | goto err; |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2282 | if (pipe(errpipe) < 0) |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2283 | goto err; |
| 2284 | |
Imre Deak | 40d6f19 | 2017-10-13 14:30:08 +0300 | [diff] [blame] | 2285 | /* |
| 2286 | * The clone() system call called from a largish executable has |
| 2287 | * difficulty to make progress if interrupted too frequently, so |
| 2288 | * suspend the signal helper for the time of the syscall. |
| 2289 | */ |
| 2290 | igt_suspend_signal_helper(); |
| 2291 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2292 | igt_fork_helper(&process) { |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2293 | close(outpipe[0]); |
| 2294 | close(errpipe[0]); |
| 2295 | |
| 2296 | if (dup2(outpipe[1], STDOUT_FILENO) < 0) |
| 2297 | goto child_err; |
| 2298 | if (dup2(errpipe[1], STDERR_FILENO) < 0) |
| 2299 | goto child_err; |
| 2300 | |
| 2301 | execl("/bin/sh", "sh", "-c", command, |
| 2302 | (char *) NULL); |
| 2303 | |
| 2304 | child_err: |
| 2305 | exit(EXIT_FAILURE); |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2306 | } |
| 2307 | |
Imre Deak | 40d6f19 | 2017-10-13 14:30:08 +0300 | [diff] [blame] | 2308 | igt_resume_signal_helper(); |
| 2309 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2310 | close(outpipe[1]); |
| 2311 | close(errpipe[1]); |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2312 | |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2313 | while (outpipe[0] >= 0 || errpipe[0] >= 0) { |
| 2314 | log_output(&outpipe[0], IGT_LOG_INFO); |
| 2315 | log_output(&errpipe[0], IGT_LOG_WARN); |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2316 | } |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2317 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2318 | status = igt_wait_helper(&process); |
| 2319 | |
| 2320 | return WEXITSTATUS(status); |
| 2321 | err: |
Petri Latvala | f16a799 | 2017-09-21 15:52:28 +0300 | [diff] [blame] | 2322 | close(outpipe[0]); |
| 2323 | close(outpipe[1]); |
| 2324 | close(errpipe[0]); |
| 2325 | close(errpipe[1]); |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2326 | return -1; |
| 2327 | } |
| 2328 | |
| 2329 | /** |
| 2330 | * igt_system_quiet: |
| 2331 | * Similar to igt_system(), except redirect output to /dev/null |
| 2332 | * |
| 2333 | * Returns: The exit status of the executed process. -1 for failure. |
| 2334 | */ |
| 2335 | int igt_system_quiet(const char *command) |
| 2336 | { |
| 2337 | int stderr_fd_copy, stdout_fd_copy, status, nullfd; |
| 2338 | |
| 2339 | /* redirect */ |
| 2340 | if ((nullfd = open("/dev/null", O_WRONLY)) == -1) |
| 2341 | goto err; |
| 2342 | if ((stdout_fd_copy = dup(STDOUT_FILENO)) == -1) |
| 2343 | goto err; |
| 2344 | if ((stderr_fd_copy = dup(STDERR_FILENO)) == -1) |
| 2345 | goto err; |
| 2346 | |
| 2347 | if (dup2(nullfd, STDOUT_FILENO) == -1) |
| 2348 | goto err; |
| 2349 | if (dup2(nullfd, STDERR_FILENO) == -1) |
| 2350 | goto err; |
| 2351 | |
Imre Deak | 40d6f19 | 2017-10-13 14:30:08 +0300 | [diff] [blame] | 2352 | /* See igt_system() for the reason for suspending the signal helper. */ |
| 2353 | igt_suspend_signal_helper(); |
| 2354 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2355 | if ((status = system(command)) == -1) |
| 2356 | goto err; |
| 2357 | |
Imre Deak | 40d6f19 | 2017-10-13 14:30:08 +0300 | [diff] [blame] | 2358 | igt_resume_signal_helper(); |
| 2359 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2360 | /* restore */ |
| 2361 | if (dup2(stdout_fd_copy, STDOUT_FILENO) == -1) |
| 2362 | goto err; |
| 2363 | if (dup2(stderr_fd_copy, STDERR_FILENO) == -1) |
| 2364 | goto err; |
| 2365 | |
| 2366 | close(stdout_fd_copy); |
| 2367 | close(stderr_fd_copy); |
| 2368 | close(nullfd); |
| 2369 | |
| 2370 | return WEXITSTATUS(status); |
| 2371 | err: |
Imre Deak | 40d6f19 | 2017-10-13 14:30:08 +0300 | [diff] [blame] | 2372 | igt_resume_signal_helper(); |
| 2373 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 2374 | close(stderr_fd_copy); |
| 2375 | close(stdout_fd_copy); |
| 2376 | close(nullfd); |
| 2377 | |
| 2378 | return -1; |
| 2379 | } |