1 1.1 mrg #! /bin/bash 2 1.1 mrg 3 1.1 mrg #set -x 4 1.1 mrg 5 1.1 mrg ######################################################################## 6 1.1 mrg # 7 1.1 mrg # File: reg-hunt 8 1.1 mrg # Author: Janis Johnson <janis187 (at] us.ibm.com> 9 1.1 mrg # Date: 2003/08/19 10 1.1 mrg # 11 1.1 mrg # Search for the patch identifier for which results for a test changed, 12 1.1 mrg # using a binary search. The functionality for getting sources, 13 1.1 mrg # building the component to test, and running the test are in other 14 1.1 mrg # scripts that are run from here. Before the search begins, we verify 15 1.1 mrg # that we get the expected behavior for the first and last patch 16 1.1 mrg # identifiers. 17 1.1 mrg # 18 1.1 mrg # Define these in a file whose name is the argument to this script: 19 1.1 mrg # LOW_PATCH: Patch identifier. 20 1.1 mrg # HIGH_PATCH: Patch identifier. 21 1.1 mrg # REG_UPDATE: Pathname of script to update your source tree; returns 22 1.1 mrg # zero for success, nonzero for failure. 23 1.1 mrg # REG_BUILD: Pathname of script to build enough of the product to run 24 1.1 mrg # the test; returns zero for success, nonzero for failure. 25 1.1 mrg # REG_TEST: Pathname of script to run the test; returns 1 if we 26 1.1 mrg # should search later patches, 0 if we should search 27 1.1 mrg # earlier patches, and something else if there was an 28 1.1 mrg # unexpected failure. 29 1.1 mrg # Optional: 30 1.1 mrg # REG_REPORT Pathname of script to call at the end with the id of the 31 1.1 mrg # patch that caused the change in behavior. 32 1.1 mrg # REG_FINISH Pathname of script to call at the end with the two final 33 1.1 mrg # patch identifiers as arguments. 34 1.1 mrg # REG_NEWMID Pathname of script to call when a build has failed, with 35 1.1 mrg # arguments of the failed id and the current low and high 36 1.1 mrg # SKIP_LOW If 1, skip verifying the low patch identifier of the 37 1.1 mrg # range; define this only if you're restarting and have 38 1.1 mrg # already tested the low patch. 39 1.1 mrg # SKIP_HIGH If 1, skip verifying the high patch identifier of the 40 1.1 mrg # range; define this only if you're restarting and have 41 1.1 mrg # already tested the high patch. 42 1.1 mrg # FIRST_MID Use this as the first midpoint, to avoid a midpoint that 43 1.1 mrg # is known not to build. 44 1.1 mrg # VERBOSITY Default is 0, to print only errors and final message. 45 1.1 mrg # DATE_IN_MSG If set to anything but 0, include the time and date in 46 1.1 mrg # messages. 47 1.1 mrg # 48 1.1 mrg # 49 1.1 mrg # 50 1.1 mrg # Copyright (c) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. 51 1.1 mrg # 52 1.1 mrg # This file is free software; you can redistribute it and/or modify 53 1.1 mrg # it under the terms of the GNU General Public License as published by 54 1.1 mrg # the Free Software Foundation; either version 3 of the License, or 55 1.1 mrg # (at your option) any later version. 56 1.1 mrg # 57 1.1 mrg # This program is distributed in the hope that it will be useful, 58 1.1 mrg # but WITHOUT ANY WARRANTY; without even the implied warranty of 59 1.1 mrg # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 60 1.1 mrg # GNU General Public License for more details. 61 1.1 mrg # 62 1.1 mrg # For a copy of the GNU General Public License, write the the 63 1.1 mrg # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 64 1.1 mrg # Boston, MA 02111-1301, USA. 65 1.1 mrg # 66 1.1 mrg ######################################################################## 67 1.1 mrg 68 1.1 mrg ######################################################################## 69 1.1 mrg # Functions 70 1.1 mrg ######################################################################## 71 1.1 mrg 72 1.1 mrg # Issue a message if its verbosity level is high enough. 73 1.1 mrg 74 1.1 mrg msg() { 75 1.1 mrg test ${1} -gt ${VERBOSITY} && return 76 1.1 mrg 77 1.1 mrg if [ "x${DATE_IN_MSG}" = "x" ]; then 78 1.1 mrg echo "${2}" 79 1.1 mrg else 80 1.1 mrg echo "`date` ${2}" 81 1.1 mrg fi 82 1.1 mrg } 83 1.1 mrg 84 1.1 mrg # Issue an error message and exit with a non-zero status. If there 85 1.1 mrg # is a valid current range whose end points have been tested, report 86 1.1 mrg # it so the user can start again from there. 87 1.1 mrg 88 1.1 mrg error() { 89 1.1 mrg msg 0 "error: ${1}" 90 1.1 mrg test ${VALID_RANGE} -eq 1 && \ 91 1.1 mrg echo "current range:" 92 1.1 mrg echo "LOW_PATCH=${LATER_THAN}" 93 1.1 mrg echo "HIGH_PATCH=${EARLIER_THAN}" 94 1.1 mrg exit 1 95 1.1 mrg } 96 1.1 mrg 97 1.1 mrg # Build the components to test using sources as of a particular patch 98 1.1 mrg # and run a test case. Pass each of the scripts the patch identifier 99 1.1 mrg # that we're testing; the first one needs it, the others can ignore it 100 1.1 mrg # if they want. 101 1.1 mrg 102 1.1 mrg process_patch () { 103 1.1 mrg TEST_ID=${1} 104 1.1 mrg 105 1.1 mrg # If we're keeping track of known failures, see if TEST_ID is one and 106 1.1 mrg # if so, don't bother updating sources and trying to build. 107 1.1 mrg 108 1.1 mrg FAILS=0 109 1.1 mrg SKIP=0 110 1.1 mrg if [ ${SKIP_FAILURES} -eq 1 ]; then 111 1.1 mrg ${REG_CHECKFAIL} ${TEST_ID} 112 1.1 mrg if [ $? -eq 0 ]; then 113 1.1 mrg msg 1 "skipping ${TEST_ID}; it is a known build failure" 114 1.1 mrg FAILS=1 115 1.1 mrg SKIP=1 116 1.1 mrg fi 117 1.1 mrg fi 118 1.1 mrg 119 1.1 mrg if [ ${FAILS} -eq 0 ]; then 120 1.1 mrg ${REG_UPDATE} ${TEST_ID} || error "source update failed for ${TEST_ID}" 121 1.1 mrg ${REG_BUILD} ${TEST_ID} 122 1.1 mrg if [ $? -ne 0 ]; then 123 1.1 mrg FAILS=1 124 1.1 mrg msg 1 "build failed for ${TEST_ID}" 125 1.1 mrg if [ ${SKIP_FAILURES} -eq 1 ]; then 126 1.1 mrg ${REG_RECORDFAIL} ${TEST_ID} 127 1.1 mrg fi 128 1.1 mrg fi 129 1.1 mrg fi 130 1.1 mrg 131 1.1 mrg if [ ${FAILS} -eq 0 ]; then 132 1.1 mrg ${REG_TEST} ${TEST_ID} 133 1.1 mrg LATER=$? 134 1.1 mrg if [ $LATER -ne 0 -a $LATER -ne 1 ]; then 135 1.1 mrg msg 0 "unexpected test failure for ${TEST_ID}" 136 1.1 mrg exit 1 137 1.1 mrg fi 138 1.1 mrg else 139 1.1 mrg 140 1.1 mrg # The build failed, or this patch is already known to fail to build. 141 1.1 mrg # If it's an endpoint, or if we don't have a way to recover from 142 1.1 mrg # build failures, quit now. 143 1.1 mrg 144 1.1 mrg if [ ${SKIP} -eq 0 ]; then 145 1.1 mrg if [ "x${REG_NEWMID}" == "x" \ 146 1.1 mrg -o ${TEST_ID} -eq ${LATER_THAN} \ 147 1.1 mrg -o ${TEST_ID} -eq ${EARLIER_THAN} ]; then 148 1.1 mrg error "build failed for ${TEST_ID}" 149 1.1 mrg fi 150 1.1 mrg fi 151 1.1 mrg 152 1.1 mrg # Try to find a new patch to try within the current range. 153 1.1 mrg 154 1.1 mrg FIRST_MID=`${REG_NEWMID} ${LATER_THAN} ${EARLIER_THAN}` 155 1.1 mrg if [ ${FIRST_MID} -eq 0 ]; then 156 1.1 mrg 157 1.1 mrg # The heuristics in the tool ran out of patches to try next; 158 1.1 mrg # let the user handle it from here.+ 159 1.1 mrg error "build failed for ${TEST_ID}, could not find new candidate" 160 1.1 mrg fi 161 1.1 mrg msg 1 "using ${FIRST_MID}, between ${LATER_THAN} and ${EARLIER_THAN}" 162 1.1 mrg fi 163 1.1 mrg 164 1.1 mrg # Return with a valid LATER value or a new ID to try in FIRST_MID. 165 1.1 mrg } 166 1.1 mrg 167 1.1 mrg # Get the number of a patch within the range. It's not actually the 168 1.1 mrg # middle one, but the one that might minimize the number of checks. 169 1.1 mrg 170 1.1 mrg get_mid_special() { 171 1.1 mrg LOW=$1 172 1.1 mrg HIGH=$2 173 1.1 mrg 174 1.1 mrg let DIFF=HIGH-LOW 175 1.1 mrg M=1 176 1.1 mrg POWER2=1 177 1.1 mrg while 178 1.1 mrg [ $POWER2 -lt $DIFF ] 179 1.1 mrg do 180 1.1 mrg let M=POWER2 181 1.1 mrg let POWER2=POWER2*2 182 1.1 mrg done 183 1.1 mrg let MID=LOW+M 184 1.1 mrg } 185 1.1 mrg 186 1.1 mrg # Get the number of the patch in the middle of the range. 187 1.1 mrg 188 1.1 mrg get_mid () { 189 1.1 mrg LOW=$1 190 1.1 mrg HIGH=$2 191 1.1 mrg 192 1.1 mrg let DIFF=HIGH-LOW 193 1.1 mrg let M=DIFF/2 194 1.1 mrg let MID=LOW+M 195 1.1 mrg } 196 1.1 mrg 197 1.1 mrg # Perform a binary search on patch identifiers within the range 198 1.1 mrg # specified by the arguments. 199 1.1 mrg 200 1.1 mrg search_patches () { 201 1.1 mrg LOW=$1 202 1.1 mrg HIGH=$2 203 1.1 mrg 204 1.1 mrg # Get an identifier within the range. The user can override the 205 1.1 mrg # initial mid patch if it is known to have problems, e.g., if a 206 1.1 mrg # build fails for that patch. 207 1.1 mrg 208 1.1 mrg if [ ${FIRST_MID} -ne 0 ]; then 209 1.1 mrg MID=${FIRST_MID} 210 1.1 mrg FIRST_MID=0 211 1.1 mrg let DIFF=HIGH-LOW 212 1.1 mrg else 213 1.1 mrg get_mid $LOW $HIGH 214 1.1 mrg fi 215 1.1 mrg 216 1.1 mrg while [ ${DIFF} -gt 1 ]; do 217 1.1 mrg TEST_ID="${MID}" 218 1.1 mrg 219 1.1 mrg # Test it. 220 1.1 mrg 221 1.1 mrg process_patch ${TEST_ID} 222 1.1 mrg 223 1.1 mrg # FIRST_MID being set is a signal that the build failed and we 224 1.1 mrg # should start over again. 225 1.1 mrg 226 1.1 mrg test ${FIRST_MID} -ne 0 && return 227 1.1 mrg 228 1.1 mrg # Narrow the search based on the outcome of testing TEST_ID. 229 1.1 mrg 230 1.1 mrg if [ ${LATER} -eq 1 ]; then 231 1.1 mrg msg 1 "search patches later than ${TEST_ID}" 232 1.1 mrg LATER_THAN=${TEST_ID} 233 1.1 mrg let LOW=MID 234 1.1 mrg else 235 1.1 mrg msg 1 "search patches earlier than ${TEST_ID}" 236 1.1 mrg EARLIER_THAN=${TEST_ID} 237 1.1 mrg let HIGH=MID 238 1.1 mrg fi 239 1.1 mrg 240 1.1 mrg get_mid $LOW $HIGH 241 1.1 mrg done 242 1.1 mrg } 243 1.1 mrg 244 1.1 mrg ######################################################################## 245 1.1 mrg # Main program (so to speak) 246 1.1 mrg ######################################################################## 247 1.1 mrg 248 1.1 mrg # The error function uses this. 249 1.1 mrg 250 1.1 mrg VALID_RANGE=0 251 1.1 mrg 252 1.1 mrg # Process the configuration file. 253 1.1 mrg 254 1.1 mrg if [ $# != 1 ]; then 255 1.1 mrg echo Usage: $0 config_file 256 1.1 mrg exit 1 257 1.1 mrg fi 258 1.1 mrg 259 1.1 mrg CONFIG=${1} 260 1.1 mrg if [ ! -f ${CONFIG} ]; then 261 1.1 mrg error "configuration file ${CONFIG} does not exist" 262 1.1 mrg fi 263 1.1 mrg 264 1.1 mrg # OK, the config file exists. Source it, make sure required parameters 265 1.1 mrg # are defined and their files exist, and give default values to optional 266 1.1 mrg # parameters. 267 1.1 mrg 268 1.1 mrg . ${CONFIG} 269 1.1 mrg 270 1.1 mrg test "x${REG_UPDATE}" = "x" && error "REG_UPDATE is not defined" 271 1.1 mrg test "x${REG_BUILD}" = "x" && error "REG_BUILD is not defined" 272 1.1 mrg test "x${REG_TEST}" = "x" && error "REG_TEST is not defined" 273 1.1 mrg test -x ${REG_TEST} || error "REG_TEST is not an executable file" 274 1.1 mrg test "x${SKIP_LOW}" = "x" && SKIP_LOW=0 275 1.1 mrg test "x${SKIP_HIGH}" = "x" && SKIP_HIGH=0 276 1.1 mrg test "x${VERBOSITY}" = "x" && VERBOSITY=0 277 1.1 mrg test "x${REG_FINISH}" = "x" && REG_FINISH=true 278 1.1 mrg test "x${REG_REPORT}" = "x" && REG_REPORT=true 279 1.1 mrg 280 1.1 mrg msg 2 "LOW_PATCH = ${LOW_PATCH}" 281 1.1 mrg msg 2 "HIGH_PATCH = ${HIGH_PATCH}" 282 1.1 mrg msg 2 "REG_UPDATE = ${REG_UPDATE}" 283 1.1 mrg msg 2 "REG_BUILD = ${REG_BUILD}" 284 1.1 mrg msg 2 "REG_TEST = ${REG_TEST}" 285 1.1 mrg msg 2 "REG_NEWMID = ${REG_NEWMID}" 286 1.1 mrg msg 2 "SKIP_LOW = ${SKIP_LOW}" 287 1.1 mrg msg 2 "SKIP_HIGH = ${SKIP_HIGH}" 288 1.1 mrg msg 2 "FIRST_MID = ${FIRST_MID}" 289 1.1 mrg msg 2 "VERBOSITY = ${VERBOSITY}" 290 1.1 mrg 291 1.1 mrg # If REG_NEWMID was defined, assume that we're skipping known failures 292 1.1 mrg # and adding to the list for new failures. If the list of failures 293 1.1 mrg # doesn't exist, create it. We use a different flag, SKIP_FAILURES, 294 1.1 mrg # to make it easier to separate the flag from REG_NEWMID if we want 295 1.1 mrg # to change the usage later. 296 1.1 mrg 297 1.1 mrg if [ "x${REG_NEWMID}" != "x" ]; then 298 1.1 mrg touch ${REG_FAILLIST} 299 1.1 mrg SKIP_FAILURES=1 300 1.1 mrg else 301 1.1 mrg SKIP_FAILURES=0 302 1.1 mrg fi 303 1.1 mrg 304 1.1 mrg # If FIRST_MID was defined, make sure it's in the range. 305 1.1 mrg 306 1.1 mrg if [ "x${FIRST_MID}" != "x" ]; then 307 1.1 mrg test ${FIRST_MID} -le ${LOW_PATCH} && \ 308 1.1 mrg error "FIRST_MID id is lower than LOW_PATCH" 309 1.1 mrg test ${FIRST_MID} -ge ${HIGH_PATCH} && \ 310 1.1 mrg error "FIRST_MID is higher than HIGH_PATCH" 311 1.1 mrg else 312 1.1 mrg FIRST_MID=0 313 1.1 mrg fi 314 1.1 mrg 315 1.1 mrg # Keep track of the bounds of the range where the test behavior changes. 316 1.1 mrg 317 1.1 mrg LATER_THAN=${LOW_PATCH} 318 1.1 mrg EARLIER_THAN=${HIGH_PATCH} 319 1.1 mrg LATER=1 320 1.1 mrg 321 1.1 mrg msg 1 "LATER_THAN = ${LATER_THAN}" 322 1.1 mrg msg 1 "EARLIER_THAN = ${EARLIER_THAN}" 323 1.1 mrg 324 1.1 mrg # Verify that the range isn't backwards. 325 1.1 mrg 326 1.1 mrg test ${LOW_PATCH} -lt ${HIGH_PATCH} || \ 327 1.1 mrg error "patch identifier range is backwards" 328 1.1 mrg 329 1.1 mrg # Verify that the first and last patches in the range get the results we 330 1.1 mrg # expect. If not, quit, because any of several things could be wrong. 331 1.1 mrg 332 1.1 mrg if [ ${SKIP_HIGH} -eq 0 ]; then 333 1.1 mrg process_patch ${EARLIER_THAN} 334 1.1 mrg test ${LATER} -ne 0 && \ 335 1.1 mrg error "unexpected result for high patch ${EARLIER_THAN}" 336 1.1 mrg msg 1 "result for high patch ${EARLIER_THAN} is as expected" 337 1.1 mrg fi 338 1.1 mrg 339 1.1 mrg if [ ${SKIP_LOW} -eq 0 ]; then 340 1.1 mrg process_patch ${LATER_THAN} 341 1.1 mrg test ${LATER} -ne 1 && \ 342 1.1 mrg error "unexpected result for low patch ${LATER_THAN}" 343 1.1 mrg msg 1 "result for low patch ${LATER_THAN} is as expected" 344 1.1 mrg fi 345 1.1 mrg 346 1.1 mrg # Search within the range, now that we know that the end points are valid. 347 1.1 mrg # If the build failed then FIRST_MID is set to a new patch to try. 348 1.1 mrg 349 1.1 mrg VALID_RANGE=1 350 1.1 mrg while true; do 351 1.1 mrg search_patches ${LATER_THAN} ${EARLIER_THAN} 352 1.1 mrg test ${FIRST_MID} -eq 0 && break 353 1.1 mrg done 354 1.1 mrg 355 1.1 mrg # Report where the test behavior changes. 356 1.1 mrg 357 1.1 mrg echo "Test result changes with id ${EARLIER_THAN}" 358 1.1 mrg ${REG_REPORT} ${EARLIER_THAN} 359 1.1 mrg 360 1.1 mrg # Invoke the optional script to verify the result and report additional 361 1.1 mrg # information about changes between the two patches. 362 1.1 mrg 363 1.1 mrg ${REG_FINISH} ${LATER_THAN} ${EARLIER_THAN} 364