100ff2212SAndy Fiddaman#!@TOOLS_PYTHON@ 296ccc8cbSesaxe# 396ccc8cbSesaxe# CDDL HEADER START 496ccc8cbSesaxe# 596ccc8cbSesaxe# The contents of this file are subject to the terms of the 696ccc8cbSesaxe# Common Development and Distribution License (the "License"). 796ccc8cbSesaxe# You may not use this file except in compliance with the License. 896ccc8cbSesaxe# 996ccc8cbSesaxe# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 1096ccc8cbSesaxe# or http://www.opensolaris.org/os/licensing. 1196ccc8cbSesaxe# See the License for the specific language governing permissions 1296ccc8cbSesaxe# and limitations under the License. 1396ccc8cbSesaxe# 1496ccc8cbSesaxe# When distributing Covered Code, include this CDDL HEADER in each 1596ccc8cbSesaxe# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1696ccc8cbSesaxe# If applicable, add the following below this CDDL HEADER, with the 1796ccc8cbSesaxe# fields enclosed by brackets "[]" replaced with your own identifying 1896ccc8cbSesaxe# information: Portions Copyright [yyyy] [name of copyright owner] 1996ccc8cbSesaxe# 2096ccc8cbSesaxe# CDDL HEADER END 2196ccc8cbSesaxe# 22598cc7dfSVladimir Kotal# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 233eeb7968SAndy Fiddaman# Copyright 2019 OmniOS Community Edition (OmniOSce) Association. 2496ccc8cbSesaxe# 2596ccc8cbSesaxe 2696ccc8cbSesaxe# 2796ccc8cbSesaxe# wsdiff(1) is a tool that can be used to determine which compiled objects 2896ccc8cbSesaxe# have changed as a result of a given source change. Developers backporting 2996ccc8cbSesaxe# new features, RFEs and bug fixes need to be able to identify the set of 3096ccc8cbSesaxe# patch deliverables necessary for feature/fix realization on a patched system. 3196ccc8cbSesaxe# 3296ccc8cbSesaxe# The tool works by comparing objects in two trees/proto areas (one build with, 3396ccc8cbSesaxe# and without the source changes. 3496ccc8cbSesaxe# 3596ccc8cbSesaxe# Using wsdiff(1) is fairly simple: 3696ccc8cbSesaxe# - Bringover to a fresh workspace 3796ccc8cbSesaxe# - Perform a full non-debug build (clobber if workspace isn't fresh) 3896ccc8cbSesaxe# - Move the proto area aside, call it proto.old, or something. 3996ccc8cbSesaxe# - Integrate your changes to the workspace 4096ccc8cbSesaxe# - Perform another full non-debug clobber build. 4196ccc8cbSesaxe# - Use wsdiff(1) to see what changed: 4296ccc8cbSesaxe# $ wsdiff proto.old proto 4396ccc8cbSesaxe# 4496ccc8cbSesaxe# By default, wsdiff will print the list of changed objects / deliverables to 4596ccc8cbSesaxe# stdout. If a results file is specified via -r, the list of differing objects, 4696ccc8cbSesaxe# and details about why wsdiff(1) thinks they are different will be logged to 4796ccc8cbSesaxe# the results file. 4896ccc8cbSesaxe# 493eeb7968SAndy Fiddaman# By invoking nightly(1) with the -w option to NIGHTLY_FLAGS, nightly(1) will 503eeb7968SAndy Fiddaman# use wsdiff(1) to report on what objects changed since the last build. 5196ccc8cbSesaxe# 5296ccc8cbSesaxe# For patch deliverable purposes, it's advised to have nightly do a clobber, 5396ccc8cbSesaxe# non-debug build. 5496ccc8cbSesaxe# 5596ccc8cbSesaxe# Think about the results. Was something flagged that you don't expect? Go look 5696ccc8cbSesaxe# at the results file to see details about the differences. 5796ccc8cbSesaxe# 583eeb7968SAndy Fiddaman# Use the -i option in conjunction with -v and -V to dive deeper and have 593eeb7968SAndy Fiddaman# wsdiff(1) report with more verbosity. 6096ccc8cbSesaxe# 6196ccc8cbSesaxe# Usage: wsdiff [-vVt] [-r results ] [-i filelist ] old new 6296ccc8cbSesaxe# 6396ccc8cbSesaxe# Where "old" is the path to the proto area build without the changes, and 6496ccc8cbSesaxe# "new" is the path to the proto area built with the changes. The following 6596ccc8cbSesaxe# options are supported: 6696ccc8cbSesaxe# 6796ccc8cbSesaxe# -v Do not truncate observed diffs in results 6896ccc8cbSesaxe# -V Log *all* ELF sect diffs vs. logging the first diff found 6996ccc8cbSesaxe# -t Use onbld tools in $SRC/tools 7096ccc8cbSesaxe# -r Log results and observed differences 7196ccc8cbSesaxe# -i Tell wsdiff which objects to compare via an input file list 7296ccc8cbSesaxe 7300ff2212SAndy Fiddamanfrom __future__ import print_function 742b15649cSAndy Fiddamanimport datetime, fnmatch, getopt, os, profile, io, subprocess 75598cc7dfSVladimir Kotalimport re, resource, select, shutil, signal, string, struct, sys, tempfile 76598cc7dfSVladimir Kotalimport time, threading 7796ccc8cbSesaxefrom stat import * 783eeb7968SAndy Fiddaman 793eeb7968SAndy FiddamanPY3 = sys.version_info[0] == 3 803eeb7968SAndy Fiddaman 813eeb7968SAndy Fiddamanif not PY3: 823eeb7968SAndy Fiddaman import commands 8396ccc8cbSesaxe 8496ccc8cbSesaxe# Human readable diffs truncated by default if longer than this 8596ccc8cbSesaxe# Specifying -v on the command line will override 8696ccc8cbSesaxediffs_sz_thresh = 4096 8796ccc8cbSesaxe 88598cc7dfSVladimir Kotal# Lock name Provides exclusive access to 89598cc7dfSVladimir Kotal# --------------+------------------------------------------------ 90598cc7dfSVladimir Kotal# output_lock standard output or temporary file (difference()) 91598cc7dfSVladimir Kotal# log_lock the results file (log_difference()) 92598cc7dfSVladimir Kotal# wset_lock changedFiles list (workerThread()) 93598cc7dfSVladimir Kotaloutput_lock = threading.Lock() 94598cc7dfSVladimir Kotallog_lock = threading.Lock() 95598cc7dfSVladimir Kotalwset_lock = threading.Lock() 96598cc7dfSVladimir Kotal 97598cc7dfSVladimir Kotal# Variable for thread control 98598cc7dfSVladimir Kotalkeep_processing = True 99598cc7dfSVladimir Kotal 10096ccc8cbSesaxe# Default search path for wsdiff 10196ccc8cbSesaxewsdiff_path = [ "/usr/bin", 10296ccc8cbSesaxe "/usr/ccs/bin", 10396ccc8cbSesaxe "/lib/svc/bin", 10496ccc8cbSesaxe "/opt/onbld/bin" ] 10596ccc8cbSesaxe 10696ccc8cbSesaxe# These are objects that wsdiff will notice look different, but will not report. 10796ccc8cbSesaxe# Existence of an exceptions list, and adding things here is *dangerous*, 10896ccc8cbSesaxe# and therefore the *only* reasons why anything would be listed here is because 10996ccc8cbSesaxe# the objects do not build deterministically, yet we *cannot* fix this. 11096ccc8cbSesaxe# 11196ccc8cbSesaxe# These perl libraries use __DATE__ and therefore always look different. 11296ccc8cbSesaxe# Ideally, we would purge use the use of __DATE__ from the source, but because 11396ccc8cbSesaxe# this is source we wish to distribute with Solaris "unchanged", we cannot modify. 11496ccc8cbSesaxe# 11500ff2212SAndy Fiddamanwsdiff_exceptions = [ 11600ff2212SAndy Fiddaman "usr/perl5/5.8.4/lib/sun4-solaris-64int/CORE/libperl.so.1", 11796ccc8cbSesaxe "usr/perl5/5.6.1/lib/sun4-solaris-64int/CORE/libperl.so.1", 11896ccc8cbSesaxe "usr/perl5/5.8.4/lib/i86pc-solaris-64int/CORE/libperl.so.1", 11996ccc8cbSesaxe "usr/perl5/5.6.1/lib/i86pc-solaris-64int/CORE/libperl.so.1" 12096ccc8cbSesaxe] 12196ccc8cbSesaxe 1223eeb7968SAndy Fiddamanif PY3: 123de8c868eSAlexander Pyhalov def getoutput(cmd): 124de8c868eSAlexander Pyhalov import shlex, tempfile 125de8c868eSAlexander Pyhalov f, fpath = tempfile.mkstemp() 126de8c868eSAlexander Pyhalov status = os.system("{ " + cmd + "; } >" + 127de8c868eSAlexander Pyhalov shlex.quote(fpath) + " 2>&1") 128de8c868eSAlexander Pyhalov returncode = os.WEXITSTATUS(status) 129de8c868eSAlexander Pyhalov with os.fdopen(f, "r") as tfile: 130de8c868eSAlexander Pyhalov output = tfile.read() 131de8c868eSAlexander Pyhalov os.unlink(fpath) 132de8c868eSAlexander Pyhalov if output[-1:] == '\n': 133de8c868eSAlexander Pyhalov output = output[:-1] 134de8c868eSAlexander Pyhalov return returncode, output 1353eeb7968SAndy Fiddamanelse: 136de8c868eSAlexander Pyhalov getoutput = commands.getstatusoutput 1372b15649cSAndy Fiddaman 13896ccc8cbSesaxe##### 13996ccc8cbSesaxe# Logging routines 14096ccc8cbSesaxe# 14196ccc8cbSesaxe 142598cc7dfSVladimir Kotal# Debug message to be printed to the screen, and the log file 143598cc7dfSVladimir Kotaldef debug(msg) : 144598cc7dfSVladimir Kotal 145598cc7dfSVladimir Kotal # Add prefix to highlight debugging message 146598cc7dfSVladimir Kotal msg = "## " + msg 147598cc7dfSVladimir Kotal if debugon : 148598cc7dfSVladimir Kotal output_lock.acquire() 14900ff2212SAndy Fiddaman print(msg) 150598cc7dfSVladimir Kotal sys.stdout.flush() 151598cc7dfSVladimir Kotal output_lock.release() 152598cc7dfSVladimir Kotal if logging : 153598cc7dfSVladimir Kotal log_lock.acquire() 15400ff2212SAndy Fiddaman print(msg, file=log) 155598cc7dfSVladimir Kotal log.flush() 156598cc7dfSVladimir Kotal log_lock.release() 157598cc7dfSVladimir Kotal 15896ccc8cbSesaxe# Informational message to be printed to the screen, and the log file 15996ccc8cbSesaxedef info(msg) : 16096ccc8cbSesaxe 161598cc7dfSVladimir Kotal output_lock.acquire() 16200ff2212SAndy Fiddaman print(msg) 16396ccc8cbSesaxe sys.stdout.flush() 164598cc7dfSVladimir Kotal output_lock.release() 165598cc7dfSVladimir Kotal if logging : 166598cc7dfSVladimir Kotal log_lock.acquire() 16700ff2212SAndy Fiddaman print(msg, file=log) 168598cc7dfSVladimir Kotal log.flush() 169598cc7dfSVladimir Kotal log_lock.release() 17096ccc8cbSesaxe 17196ccc8cbSesaxe# Error message to be printed to the screen, and the log file 17296ccc8cbSesaxedef error(msg) : 17396ccc8cbSesaxe 174598cc7dfSVladimir Kotal output_lock.acquire() 17500ff2212SAndy Fiddaman print("ERROR: " + msg, file=sys.stderr) 17696ccc8cbSesaxe sys.stderr.flush() 177598cc7dfSVladimir Kotal output_lock.release() 17896ccc8cbSesaxe if logging : 179598cc7dfSVladimir Kotal log_lock.acquire() 18000ff2212SAndy Fiddaman print("ERROR: " + msg, file=log) 18196ccc8cbSesaxe log.flush() 182598cc7dfSVladimir Kotal log_lock.release() 18396ccc8cbSesaxe 18496ccc8cbSesaxe# Informational message to be printed only to the log, if there is one. 18596ccc8cbSesaxedef v_info(msg) : 18696ccc8cbSesaxe 18796ccc8cbSesaxe if logging : 188598cc7dfSVladimir Kotal log_lock.acquire() 18900ff2212SAndy Fiddaman print(msg, file=log) 19096ccc8cbSesaxe log.flush() 191598cc7dfSVladimir Kotal log_lock.release() 19296ccc8cbSesaxe 19396ccc8cbSesaxe# 19496ccc8cbSesaxe# Flag a detected file difference 19596ccc8cbSesaxe# Display the fileName to stdout, and log the difference 19696ccc8cbSesaxe# 19796ccc8cbSesaxedef difference(f, dtype, diffs) : 19896ccc8cbSesaxe 19996ccc8cbSesaxe if f in wsdiff_exceptions : 20096ccc8cbSesaxe return 20196ccc8cbSesaxe 202598cc7dfSVladimir Kotal output_lock.acquire() 203598cc7dfSVladimir Kotal if sorted : 204598cc7dfSVladimir Kotal differentFiles.append(f) 205598cc7dfSVladimir Kotal else: 20600ff2212SAndy Fiddaman print(f) 20796ccc8cbSesaxe sys.stdout.flush() 208598cc7dfSVladimir Kotal output_lock.release() 20996ccc8cbSesaxe 21096ccc8cbSesaxe log_difference(f, dtype, diffs) 21196ccc8cbSesaxe 21296ccc8cbSesaxe# 21396ccc8cbSesaxe# Do the actual logging of the difference to the results file 21496ccc8cbSesaxe# 21596ccc8cbSesaxedef log_difference(f, dtype, diffs) : 216598cc7dfSVladimir Kotal 21796ccc8cbSesaxe if logging : 218598cc7dfSVladimir Kotal log_lock.acquire() 21900ff2212SAndy Fiddaman print(f, file=log) 22000ff2212SAndy Fiddaman print("NOTE: " + dtype + " difference detected.", file=log) 22196ccc8cbSesaxe 22296ccc8cbSesaxe difflen = len(diffs) 22396ccc8cbSesaxe if difflen > 0 : 22400ff2212SAndy Fiddaman print('', file=log) 22596ccc8cbSesaxe 22696ccc8cbSesaxe if not vdiffs and difflen > diffs_sz_thresh : 22700ff2212SAndy Fiddaman print(diffs[:diffs_sz_thresh], file=log) 22800ff2212SAndy Fiddaman print("... truncated due to length: " + 22900ff2212SAndy Fiddaman "use -v to override ...", file=log) 23096ccc8cbSesaxe else : 23100ff2212SAndy Fiddaman print(diffs, file=log) 23200ff2212SAndy Fiddaman print('\n', file=log) 23396ccc8cbSesaxe log.flush() 234598cc7dfSVladimir Kotal log_lock.release() 23596ccc8cbSesaxe 23696ccc8cbSesaxe 23796ccc8cbSesaxe##### 23896ccc8cbSesaxe# diff generating routines 23996ccc8cbSesaxe# 24096ccc8cbSesaxe 24196ccc8cbSesaxe# 2423eeb7968SAndy Fiddaman# Return human readable diffs from two files 24396ccc8cbSesaxe# 24496ccc8cbSesaxedef diffFileData(tmpf1, tmpf2) : 24596ccc8cbSesaxe 246598cc7dfSVladimir Kotal binaries = False 247598cc7dfSVladimir Kotal 24896ccc8cbSesaxe # Filter the data through od(1) if the data is detected 24996ccc8cbSesaxe # as being binary 25096ccc8cbSesaxe if isBinary(tmpf1) or isBinary(tmpf2) : 251598cc7dfSVladimir Kotal binaries = True 25296ccc8cbSesaxe tmp_od1 = tmpf1 + ".od" 25396ccc8cbSesaxe tmp_od2 = tmpf2 + ".od" 25496ccc8cbSesaxe 25596ccc8cbSesaxe cmd = od_cmd + " -c -t x4" + " " + tmpf1 + " > " + tmp_od1 25696ccc8cbSesaxe os.system(cmd) 25796ccc8cbSesaxe cmd = od_cmd + " -c -t x4" + " " + tmpf2 + " > " + tmp_od2 25896ccc8cbSesaxe os.system(cmd) 25996ccc8cbSesaxe 26096ccc8cbSesaxe tmpf1 = tmp_od1 26196ccc8cbSesaxe tmpf2 = tmp_od2 26296ccc8cbSesaxe 263598cc7dfSVladimir Kotal try: 2642b15649cSAndy Fiddaman rc, data = getoutput(diff_cmd + " " + tmpf1 + " " + tmpf2) 265598cc7dfSVladimir Kotal # Remove the temp files as we no longer need them. 266598cc7dfSVladimir Kotal if binaries : 267598cc7dfSVladimir Kotal try: 268598cc7dfSVladimir Kotal os.unlink(tmp_od1) 26900ff2212SAndy Fiddaman except OSError as e: 270598cc7dfSVladimir Kotal error("diffFileData: unlink failed %s" % e) 271598cc7dfSVladimir Kotal try: 272598cc7dfSVladimir Kotal os.unlink(tmp_od2) 27300ff2212SAndy Fiddaman except OSError as e: 274598cc7dfSVladimir Kotal error("diffFileData: unlink failed %s" % e) 275598cc7dfSVladimir Kotal except: 27600ff2212SAndy Fiddaman error("failed to get output of command: " + diff_cmd + " " 277598cc7dfSVladimir Kotal + tmpf1 + " " + tmpf2) 278598cc7dfSVladimir Kotal 279598cc7dfSVladimir Kotal # Send exception for the failed command up 280598cc7dfSVladimir Kotal raise 281598cc7dfSVladimir Kotal return 28296ccc8cbSesaxe 28396ccc8cbSesaxe return data 28496ccc8cbSesaxe 28596ccc8cbSesaxe##### 28696ccc8cbSesaxe# Misc utility functions 28796ccc8cbSesaxe# 28896ccc8cbSesaxe 28996ccc8cbSesaxe# Prune off the leading prefix from string s 29096ccc8cbSesaxedef str_prefix_trunc(s, prefix) : 29196ccc8cbSesaxe snipLen = len(prefix) 29296ccc8cbSesaxe return s[snipLen:] 29396ccc8cbSesaxe 29496ccc8cbSesaxe# 29596ccc8cbSesaxe# Prune off leading proto path goo (if there is one) to yield 29696ccc8cbSesaxe# the deliverable's eventual path relative to root 29796ccc8cbSesaxe# e.g. proto.base/root_sparc/usr/src/cmd/prstat => usr/src/cmd/prstat 29896ccc8cbSesaxe# 29996ccc8cbSesaxedef fnFormat(fn) : 30096ccc8cbSesaxe root_arch_str = "root_" + arch 30196ccc8cbSesaxe 30296ccc8cbSesaxe pos = fn.find(root_arch_str) 30396ccc8cbSesaxe if pos == -1 : 30496ccc8cbSesaxe return fn 30596ccc8cbSesaxe 30696ccc8cbSesaxe pos = fn.find("/", pos) 30796ccc8cbSesaxe if pos == -1 : 30896ccc8cbSesaxe return fn 30996ccc8cbSesaxe 31096ccc8cbSesaxe return fn[pos + 1:] 31196ccc8cbSesaxe 31296ccc8cbSesaxe##### 31396ccc8cbSesaxe# Usage / argument processing 31496ccc8cbSesaxe# 31596ccc8cbSesaxe 31696ccc8cbSesaxe# 31796ccc8cbSesaxe# Display usage message 31896ccc8cbSesaxe# 31996ccc8cbSesaxedef usage() : 32096ccc8cbSesaxe sys.stdout.flush() 32100ff2212SAndy Fiddaman print("""Usage: wsdiff [-dvVst] [-r results ] [-i filelist ] old new 322598cc7dfSVladimir Kotal -d Print debug messages about the progress 32396ccc8cbSesaxe -v Do not truncate observed diffs in results 32496ccc8cbSesaxe -V Log *all* ELF sect diffs vs. logging the first diff found 32596ccc8cbSesaxe -t Use onbld tools in $SRC/tools 32696ccc8cbSesaxe -r Log results and observed differences 327598cc7dfSVladimir Kotal -s Produce sorted list of differences 32800ff2212SAndy Fiddaman -i Tell wsdiff which objects to compare via an input file list""", 32900ff2212SAndy Fiddaman file=sys.stderr) 33096ccc8cbSesaxe sys.exit(1) 33196ccc8cbSesaxe 33296ccc8cbSesaxe# 33396ccc8cbSesaxe# Process command line options 33496ccc8cbSesaxe# 33596ccc8cbSesaxedef args() : 33696ccc8cbSesaxe 337598cc7dfSVladimir Kotal global debugon 33896ccc8cbSesaxe global logging 33996ccc8cbSesaxe global vdiffs 34096ccc8cbSesaxe global reportAllSects 341598cc7dfSVladimir Kotal global sorted 34296ccc8cbSesaxe 343598cc7dfSVladimir Kotal validOpts = 'di:r:vVst?' 34496ccc8cbSesaxe 34596ccc8cbSesaxe baseRoot = "" 34696ccc8cbSesaxe ptchRoot = "" 34796ccc8cbSesaxe fileNamesFile = "" 34896ccc8cbSesaxe results = "" 34996ccc8cbSesaxe localTools = False 35096ccc8cbSesaxe 35196ccc8cbSesaxe # getopt.getopt() returns: 35296ccc8cbSesaxe # an option/value tuple 35396ccc8cbSesaxe # a list of remaining non-option arguments 35496ccc8cbSesaxe # 35596ccc8cbSesaxe # A correct wsdiff invocation will have exactly two non option 35696ccc8cbSesaxe # arguments, the paths to the base (old), ptch (new) proto areas 35796ccc8cbSesaxe try: 35896ccc8cbSesaxe optlist, args = getopt.getopt(sys.argv[1:], validOpts) 35900ff2212SAndy Fiddaman except getopt.error as val: 36096ccc8cbSesaxe usage() 36196ccc8cbSesaxe 36296ccc8cbSesaxe if len(args) != 2 : 36396ccc8cbSesaxe usage(); 36496ccc8cbSesaxe 36596ccc8cbSesaxe for opt,val in optlist : 366598cc7dfSVladimir Kotal if opt == '-d' : 367598cc7dfSVladimir Kotal debugon = True 368598cc7dfSVladimir Kotal elif opt == '-i' : 36996ccc8cbSesaxe fileNamesFile = val 37096ccc8cbSesaxe elif opt == '-r' : 37196ccc8cbSesaxe results = val 37296ccc8cbSesaxe logging = True 373598cc7dfSVladimir Kotal elif opt == '-s' : 374598cc7dfSVladimir Kotal sorted = True 37596ccc8cbSesaxe elif opt == '-v' : 37696ccc8cbSesaxe vdiffs = True 37796ccc8cbSesaxe elif opt == '-V' : 37896ccc8cbSesaxe reportAllSects = True 37996ccc8cbSesaxe elif opt == '-t': 38096ccc8cbSesaxe localTools = True 38196ccc8cbSesaxe else: 38296ccc8cbSesaxe usage() 38396ccc8cbSesaxe 38496ccc8cbSesaxe baseRoot = args[0] 38596ccc8cbSesaxe ptchRoot = args[1] 38696ccc8cbSesaxe 38796ccc8cbSesaxe if len(baseRoot) == 0 or len(ptchRoot) == 0 : 38896ccc8cbSesaxe usage() 38996ccc8cbSesaxe 39096ccc8cbSesaxe if logging and len(results) == 0 : 39196ccc8cbSesaxe usage() 39296ccc8cbSesaxe 39396ccc8cbSesaxe if vdiffs and not logging : 39496ccc8cbSesaxe error("The -v option requires a results file (-r)") 39596ccc8cbSesaxe sys.exit(1) 39696ccc8cbSesaxe 39796ccc8cbSesaxe if reportAllSects and not logging : 39896ccc8cbSesaxe error("The -V option requires a results file (-r)") 39996ccc8cbSesaxe sys.exit(1) 40096ccc8cbSesaxe 40196ccc8cbSesaxe # alphabetical order 40296ccc8cbSesaxe return baseRoot, fileNamesFile, localTools, ptchRoot, results 40396ccc8cbSesaxe 40496ccc8cbSesaxe##### 40596ccc8cbSesaxe# File identification 40696ccc8cbSesaxe# 40796ccc8cbSesaxe 40896ccc8cbSesaxe# 40996ccc8cbSesaxe# Identify the file type. 41096ccc8cbSesaxe# If it's not ELF, use the file extension to identify 41196ccc8cbSesaxe# certain file types that require special handling to 41296ccc8cbSesaxe# compare. Otherwise just return a basic "ASCII" type. 41396ccc8cbSesaxe# 41496ccc8cbSesaxedef getTheFileType(f) : 41596ccc8cbSesaxe 41696ccc8cbSesaxe extensions = { 'a' : 'ELF Object Archive', 41796ccc8cbSesaxe 'jar' : 'Java Archive', 41896ccc8cbSesaxe 'html' : 'HTML', 41996ccc8cbSesaxe 'ln' : 'Lint Library', 42096ccc8cbSesaxe 'db' : 'Sqlite Database' } 42196ccc8cbSesaxe 422619b4598Srotondo try: 42396ccc8cbSesaxe if os.stat(f)[ST_SIZE] == 0 : 42496ccc8cbSesaxe return 'ASCII' 425619b4598Srotondo except: 426619b4598Srotondo error("failed to stat " + f) 427619b4598Srotondo return 'Error' 42896ccc8cbSesaxe 42996ccc8cbSesaxe if isELF(f) == 1 : 43096ccc8cbSesaxe return 'ELF' 43196ccc8cbSesaxe 43296ccc8cbSesaxe fnamelist = f.split('.') 43396ccc8cbSesaxe if len(fnamelist) > 1 : # Test the file extension 43496ccc8cbSesaxe extension = fnamelist[-1] 43596ccc8cbSesaxe if extension in extensions.keys(): 43696ccc8cbSesaxe return extensions[extension] 43796ccc8cbSesaxe 43896ccc8cbSesaxe return 'ASCII' 43996ccc8cbSesaxe 44096ccc8cbSesaxe# 44196ccc8cbSesaxe# Return non-zero if "f" is an ELF file 44296ccc8cbSesaxe# 4432b15649cSAndy Fiddamanelfmagic = b'\177ELF' 44496ccc8cbSesaxedef isELF(f) : 44596ccc8cbSesaxe try: 4463eeb7968SAndy Fiddaman with open(f, mode='rb') as fd: 44796ccc8cbSesaxe magic = fd.read(len(elfmagic)) 44896ccc8cbSesaxe 44996ccc8cbSesaxe if magic == elfmagic : 45096ccc8cbSesaxe return 1 4512b15649cSAndy Fiddaman except: 4522b15649cSAndy Fiddaman pass 45396ccc8cbSesaxe return 0 45496ccc8cbSesaxe 45596ccc8cbSesaxe# 45696ccc8cbSesaxe# Return non-zero is "f" is binary. 45796ccc8cbSesaxe# Consider the file to be binary if it contains any null characters 45896ccc8cbSesaxe# 45996ccc8cbSesaxedef isBinary(f) : 46096ccc8cbSesaxe try: 4613eeb7968SAndy Fiddaman with open(f, mode='rb') as fd: 46296ccc8cbSesaxe s = fd.read() 46396ccc8cbSesaxe 4642b15649cSAndy Fiddaman if s.find(b'\0') == -1 : 46596ccc8cbSesaxe return 0 4662b15649cSAndy Fiddaman except: 4672b15649cSAndy Fiddaman pass 46896ccc8cbSesaxe return 1 46996ccc8cbSesaxe 47096ccc8cbSesaxe##### 47196ccc8cbSesaxe# Directory traversal and file finding 47296ccc8cbSesaxe# 47396ccc8cbSesaxe 47496ccc8cbSesaxe# 47596ccc8cbSesaxe# Return a sorted list of files found under the specified directory 47696ccc8cbSesaxe# 47796ccc8cbSesaxedef findFiles(d) : 47896ccc8cbSesaxe for path, subdirs, files in os.walk(d) : 47996ccc8cbSesaxe files.sort() 48096ccc8cbSesaxe for name in files : 48196ccc8cbSesaxe yield os.path.join(path, name) 48296ccc8cbSesaxe 48396ccc8cbSesaxe# 48496ccc8cbSesaxe# Examine all files in base, ptch 48596ccc8cbSesaxe# 48696ccc8cbSesaxe# Return a list of files appearing in both proto areas, 48796ccc8cbSesaxe# a list of new files (files found only in ptch) and 48896ccc8cbSesaxe# a list of deleted files (files found only in base) 48996ccc8cbSesaxe# 49096ccc8cbSesaxedef protoCatalog(base, ptch) : 491598cc7dfSVladimir Kotal 49296ccc8cbSesaxe compFiles = [] # List of files in both proto areas 49396ccc8cbSesaxe ptchList = [] # List of file in patch proto area 49496ccc8cbSesaxe 49596ccc8cbSesaxe newFiles = [] # New files detected 49696ccc8cbSesaxe deletedFiles = [] # Deleted files 49796ccc8cbSesaxe 498598cc7dfSVladimir Kotal debug("Getting the list of files in the base area"); 49996ccc8cbSesaxe baseFilesList = list(findFiles(base)) 50096ccc8cbSesaxe baseStringLength = len(base) 501598cc7dfSVladimir Kotal debug("Found " + str(len(baseFilesList)) + " files") 50296ccc8cbSesaxe 503598cc7dfSVladimir Kotal debug("Getting the list of files in the patch area"); 50496ccc8cbSesaxe ptchFilesList = list(findFiles(ptch)) 50596ccc8cbSesaxe ptchStringLength = len(ptch) 506598cc7dfSVladimir Kotal debug("Found " + str(len(ptchFilesList)) + " files") 50796ccc8cbSesaxe 50896ccc8cbSesaxe # Inventory files in the base proto area 509598cc7dfSVladimir Kotal debug("Determining the list of regular files in the base area"); 51096ccc8cbSesaxe for fn in baseFilesList : 51196ccc8cbSesaxe if os.path.islink(fn) : 51296ccc8cbSesaxe continue 51396ccc8cbSesaxe 51496ccc8cbSesaxe fileName = fn[baseStringLength:] 51596ccc8cbSesaxe compFiles.append(fileName) 516598cc7dfSVladimir Kotal debug("Found " + str(len(compFiles)) + " files") 51796ccc8cbSesaxe 51896ccc8cbSesaxe # Inventory files in the patch proto area 519598cc7dfSVladimir Kotal debug("Determining the list of regular files in the patch area"); 52096ccc8cbSesaxe for fn in ptchFilesList : 52196ccc8cbSesaxe if os.path.islink(fn) : 52296ccc8cbSesaxe continue 52396ccc8cbSesaxe 52496ccc8cbSesaxe fileName = fn[ptchStringLength:] 52596ccc8cbSesaxe ptchList.append(fileName) 526598cc7dfSVladimir Kotal debug("Found " + str(len(ptchList)) + " files") 52796ccc8cbSesaxe 52896ccc8cbSesaxe # Deleted files appear in the base area, but not the patch area 529598cc7dfSVladimir Kotal debug("Searching for deleted files by comparing the lists") 53096ccc8cbSesaxe for fileName in compFiles : 53196ccc8cbSesaxe if not fileName in ptchList : 53296ccc8cbSesaxe deletedFiles.append(fileName) 533598cc7dfSVladimir Kotal debug("Found " + str(len(deletedFiles)) + " deleted files") 53496ccc8cbSesaxe 53596ccc8cbSesaxe # Eliminate "deleted" files from the list of objects appearing 53696ccc8cbSesaxe # in both the base and patch proto areas 537598cc7dfSVladimir Kotal debug("Eliminating deleted files from the list of objects") 53896ccc8cbSesaxe for fileName in deletedFiles : 53996ccc8cbSesaxe try: 54096ccc8cbSesaxe compFiles.remove(fileName) 54196ccc8cbSesaxe except: 54296ccc8cbSesaxe error("filelist.remove() failed") 54300ff2212SAndy Fiddaman debug("List for comparison reduced to " + str(len(compFiles)) 544598cc7dfSVladimir Kotal + " files") 54596ccc8cbSesaxe 54696ccc8cbSesaxe # New files appear in the patch area, but not the base 547598cc7dfSVladimir Kotal debug("Getting the list of newly added files") 54896ccc8cbSesaxe for fileName in ptchList : 54996ccc8cbSesaxe if not fileName in compFiles : 55096ccc8cbSesaxe newFiles.append(fileName) 551598cc7dfSVladimir Kotal debug("Found " + str(len(newFiles)) + " new files") 55296ccc8cbSesaxe 55396ccc8cbSesaxe return compFiles, newFiles, deletedFiles 55496ccc8cbSesaxe 55596ccc8cbSesaxe# 55696ccc8cbSesaxe# Examine the files listed in the input file list 55796ccc8cbSesaxe# 55896ccc8cbSesaxe# Return a list of files appearing in both proto areas, 55996ccc8cbSesaxe# a list of new files (files found only in ptch) and 56096ccc8cbSesaxe# a list of deleted files (files found only in base) 56196ccc8cbSesaxe# 56296ccc8cbSesaxedef flistCatalog(base, ptch, flist) : 56396ccc8cbSesaxe compFiles = [] # List of files in both proto areas 56496ccc8cbSesaxe newFiles = [] # New files detected 56596ccc8cbSesaxe deletedFiles = [] # Deleted files 56696ccc8cbSesaxe 56796ccc8cbSesaxe try: 56896ccc8cbSesaxe fd = open(flist, "r") 56996ccc8cbSesaxe except: 57096ccc8cbSesaxe error("could not open: " + flist) 57196ccc8cbSesaxe cleanup(1) 57296ccc8cbSesaxe 57396ccc8cbSesaxe files = [] 57496ccc8cbSesaxe files = fd.readlines() 575598cc7dfSVladimir Kotal fd.close() 57696ccc8cbSesaxe 57796ccc8cbSesaxe for f in files : 57896ccc8cbSesaxe ptch_present = True 57996ccc8cbSesaxe base_present = True 58096ccc8cbSesaxe 58196ccc8cbSesaxe if f == '\n' : 58296ccc8cbSesaxe continue 58396ccc8cbSesaxe 58496ccc8cbSesaxe # the fileNames have a trailing '\n' 58596ccc8cbSesaxe f = f.rstrip() 58696ccc8cbSesaxe 58796ccc8cbSesaxe # The objects in the file list have paths relative 58896ccc8cbSesaxe # to $ROOT or to the base/ptch directory specified on 58996ccc8cbSesaxe # the command line. 59096ccc8cbSesaxe # If it's relative to $ROOT, we'll need to add back the 59196ccc8cbSesaxe # root_`uname -p` goo we stripped off in fnFormat() 59296ccc8cbSesaxe if os.path.exists(base + f) : 59396ccc8cbSesaxe fn = f; 59496ccc8cbSesaxe elif os.path.exists(base + "root_" + arch + "/" + f) : 59596ccc8cbSesaxe fn = "root_" + arch + "/" + f 59696ccc8cbSesaxe else : 59796ccc8cbSesaxe base_present = False 59896ccc8cbSesaxe 59996ccc8cbSesaxe if base_present : 60096ccc8cbSesaxe if not os.path.exists(ptch + fn) : 60196ccc8cbSesaxe ptch_present = False 60296ccc8cbSesaxe else : 60396ccc8cbSesaxe if os.path.exists(ptch + f) : 60496ccc8cbSesaxe fn = f 60596ccc8cbSesaxe elif os.path.exists(ptch + "root_" + arch + "/" + f) : 60696ccc8cbSesaxe fn = "root_" + arch + "/" + f 60796ccc8cbSesaxe else : 60896ccc8cbSesaxe ptch_present = False 60996ccc8cbSesaxe 61096ccc8cbSesaxe if os.path.islink(base + fn) : # ignore links 61196ccc8cbSesaxe base_present = False 61296ccc8cbSesaxe if os.path.islink(ptch + fn) : 61396ccc8cbSesaxe ptch_present = False 61496ccc8cbSesaxe 61596ccc8cbSesaxe if base_present and ptch_present : 61696ccc8cbSesaxe compFiles.append(fn) 61796ccc8cbSesaxe elif base_present : 61896ccc8cbSesaxe deletedFiles.append(fn) 61996ccc8cbSesaxe elif ptch_present : 62096ccc8cbSesaxe newFiles.append(fn) 62196ccc8cbSesaxe else : 62200ff2212SAndy Fiddaman if (os.path.islink(base + fn) and 62300ff2212SAndy Fiddaman os.path.islink(ptch + fn)) : 62496ccc8cbSesaxe continue 62500ff2212SAndy Fiddaman error(f + " in file list, but not in either tree. " + 626598cc7dfSVladimir Kotal "Skipping...") 62796ccc8cbSesaxe 62896ccc8cbSesaxe return compFiles, newFiles, deletedFiles 62996ccc8cbSesaxe 63096ccc8cbSesaxe 63196ccc8cbSesaxe# 63296ccc8cbSesaxe# Build a fully qualified path to an external tool/utility. 63396ccc8cbSesaxe# Consider the default system locations. For onbld tools, if 63496ccc8cbSesaxe# the -t option was specified, we'll try to use built tools in $SRC tools, 63596ccc8cbSesaxe# and otherwise, we'll fall back on /opt/onbld/ 63696ccc8cbSesaxe# 63796ccc8cbSesaxedef find_tool(tool) : 63896ccc8cbSesaxe 63996ccc8cbSesaxe # First, check what was passed 64096ccc8cbSesaxe if os.path.exists(tool) : 64196ccc8cbSesaxe return tool 64296ccc8cbSesaxe 64396ccc8cbSesaxe # Next try in wsdiff path 64496ccc8cbSesaxe for pdir in wsdiff_path : 64596ccc8cbSesaxe location = pdir + "/" + tool 64696ccc8cbSesaxe if os.path.exists(location) : 64796ccc8cbSesaxe return location + " " 64896ccc8cbSesaxe 64996ccc8cbSesaxe location = pdir + "/" + arch + "/" + tool 65096ccc8cbSesaxe if os.path.exists(location) : 65196ccc8cbSesaxe return location + " " 65296ccc8cbSesaxe 65396ccc8cbSesaxe error("Could not find path to: " + tool); 65496ccc8cbSesaxe sys.exit(1); 65596ccc8cbSesaxe 65696ccc8cbSesaxe 65796ccc8cbSesaxe##### 65896ccc8cbSesaxe# ELF file comparison helper routines 65996ccc8cbSesaxe# 66096ccc8cbSesaxe 66196ccc8cbSesaxe# 66296ccc8cbSesaxe# Return a dictionary of ELF section types keyed by section name 66396ccc8cbSesaxe# 66496ccc8cbSesaxedef get_elfheader(f) : 66596ccc8cbSesaxe 66696ccc8cbSesaxe header = {} 66796ccc8cbSesaxe 6682b15649cSAndy Fiddaman rc, hstring = getoutput(elfdump_cmd + " -c " + f) 66996ccc8cbSesaxe 67096ccc8cbSesaxe if len(hstring) == 0 : 67196ccc8cbSesaxe error("Failed to dump ELF header for " + f) 672598cc7dfSVladimir Kotal raise 67396ccc8cbSesaxe return 67496ccc8cbSesaxe 67596ccc8cbSesaxe # elfdump(1) dumps the section headers with the section name 67696ccc8cbSesaxe # following "sh_name:", and the section type following "sh_type:" 67796ccc8cbSesaxe sections = hstring.split("Section Header") 67896ccc8cbSesaxe for sect in sections : 67996ccc8cbSesaxe datap = sect.find("sh_name:"); 68096ccc8cbSesaxe if datap == -1 : 68196ccc8cbSesaxe continue 68296ccc8cbSesaxe section = sect[datap:].split()[1] 68396ccc8cbSesaxe datap = sect.find("sh_type:"); 68496ccc8cbSesaxe if datap == -1 : 68500ff2212SAndy Fiddaman error("Could not get type for sect: " + section + 68696ccc8cbSesaxe " in " + f) 68796ccc8cbSesaxe sh_type = sect[datap:].split()[2] 68896ccc8cbSesaxe header[section] = sh_type 68996ccc8cbSesaxe 69096ccc8cbSesaxe return header 69196ccc8cbSesaxe 69296ccc8cbSesaxe# 69396ccc8cbSesaxe# Extract data in the specified ELF section from the given file 69496ccc8cbSesaxe# 69596ccc8cbSesaxedef extract_elf_section(f, section) : 69696ccc8cbSesaxe 6972b15649cSAndy Fiddaman rc, data = getoutput(dump_cmd + " -sn " + section + " " + f) 69896ccc8cbSesaxe 69996ccc8cbSesaxe if len(data) == 0 : 70000ff2212SAndy Fiddaman error(dump_cmd + "yielded no data on section " + section + 701598cc7dfSVladimir Kotal " of " + f) 702598cc7dfSVladimir Kotal raise 70396ccc8cbSesaxe return 70496ccc8cbSesaxe 70596ccc8cbSesaxe # dump(1) displays the file name to start... 70696ccc8cbSesaxe # get past it to the data itself 70796ccc8cbSesaxe dbegin = data.find(":") + 1 70896ccc8cbSesaxe data = data[dbegin:]; 70996ccc8cbSesaxe 71096ccc8cbSesaxe return (data) 71196ccc8cbSesaxe 71296ccc8cbSesaxe# 71396ccc8cbSesaxe# Return a (hopefully meaningful) human readable set of diffs 71496ccc8cbSesaxe# for the specified ELF section between f1 and f2 71596ccc8cbSesaxe# 71696ccc8cbSesaxe# Depending on the section, various means for dumping and diffing 71796ccc8cbSesaxe# the data may be employed. 71896ccc8cbSesaxe# 71996ccc8cbSesaxetext_sections = [ '.text', '.init', '.fini' ] 72096ccc8cbSesaxedef diff_elf_section(f1, f2, section, sh_type) : 72196ccc8cbSesaxe 722598cc7dfSVladimir Kotal t = threading.currentThread() 723598cc7dfSVladimir Kotal tmpFile1 = tmpDir1 + os.path.basename(f1) + t.getName() 724598cc7dfSVladimir Kotal tmpFile2 = tmpDir2 + os.path.basename(f2) + t.getName() 725598cc7dfSVladimir Kotal 72696ccc8cbSesaxe if (sh_type == "SHT_RELA") : # sh_type == SHT_RELA 72796ccc8cbSesaxe cmd1 = elfdump_cmd + " -r " + f1 + " > " + tmpFile1 72896ccc8cbSesaxe cmd2 = elfdump_cmd + " -r " + f2 + " > " + tmpFile2 72996ccc8cbSesaxe elif (section == ".group") : 73096ccc8cbSesaxe cmd1 = elfdump_cmd + " -g " + f1 + " > " + tmpFile1 73196ccc8cbSesaxe cmd2 = elfdump_cmd + " -g " + f2 + " > " + tmpFile2 73296ccc8cbSesaxe elif (section == ".hash") : 73396ccc8cbSesaxe cmd1 = elfdump_cmd + " -h " + f1 + " > " + tmpFile1 73496ccc8cbSesaxe cmd2 = elfdump_cmd + " -h " + f2 + " > " + tmpFile2 73596ccc8cbSesaxe elif (section == ".dynamic") : 73696ccc8cbSesaxe cmd1 = elfdump_cmd + " -d " + f1 + " > " + tmpFile1 73796ccc8cbSesaxe cmd2 = elfdump_cmd + " -d " + f2 + " > " + tmpFile2 73896ccc8cbSesaxe elif (section == ".got") : 73996ccc8cbSesaxe cmd1 = elfdump_cmd + " -G " + f1 + " > " + tmpFile1 74096ccc8cbSesaxe cmd2 = elfdump_cmd + " -G " + f2 + " > " + tmpFile2 74196ccc8cbSesaxe elif (section == ".SUNW_cap") : 74296ccc8cbSesaxe cmd1 = elfdump_cmd + " -H " + f1 + " > " + tmpFile1 74396ccc8cbSesaxe cmd2 = elfdump_cmd + " -H " + f2 + " > " + tmpFile2 74496ccc8cbSesaxe elif (section == ".interp") : 74596ccc8cbSesaxe cmd1 = elfdump_cmd + " -i " + f1 + " > " + tmpFile1 74696ccc8cbSesaxe cmd2 = elfdump_cmd + " -i " + f2 + " > " + tmpFile2 74796ccc8cbSesaxe elif (section == ".symtab" or section == ".dynsym") : 74800ff2212SAndy Fiddaman cmd1 = (elfdump_cmd + " -s -N " + section + " " + f1 + 74900ff2212SAndy Fiddaman " > " + tmpFile1) 75000ff2212SAndy Fiddaman cmd2 = (elfdump_cmd + " -s -N " + section + " " + f2 + 75100ff2212SAndy Fiddaman " > " + tmpFile2) 75296ccc8cbSesaxe elif (section in text_sections) : 75396ccc8cbSesaxe # dis sometimes complains when it hits something it doesn't 75496ccc8cbSesaxe # know how to disassemble. Just ignore it, as the output 75596ccc8cbSesaxe # being generated here is human readable, and we've already 75696ccc8cbSesaxe # correctly flagged the difference. 75700ff2212SAndy Fiddaman cmd1 = (dis_cmd + " -t " + section + " " + f1 + 75800ff2212SAndy Fiddaman " 2>/dev/null | grep -v disassembly > " + tmpFile1) 75900ff2212SAndy Fiddaman cmd2 = (dis_cmd + " -t " + section + " " + f2 + 76000ff2212SAndy Fiddaman " 2>/dev/null | grep -v disassembly > " + tmpFile2) 76196ccc8cbSesaxe else : 76200ff2212SAndy Fiddaman cmd1 = (elfdump_cmd + " -w " + tmpFile1 + " -N " + 76300ff2212SAndy Fiddaman section + " " + f1) 76400ff2212SAndy Fiddaman cmd2 = (elfdump_cmd + " -w " + tmpFile2 + " -N " + 76500ff2212SAndy Fiddaman section + " " + f2) 76696ccc8cbSesaxe 76796ccc8cbSesaxe os.system(cmd1) 76896ccc8cbSesaxe os.system(cmd2) 76996ccc8cbSesaxe 77096ccc8cbSesaxe data = diffFileData(tmpFile1, tmpFile2) 77196ccc8cbSesaxe 772598cc7dfSVladimir Kotal # remove temp files as we no longer need them 773598cc7dfSVladimir Kotal try: 774598cc7dfSVladimir Kotal os.unlink(tmpFile1) 77500ff2212SAndy Fiddaman except OSError as e: 776598cc7dfSVladimir Kotal error("diff_elf_section: unlink failed %s" % e) 777598cc7dfSVladimir Kotal try: 778598cc7dfSVladimir Kotal os.unlink(tmpFile2) 77900ff2212SAndy Fiddaman except OSError as e: 780598cc7dfSVladimir Kotal error("diff_elf_section: unlink failed %s" % e) 781598cc7dfSVladimir Kotal 78296ccc8cbSesaxe return (data) 78396ccc8cbSesaxe 78496ccc8cbSesaxe# 78596ccc8cbSesaxe# compare the relevant sections of two ELF binaries 78696ccc8cbSesaxe# and report any differences 78796ccc8cbSesaxe# 78896ccc8cbSesaxe# Returns: 1 if any differenes found 78996ccc8cbSesaxe# 0 if no differences found 79096ccc8cbSesaxe# -1 on error 79196ccc8cbSesaxe# 79296ccc8cbSesaxe 79396ccc8cbSesaxe# Sections deliberately not considered when comparing two ELF 79496ccc8cbSesaxe# binaries. Differences observed in these sections are not considered 79596ccc8cbSesaxe# significant where patch deliverable identification is concerned. 79696ccc8cbSesaxesections_to_skip = [ ".SUNW_signature", 79796ccc8cbSesaxe ".comment", 79896ccc8cbSesaxe ".SUNW_ctf", 79996ccc8cbSesaxe ".debug", 80096ccc8cbSesaxe ".plt", 80196ccc8cbSesaxe ".rela.bss", 80296ccc8cbSesaxe ".rela.plt", 80396ccc8cbSesaxe ".line", 80496ccc8cbSesaxe ".note", 805f6a1d796Sesaxe ".compcom", 80696ccc8cbSesaxe ] 80796ccc8cbSesaxe 80896ccc8cbSesaxesections_preferred = [ ".rodata.str1.8", 80996ccc8cbSesaxe ".rodata.str1.1", 81096ccc8cbSesaxe ".rodata", 81196ccc8cbSesaxe ".data1", 81296ccc8cbSesaxe ".data", 81396ccc8cbSesaxe ".text", 81496ccc8cbSesaxe ] 81596ccc8cbSesaxe 81696ccc8cbSesaxedef compareElfs(base, ptch, quiet) : 81796ccc8cbSesaxe 81896ccc8cbSesaxe global logging 81996ccc8cbSesaxe 820598cc7dfSVladimir Kotal try: 82196ccc8cbSesaxe base_header = get_elfheader(base) 822598cc7dfSVladimir Kotal except: 823598cc7dfSVladimir Kotal return 8242b15649cSAndy Fiddaman sections = list(base_header.keys()) 82596ccc8cbSesaxe 826598cc7dfSVladimir Kotal try: 82796ccc8cbSesaxe ptch_header = get_elfheader(ptch) 828598cc7dfSVladimir Kotal except: 829598cc7dfSVladimir Kotal return 8302b15649cSAndy Fiddaman e2_only_sections = list(ptch_header.keys()) 83196ccc8cbSesaxe 83296ccc8cbSesaxe e1_only_sections = [] 83396ccc8cbSesaxe 83496ccc8cbSesaxe fileName = fnFormat(base) 83596ccc8cbSesaxe 83696ccc8cbSesaxe # Derive the list of ELF sections found only in 83796ccc8cbSesaxe # either e1 or e2. 83896ccc8cbSesaxe for sect in sections : 83996ccc8cbSesaxe if not sect in e2_only_sections : 84096ccc8cbSesaxe e1_only_sections.append(sect) 84196ccc8cbSesaxe else : 84296ccc8cbSesaxe e2_only_sections.remove(sect) 84396ccc8cbSesaxe 84496ccc8cbSesaxe if len(e1_only_sections) > 0 : 84596ccc8cbSesaxe if quiet : 84696ccc8cbSesaxe return 1 84796ccc8cbSesaxe 848598cc7dfSVladimir Kotal data = "" 849598cc7dfSVladimir Kotal if logging : 85096ccc8cbSesaxe slist = "" 85196ccc8cbSesaxe for sect in e1_only_sections : 85296ccc8cbSesaxe slist = slist + sect + "\t" 85300ff2212SAndy Fiddaman data = ("ELF sections found in " + 85400ff2212SAndy Fiddaman base + " but not in " + ptch + 85500ff2212SAndy Fiddaman "\n\n" + slist) 856598cc7dfSVladimir Kotal 857598cc7dfSVladimir Kotal difference(fileName, "ELF", data) 85896ccc8cbSesaxe return 1 85996ccc8cbSesaxe 86096ccc8cbSesaxe if len(e2_only_sections) > 0 : 86196ccc8cbSesaxe if quiet : 86296ccc8cbSesaxe return 1 86396ccc8cbSesaxe 864598cc7dfSVladimir Kotal data = "" 865598cc7dfSVladimir Kotal if logging : 86696ccc8cbSesaxe slist = "" 86796ccc8cbSesaxe for sect in e2_only_sections : 86896ccc8cbSesaxe slist = slist + sect + "\t" 86900ff2212SAndy Fiddaman data = ("ELF sections found in " + 87000ff2212SAndy Fiddaman ptch + " but not in " + base + 87100ff2212SAndy Fiddaman "\n\n" + slist) 872598cc7dfSVladimir Kotal 873598cc7dfSVladimir Kotal difference(fileName, "ELF", data) 87496ccc8cbSesaxe return 1 87596ccc8cbSesaxe 87696ccc8cbSesaxe # Look for preferred sections, and put those at the 87796ccc8cbSesaxe # top of the list of sections to compare 87896ccc8cbSesaxe for psect in sections_preferred : 87996ccc8cbSesaxe if psect in sections : 88096ccc8cbSesaxe sections.remove(psect) 88196ccc8cbSesaxe sections.insert(0, psect) 88296ccc8cbSesaxe 88396ccc8cbSesaxe # Compare ELF sections 88496ccc8cbSesaxe first_section = True 88596ccc8cbSesaxe for sect in sections : 88696ccc8cbSesaxe 88796ccc8cbSesaxe if sect in sections_to_skip : 88896ccc8cbSesaxe continue 88996ccc8cbSesaxe 890598cc7dfSVladimir Kotal try: 89196ccc8cbSesaxe s1 = extract_elf_section(base, sect); 892598cc7dfSVladimir Kotal except: 893598cc7dfSVladimir Kotal return 894598cc7dfSVladimir Kotal 895598cc7dfSVladimir Kotal try: 89696ccc8cbSesaxe s2 = extract_elf_section(ptch, sect); 897598cc7dfSVladimir Kotal except: 898598cc7dfSVladimir Kotal return 89996ccc8cbSesaxe 90096ccc8cbSesaxe if len(s1) != len (s2) or s1 != s2: 90196ccc8cbSesaxe if not quiet: 90296ccc8cbSesaxe sh_type = base_header[sect] 90300ff2212SAndy Fiddaman data = diff_elf_section(base, ptch, 904598cc7dfSVladimir Kotal sect, sh_type) 90596ccc8cbSesaxe 90696ccc8cbSesaxe # If all ELF sections are being reported, then 90796ccc8cbSesaxe # invoke difference() to flag the file name to 90896ccc8cbSesaxe # stdout only once. Any other section differences 90996ccc8cbSesaxe # should be logged to the results file directly 91096ccc8cbSesaxe if not first_section : 91100ff2212SAndy Fiddaman log_difference(fileName, 912598cc7dfSVladimir Kotal "ELF " + sect, data) 91396ccc8cbSesaxe else : 91400ff2212SAndy Fiddaman difference(fileName, "ELF " + sect, 915598cc7dfSVladimir Kotal data) 91696ccc8cbSesaxe 91796ccc8cbSesaxe if not reportAllSects : 91896ccc8cbSesaxe return 1 91996ccc8cbSesaxe first_section = False 920598cc7dfSVladimir Kotal 92196ccc8cbSesaxe return 0 92296ccc8cbSesaxe 92396ccc8cbSesaxe##### 924598cc7dfSVladimir Kotal# recursively remove 2 directories 925598cc7dfSVladimir Kotal# 926598cc7dfSVladimir Kotal# Used for removal of temporary directory strucures (ignores any errors). 927598cc7dfSVladimir Kotal# 928598cc7dfSVladimir Kotaldef clearTmpDirs(dir1, dir2) : 929598cc7dfSVladimir Kotal 930598cc7dfSVladimir Kotal if os.path.isdir(dir1) > 0 : 931598cc7dfSVladimir Kotal shutil.rmtree(dir1, True) 932598cc7dfSVladimir Kotal 933598cc7dfSVladimir Kotal if os.path.isdir(dir2) > 0 : 934598cc7dfSVladimir Kotal shutil.rmtree(dir2, True) 935598cc7dfSVladimir Kotal 936598cc7dfSVladimir Kotal 937598cc7dfSVladimir Kotal##### 93896ccc8cbSesaxe# Archive object comparison 93996ccc8cbSesaxe# 94096ccc8cbSesaxe# Returns 1 if difference detected 94196ccc8cbSesaxe# 0 if no difference detected 94296ccc8cbSesaxe# -1 on error 94396ccc8cbSesaxe# 94496ccc8cbSesaxedef compareArchives(base, ptch, fileType) : 94596ccc8cbSesaxe 94696ccc8cbSesaxe fileName = fnFormat(base) 947598cc7dfSVladimir Kotal t = threading.currentThread() 948598cc7dfSVladimir Kotal ArchTmpDir1 = tmpDir1 + os.path.basename(base) + t.getName() 949598cc7dfSVladimir Kotal ArchTmpDir2 = tmpDir2 + os.path.basename(base) + t.getName() 95096ccc8cbSesaxe 95196ccc8cbSesaxe # 95296ccc8cbSesaxe # Be optimistic and first try a straight file compare 95396ccc8cbSesaxe # as it will allow us to finish up quickly. 954598cc7dfSVladimir Kotal # 95596ccc8cbSesaxe if compareBasic(base, ptch, True, fileType) == 0 : 95696ccc8cbSesaxe return 0 95796ccc8cbSesaxe 958598cc7dfSVladimir Kotal try: 959598cc7dfSVladimir Kotal os.makedirs(ArchTmpDir1) 96000ff2212SAndy Fiddaman except OSError as e: 961598cc7dfSVladimir Kotal error("compareArchives: makedir failed %s" % e) 962598cc7dfSVladimir Kotal return -1 963598cc7dfSVladimir Kotal try: 964598cc7dfSVladimir Kotal os.makedirs(ArchTmpDir2) 96500ff2212SAndy Fiddaman except OSError as e: 966598cc7dfSVladimir Kotal error("compareArchives: makedir failed %s" % e) 967598cc7dfSVladimir Kotal return -1 968598cc7dfSVladimir Kotal 96996ccc8cbSesaxe # copy over the objects to the temp areas, and 97096ccc8cbSesaxe # unpack them 971598cc7dfSVladimir Kotal baseCmd = "cp -fp " + base + " " + ArchTmpDir1 9722b15649cSAndy Fiddaman rc, output = getoutput(baseCmd) 9732b15649cSAndy Fiddaman if rc != 0: 97496ccc8cbSesaxe error(baseCmd + " failed: " + output) 975598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 97696ccc8cbSesaxe return -1 97796ccc8cbSesaxe 978598cc7dfSVladimir Kotal ptchCmd = "cp -fp " + ptch + " " + ArchTmpDir2 9792b15649cSAndy Fiddaman rc, output = getoutput(ptchCmd) 9802b15649cSAndy Fiddaman if rc != 0: 98196ccc8cbSesaxe error(ptchCmd + " failed: " + output) 982598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 98396ccc8cbSesaxe return -1 98496ccc8cbSesaxe 9852b15649cSAndy Fiddaman bname = fileName.split('/')[-1] 98696ccc8cbSesaxe if fileType == "Java Archive" : 98700ff2212SAndy Fiddaman baseCmd = ("cd " + ArchTmpDir1 + "; " + "jar xf " + bname + 98800ff2212SAndy Fiddaman "; rm -f " + bname + " META-INF/MANIFEST.MF") 98900ff2212SAndy Fiddaman ptchCmd = ("cd " + ArchTmpDir2 + "; " + "jar xf " + bname + 99000ff2212SAndy Fiddaman "; rm -f " + bname + " META-INF/MANIFEST.MF") 99196ccc8cbSesaxe elif fileType == "ELF Object Archive" : 99200ff2212SAndy Fiddaman baseCmd = ("cd " + ArchTmpDir1 + "; " + "/usr/ccs/bin/ar x " + 99300ff2212SAndy Fiddaman bname + "; rm -f " + bname) 99400ff2212SAndy Fiddaman ptchCmd = ("cd " + ArchTmpDir2 + "; " + "/usr/ccs/bin/ar x " + 99500ff2212SAndy Fiddaman bname + "; rm -f " + bname) 99696ccc8cbSesaxe else : 99796ccc8cbSesaxe error("unexpected file type: " + fileType) 998598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 99996ccc8cbSesaxe return -1 100096ccc8cbSesaxe 100196ccc8cbSesaxe os.system(baseCmd) 100296ccc8cbSesaxe os.system(ptchCmd) 100396ccc8cbSesaxe 1004598cc7dfSVladimir Kotal baseFlist = list(findFiles(ArchTmpDir1)) 1005598cc7dfSVladimir Kotal ptchFlist = list(findFiles(ArchTmpDir2)) 100696ccc8cbSesaxe 100796ccc8cbSesaxe # Trim leading path off base/ptch file lists 100896ccc8cbSesaxe flist = [] 100996ccc8cbSesaxe for fn in baseFlist : 1010598cc7dfSVladimir Kotal flist.append(str_prefix_trunc(fn, ArchTmpDir1)) 101196ccc8cbSesaxe baseFlist = flist 101296ccc8cbSesaxe 101396ccc8cbSesaxe flist = [] 101496ccc8cbSesaxe for fn in ptchFlist : 1015598cc7dfSVladimir Kotal flist.append(str_prefix_trunc(fn, ArchTmpDir2)) 101696ccc8cbSesaxe ptchFlist = flist 101796ccc8cbSesaxe 101896ccc8cbSesaxe for fn in ptchFlist : 101996ccc8cbSesaxe if not fn in baseFlist : 102000ff2212SAndy Fiddaman difference(fileName, fileType, 102196ccc8cbSesaxe fn + " added to " + fileName) 1022598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 102396ccc8cbSesaxe return 1 102496ccc8cbSesaxe 102596ccc8cbSesaxe for fn in baseFlist : 102696ccc8cbSesaxe if not fn in ptchFlist : 102700ff2212SAndy Fiddaman difference(fileName, fileType, 102896ccc8cbSesaxe fn + " removed from " + fileName) 1029598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 103096ccc8cbSesaxe return 1 103196ccc8cbSesaxe 103200ff2212SAndy Fiddaman differs = compareOneFile((ArchTmpDir1 + fn), 1033598cc7dfSVladimir Kotal (ArchTmpDir2 + fn), True) 103496ccc8cbSesaxe if differs : 103500ff2212SAndy Fiddaman difference(fileName, fileType, 103696ccc8cbSesaxe fn + " in " + fileName + " differs") 1037598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 103896ccc8cbSesaxe return 1 1039598cc7dfSVladimir Kotal 1040598cc7dfSVladimir Kotal clearTmpDirs(ArchTmpDir1, ArchTmpDir2) 104196ccc8cbSesaxe return 0 104296ccc8cbSesaxe 104396ccc8cbSesaxe##### 104496ccc8cbSesaxe# (Basic) file comparison 104596ccc8cbSesaxe# 104696ccc8cbSesaxe# Returns 1 if difference detected 104796ccc8cbSesaxe# 0 if no difference detected 104896ccc8cbSesaxe# -1 on error 104996ccc8cbSesaxe# 105096ccc8cbSesaxedef compareBasic(base, ptch, quiet, fileType) : 105196ccc8cbSesaxe 105296ccc8cbSesaxe fileName = fnFormat(base); 105396ccc8cbSesaxe 105496ccc8cbSesaxe if quiet and os.stat(base)[ST_SIZE] != os.stat(ptch)[ST_SIZE] : 105596ccc8cbSesaxe return 1 105696ccc8cbSesaxe 105796ccc8cbSesaxe try: 10583eeb7968SAndy Fiddaman with open(base, 'rb') as fh: 10593eeb7968SAndy Fiddaman baseData = fh.read() 106096ccc8cbSesaxe except: 106196ccc8cbSesaxe error("could not open " + base) 106296ccc8cbSesaxe return -1 10633eeb7968SAndy Fiddaman 106496ccc8cbSesaxe try: 10653eeb7968SAndy Fiddaman with open(ptch, 'rb') as fh: 10663eeb7968SAndy Fiddaman ptchData = fh.read() 106796ccc8cbSesaxe except: 106896ccc8cbSesaxe error("could not open " + ptch) 106996ccc8cbSesaxe return -1 107096ccc8cbSesaxe 107196ccc8cbSesaxe if quiet : 107296ccc8cbSesaxe if baseData != ptchData : 107396ccc8cbSesaxe return 1 107496ccc8cbSesaxe else : 107596ccc8cbSesaxe if len(baseData) != len(ptchData) or baseData != ptchData : 10763eeb7968SAndy Fiddaman diffs = diffFileData(base, ptch) 107796ccc8cbSesaxe difference(fileName, fileType, diffs) 107896ccc8cbSesaxe return 1 107996ccc8cbSesaxe return 0 108096ccc8cbSesaxe 108196ccc8cbSesaxe 108296ccc8cbSesaxe##### 108396ccc8cbSesaxe# Compare two objects by producing a data dump from 108496ccc8cbSesaxe# each object, and then comparing the dump data 108596ccc8cbSesaxe# 108696ccc8cbSesaxe# Returns: 1 if a difference is detected 108796ccc8cbSesaxe# 0 if no difference detected 108896ccc8cbSesaxe# -1 upon error 108996ccc8cbSesaxe# 109096ccc8cbSesaxedef compareByDumping(base, ptch, quiet, fileType) : 109196ccc8cbSesaxe 109296ccc8cbSesaxe fileName = fnFormat(base); 1093598cc7dfSVladimir Kotal t = threading.currentThread() 1094598cc7dfSVladimir Kotal tmpFile1 = tmpDir1 + os.path.basename(base) + t.getName() 1095598cc7dfSVladimir Kotal tmpFile2 = tmpDir2 + os.path.basename(ptch) + t.getName() 109696ccc8cbSesaxe 109796ccc8cbSesaxe if fileType == "Lint Library" : 109800ff2212SAndy Fiddaman baseCmd = (lintdump_cmd + " -ir " + base + 109900ff2212SAndy Fiddaman " | egrep -v '(LINTOBJ|LINTMOD):'" + 110000ff2212SAndy Fiddaman " | grep -v PASS[1-3]:" + 110100ff2212SAndy Fiddaman " > " + tmpFile1) 110200ff2212SAndy Fiddaman ptchCmd = (lintdump_cmd + " -ir " + ptch + 110300ff2212SAndy Fiddaman " | egrep -v '(LINTOBJ|LINTMOD):'" + 110400ff2212SAndy Fiddaman " | grep -v PASS[1-3]:" + 110500ff2212SAndy Fiddaman " > " + tmpFile2) 110696ccc8cbSesaxe elif fileType == "Sqlite Database" : 110700ff2212SAndy Fiddaman baseCmd = ("echo .dump | " + sqlite_cmd + base + " > " + 110800ff2212SAndy Fiddaman tmpFile1) 110900ff2212SAndy Fiddaman ptchCmd = ("echo .dump | " + sqlite_cmd + ptch + " > " + 111000ff2212SAndy Fiddaman tmpFile2) 111196ccc8cbSesaxe 111296ccc8cbSesaxe os.system(baseCmd) 111396ccc8cbSesaxe os.system(ptchCmd) 111496ccc8cbSesaxe 111596ccc8cbSesaxe try: 11163eeb7968SAndy Fiddaman with open(tmpFile1, 'rb') as fh: 11173eeb7968SAndy Fiddaman baseData = fh.read() 111896ccc8cbSesaxe except: 111996ccc8cbSesaxe error("could not open: " + tmpFile1) 1120598cc7dfSVladimir Kotal return 11213eeb7968SAndy Fiddaman 112296ccc8cbSesaxe try: 11233eeb7968SAndy Fiddaman with open(tmpFile2, 'rb') as fh: 11243eeb7968SAndy Fiddaman ptchData = fh.read() 112596ccc8cbSesaxe except: 112696ccc8cbSesaxe error("could not open: " + tmpFile2) 1127598cc7dfSVladimir Kotal return 112896ccc8cbSesaxe 11293eeb7968SAndy Fiddaman ret = 0 113096ccc8cbSesaxe 113196ccc8cbSesaxe if len(baseData) != len(ptchData) or baseData != ptchData : 113296ccc8cbSesaxe if not quiet : 113396ccc8cbSesaxe data = diffFileData(tmpFile1, tmpFile2); 11343eeb7968SAndy Fiddaman ret = 1 1135598cc7dfSVladimir Kotal 1136598cc7dfSVladimir Kotal # Remove the temporary files now. 1137598cc7dfSVladimir Kotal try: 1138598cc7dfSVladimir Kotal os.unlink(tmpFile1) 113900ff2212SAndy Fiddaman except OSError as e: 1140598cc7dfSVladimir Kotal error("compareByDumping: unlink failed %s" % e) 1141598cc7dfSVladimir Kotal try: 1142598cc7dfSVladimir Kotal os.unlink(tmpFile2) 114300ff2212SAndy Fiddaman except OSError as e: 1144598cc7dfSVladimir Kotal error("compareByDumping: unlink failed %s" % e) 1145598cc7dfSVladimir Kotal 11463eeb7968SAndy Fiddaman return ret 114796ccc8cbSesaxe 114896ccc8cbSesaxe##### 1149619b4598Srotondo# 1150598cc7dfSVladimir Kotal# SIGINT signal handler. Changes thread control variable to tell the threads 1151598cc7dfSVladimir Kotal# to finish their current job and exit. 1152619b4598Srotondo# 1153598cc7dfSVladimir Kotaldef discontinue_processing(signl, frme): 1154598cc7dfSVladimir Kotal global keep_processing 1155619b4598Srotondo 115600ff2212SAndy Fiddaman print("Caught Ctrl-C, stopping the threads", file=sys.stderr) 1157598cc7dfSVladimir Kotal keep_processing = False 1158619b4598Srotondo 1159598cc7dfSVladimir Kotal return 0 1160619b4598Srotondo 1161598cc7dfSVladimir Kotal##### 1162598cc7dfSVladimir Kotal# 1163598cc7dfSVladimir Kotal# worker thread for changedFiles processing 1164598cc7dfSVladimir Kotal# 1165598cc7dfSVladimir Kotalclass workerThread(threading.Thread) : 1166598cc7dfSVladimir Kotal def run(self): 1167598cc7dfSVladimir Kotal global wset_lock 1168598cc7dfSVladimir Kotal global changedFiles 1169598cc7dfSVladimir Kotal global baseRoot 1170598cc7dfSVladimir Kotal global ptchRoot 1171598cc7dfSVladimir Kotal global keep_processing 1172619b4598Srotondo 1173598cc7dfSVladimir Kotal while (keep_processing) : 1174598cc7dfSVladimir Kotal # grab the lock to changedFiles and remove one member 1175598cc7dfSVladimir Kotal # and process it 1176598cc7dfSVladimir Kotal wset_lock.acquire() 1177598cc7dfSVladimir Kotal try : 1178598cc7dfSVladimir Kotal fn = changedFiles.pop() 1179598cc7dfSVladimir Kotal except IndexError : 1180598cc7dfSVladimir Kotal # there is nothing more to do 1181598cc7dfSVladimir Kotal wset_lock.release() 1182598cc7dfSVladimir Kotal return 1183598cc7dfSVladimir Kotal wset_lock.release() 1184598cc7dfSVladimir Kotal 1185598cc7dfSVladimir Kotal base = baseRoot + fn 1186598cc7dfSVladimir Kotal ptch = ptchRoot + fn 1187598cc7dfSVladimir Kotal 1188598cc7dfSVladimir Kotal compareOneFile(base, ptch, False) 1189598cc7dfSVladimir Kotal 1190619b4598Srotondo 1191619b4598Srotondo##### 119296ccc8cbSesaxe# Compare two objects. Detect type changes. 119396ccc8cbSesaxe# Vector off to the appropriate type specific 119496ccc8cbSesaxe# compare routine based on the type. 119596ccc8cbSesaxe# 119696ccc8cbSesaxedef compareOneFile(base, ptch, quiet) : 119796ccc8cbSesaxe 119896ccc8cbSesaxe # Verify the file types. 119996ccc8cbSesaxe # If they are different, indicate this and move on 120096ccc8cbSesaxe btype = getTheFileType(base) 120196ccc8cbSesaxe ptype = getTheFileType(ptch) 120296ccc8cbSesaxe 1203619b4598Srotondo if btype == 'Error' or ptype == 'Error' : 1204619b4598Srotondo return -1 1205619b4598Srotondo 120696ccc8cbSesaxe fileName = fnFormat(base) 120796ccc8cbSesaxe 120896ccc8cbSesaxe if (btype != ptype) : 1209619b4598Srotondo if not quiet : 121096ccc8cbSesaxe difference(fileName, "file type", btype + " to " + ptype) 121196ccc8cbSesaxe return 1 121296ccc8cbSesaxe else : 121396ccc8cbSesaxe fileType = btype 121496ccc8cbSesaxe 121596ccc8cbSesaxe if (fileType == 'ELF') : 121696ccc8cbSesaxe return compareElfs(base, ptch, quiet) 121796ccc8cbSesaxe 121896ccc8cbSesaxe elif (fileType == 'Java Archive' or fileType == 'ELF Object Archive') : 121996ccc8cbSesaxe return compareArchives(base, ptch, fileType) 122096ccc8cbSesaxe 122196ccc8cbSesaxe elif (fileType == 'HTML') : 122296ccc8cbSesaxe return compareBasic(base, ptch, quiet, fileType) 122396ccc8cbSesaxe 122496ccc8cbSesaxe elif ( fileType == 'Lint Library' ) : 122596ccc8cbSesaxe return compareByDumping(base, ptch, quiet, fileType) 122696ccc8cbSesaxe 122796ccc8cbSesaxe elif ( fileType == 'Sqlite Database' ) : 122896ccc8cbSesaxe return compareByDumping(base, ptch, quiet, fileType) 1229619b4598Srotondo 123096ccc8cbSesaxe else : 123196ccc8cbSesaxe # it has to be some variety of text file 123296ccc8cbSesaxe return compareBasic(base, ptch, quiet, fileType) 123396ccc8cbSesaxe 123496ccc8cbSesaxe# Cleanup and self-terminate 123596ccc8cbSesaxedef cleanup(ret) : 123696ccc8cbSesaxe 1237598cc7dfSVladimir Kotal debug("Performing cleanup (" + str(ret) + ")") 1238598cc7dfSVladimir Kotal if os.path.isdir(tmpDir1) > 0 : 1239598cc7dfSVladimir Kotal shutil.rmtree(tmpDir1) 124096ccc8cbSesaxe 1241598cc7dfSVladimir Kotal if os.path.isdir(tmpDir2) > 0 : 1242598cc7dfSVladimir Kotal shutil.rmtree(tmpDir2) 124396ccc8cbSesaxe 124496ccc8cbSesaxe if logging : 124596ccc8cbSesaxe log.close() 124696ccc8cbSesaxe 124796ccc8cbSesaxe sys.exit(ret) 124896ccc8cbSesaxe 124996ccc8cbSesaxedef main() : 125096ccc8cbSesaxe 125196ccc8cbSesaxe # Log file handle 125296ccc8cbSesaxe global log 125396ccc8cbSesaxe 125496ccc8cbSesaxe # Globals relating to command line options 125596ccc8cbSesaxe global logging, vdiffs, reportAllSects 125696ccc8cbSesaxe 125796ccc8cbSesaxe # Named temporary files / directories 1258598cc7dfSVladimir Kotal global tmpDir1, tmpDir2 125996ccc8cbSesaxe 126096ccc8cbSesaxe # Command paths 126196ccc8cbSesaxe global lintdump_cmd, elfdump_cmd, dump_cmd, dis_cmd, od_cmd, diff_cmd, sqlite_cmd 126296ccc8cbSesaxe 126396ccc8cbSesaxe # Default search path 126496ccc8cbSesaxe global wsdiff_path 126596ccc8cbSesaxe 126696ccc8cbSesaxe # Essentially "uname -p" 126796ccc8cbSesaxe global arch 126896ccc8cbSesaxe 1269598cc7dfSVladimir Kotal # changed files for worker thread processing 1270598cc7dfSVladimir Kotal global changedFiles 1271598cc7dfSVladimir Kotal global baseRoot 1272598cc7dfSVladimir Kotal global ptchRoot 1273598cc7dfSVladimir Kotal 1274598cc7dfSVladimir Kotal # Sort the list of files from a temporary file 1275598cc7dfSVladimir Kotal global sorted 1276598cc7dfSVladimir Kotal global differentFiles 1277598cc7dfSVladimir Kotal 1278598cc7dfSVladimir Kotal # Debugging indicator 1279598cc7dfSVladimir Kotal global debugon 1280598cc7dfSVladimir Kotal 128196ccc8cbSesaxe # Some globals need to be initialized 1282598cc7dfSVladimir Kotal debugon = logging = vdiffs = reportAllSects = sorted = False 128396ccc8cbSesaxe 128496ccc8cbSesaxe 128596ccc8cbSesaxe # Process command line arguments 128696ccc8cbSesaxe # Return values are returned from args() in alpha order 128796ccc8cbSesaxe # (Yes, python functions can return multiple values (ewww)) 128896ccc8cbSesaxe # Note that args() also set the globals: 128996ccc8cbSesaxe # logging to True if verbose logging (to a file) was enabled 129096ccc8cbSesaxe # vdiffs to True if logged differences aren't to be truncated 129196ccc8cbSesaxe # reportAllSects to True if all ELF section differences are to be reported 129296ccc8cbSesaxe # 129396ccc8cbSesaxe baseRoot, fileNamesFile, localTools, ptchRoot, results = args() 129496ccc8cbSesaxe 129596ccc8cbSesaxe # 129696ccc8cbSesaxe # Set up the results/log file 129796ccc8cbSesaxe # 129896ccc8cbSesaxe if logging : 129996ccc8cbSesaxe try: 130096ccc8cbSesaxe log = open(results, "w") 130196ccc8cbSesaxe except: 130296ccc8cbSesaxe logging = False 130396ccc8cbSesaxe error("failed to open log file: " + log) 130496ccc8cbSesaxe sys.exit(1) 130596ccc8cbSesaxe 1306ccac5ae3SJosef 'Jeff' Sipek dateTimeStr= "# %04d-%02d-%02d at %02d:%02d:%02d" % time.localtime()[:6] 130796ccc8cbSesaxe v_info("# This file was produced by wsdiff") 130896ccc8cbSesaxe v_info(dateTimeStr) 130996ccc8cbSesaxe 1310598cc7dfSVladimir Kotal # Changed files (used only for the sorted case) 1311598cc7dfSVladimir Kotal if sorted : 1312598cc7dfSVladimir Kotal differentFiles = [] 1313598cc7dfSVladimir Kotal 131496ccc8cbSesaxe # 131596ccc8cbSesaxe # Build paths to the tools required tools 131696ccc8cbSesaxe # 131796ccc8cbSesaxe # Try to look for tools in $SRC/tools if the "-t" option 131896ccc8cbSesaxe # was specified 131996ccc8cbSesaxe # 13202b15649cSAndy Fiddaman rc, arch = getoutput("uname -p") 13212b15649cSAndy Fiddaman arch = arch.rstrip() 132296ccc8cbSesaxe if localTools : 132396ccc8cbSesaxe try: 132496ccc8cbSesaxe src = os.environ['SRC'] 132596ccc8cbSesaxe except: 132696ccc8cbSesaxe error("-t specified, but $SRC not set. Cannot find $SRC/tools") 132796ccc8cbSesaxe src = "" 132896ccc8cbSesaxe if len(src) > 0 : 132996ccc8cbSesaxe wsdiff_path.insert(0, src + "/tools/proto/opt/onbld/bin") 133096ccc8cbSesaxe 133196ccc8cbSesaxe lintdump_cmd = find_tool("lintdump") 133296ccc8cbSesaxe elfdump_cmd = find_tool("elfdump") 133396ccc8cbSesaxe dump_cmd = find_tool("dump") 133496ccc8cbSesaxe od_cmd = find_tool("od") 133596ccc8cbSesaxe dis_cmd = find_tool("dis") 133696ccc8cbSesaxe diff_cmd = find_tool("diff") 133796ccc8cbSesaxe sqlite_cmd = find_tool("sqlite") 133896ccc8cbSesaxe 133996ccc8cbSesaxe # 1340598cc7dfSVladimir Kotal # Set resource limit for number of open files as high as possible. 1341598cc7dfSVladimir Kotal # This might get handy with big number of threads. 1342598cc7dfSVladimir Kotal # 1343598cc7dfSVladimir Kotal (nofile_soft, nofile_hard) = resource.getrlimit(resource.RLIMIT_NOFILE) 1344598cc7dfSVladimir Kotal try: 1345598cc7dfSVladimir Kotal resource.setrlimit(resource.RLIMIT_NOFILE, 1346598cc7dfSVladimir Kotal (nofile_hard, nofile_hard)) 1347598cc7dfSVladimir Kotal except: 1348598cc7dfSVladimir Kotal error("cannot set resource limits for number of open files") 1349598cc7dfSVladimir Kotal sys.exit(1) 1350598cc7dfSVladimir Kotal 1351598cc7dfSVladimir Kotal # 135296ccc8cbSesaxe # validate the base and patch paths 135396ccc8cbSesaxe # 135496ccc8cbSesaxe if baseRoot[-1] != '/' : 135596ccc8cbSesaxe baseRoot += '/' 135696ccc8cbSesaxe 135796ccc8cbSesaxe if ptchRoot[-1] != '/' : 135896ccc8cbSesaxe ptchRoot += '/' 135996ccc8cbSesaxe 136096ccc8cbSesaxe if not os.path.exists(baseRoot) : 136196ccc8cbSesaxe error("old proto area: " + baseRoot + " does not exist") 136296ccc8cbSesaxe sys.exit(1) 136396ccc8cbSesaxe 136496ccc8cbSesaxe if not os.path.exists(ptchRoot) : 136500ff2212SAndy Fiddaman error("new proto area: " + ptchRoot + " does not exist") 136696ccc8cbSesaxe sys.exit(1) 136796ccc8cbSesaxe 136896ccc8cbSesaxe # 136996ccc8cbSesaxe # log some information identifying the run 137096ccc8cbSesaxe # 137196ccc8cbSesaxe v_info("Old proto area: " + baseRoot) 137296ccc8cbSesaxe v_info("New proto area: " + ptchRoot) 137396ccc8cbSesaxe v_info("Results file: " + results + "\n") 137496ccc8cbSesaxe 137596ccc8cbSesaxe # 137696ccc8cbSesaxe # Set up the temporary directories / files 137796ccc8cbSesaxe # Could use python's tmpdir routines, but these should 137896ccc8cbSesaxe # be easier to identify / keep around for debugging 137996ccc8cbSesaxe pid = os.getpid() 138096ccc8cbSesaxe tmpDir1 = "/tmp/wsdiff_tmp1_" + str(pid) + "/" 138196ccc8cbSesaxe tmpDir2 = "/tmp/wsdiff_tmp2_" + str(pid) + "/" 1382598cc7dfSVladimir Kotal try: 138396ccc8cbSesaxe os.makedirs(tmpDir1) 138400ff2212SAndy Fiddaman except OSError as e: 1385598cc7dfSVladimir Kotal error("main: makedir failed %s" % e) 1386598cc7dfSVladimir Kotal try: 138796ccc8cbSesaxe os.makedirs(tmpDir2) 138800ff2212SAndy Fiddaman except OSError as e: 1389598cc7dfSVladimir Kotal error("main: makedir failed %s" % e) 139096ccc8cbSesaxe 139196ccc8cbSesaxe # Derive a catalog of new, deleted, and to-be-compared objects 139296ccc8cbSesaxe # either from the specified base and patch proto areas, or from 139396ccc8cbSesaxe # from an input file list 139496ccc8cbSesaxe newOrDeleted = False 139596ccc8cbSesaxe 139696ccc8cbSesaxe if fileNamesFile != "" : 139796ccc8cbSesaxe changedFiles, newFiles, deletedFiles = \ 139896ccc8cbSesaxe flistCatalog(baseRoot, ptchRoot, fileNamesFile) 139996ccc8cbSesaxe else : 1400598cc7dfSVladimir Kotal changedFiles, newFiles, deletedFiles = \ 1401598cc7dfSVladimir Kotal protoCatalog(baseRoot, ptchRoot) 140296ccc8cbSesaxe 140396ccc8cbSesaxe if len(newFiles) > 0 : 140496ccc8cbSesaxe newOrDeleted = True 140596ccc8cbSesaxe info("\nNew objects found: ") 140696ccc8cbSesaxe 1407598cc7dfSVladimir Kotal if sorted : 1408598cc7dfSVladimir Kotal newFiles.sort() 140996ccc8cbSesaxe for fn in newFiles : 141096ccc8cbSesaxe info(fnFormat(fn)) 141196ccc8cbSesaxe 141296ccc8cbSesaxe if len(deletedFiles) > 0 : 141396ccc8cbSesaxe newOrDeleted = True 141496ccc8cbSesaxe info("\nObjects removed: ") 141596ccc8cbSesaxe 1416598cc7dfSVladimir Kotal if sorted : 1417598cc7dfSVladimir Kotal deletedFiles.sort() 141896ccc8cbSesaxe for fn in deletedFiles : 141996ccc8cbSesaxe info(fnFormat(fn)) 142096ccc8cbSesaxe 142196ccc8cbSesaxe if newOrDeleted : 1422598cc7dfSVladimir Kotal info("\nChanged objects: ") 1423598cc7dfSVladimir Kotal if sorted : 1424598cc7dfSVladimir Kotal debug("The list will appear after the processing is done") 142596ccc8cbSesaxe 142696ccc8cbSesaxe # Here's where all the heavy lifting happens 142796ccc8cbSesaxe # Perform a comparison on each object appearing in 142896ccc8cbSesaxe # both proto areas. compareOneFile will examine the 142996ccc8cbSesaxe # file types of each object, and will vector off to 143096ccc8cbSesaxe # the appropriate comparison routine, where the compare 143196ccc8cbSesaxe # will happen, and any differences will be reported / logged 143296ccc8cbSesaxe 1433598cc7dfSVladimir Kotal # determine maximum number of worker threads by using 1434598cc7dfSVladimir Kotal # DMAKE_MAX_JOBS environment variable set by nightly(1) 1435598cc7dfSVladimir Kotal # or get number of CPUs in the system 1436598cc7dfSVladimir Kotal try: 1437598cc7dfSVladimir Kotal max_threads = int(os.environ['DMAKE_MAX_JOBS']) 1438598cc7dfSVladimir Kotal except: 1439598cc7dfSVladimir Kotal max_threads = os.sysconf("SC_NPROCESSORS_ONLN") 1440598cc7dfSVladimir Kotal # If we cannot get number of online CPUs in the system 1441598cc7dfSVladimir Kotal # run unparallelized otherwise bump the number up 20% 1442598cc7dfSVladimir Kotal # to achieve best results. 1443598cc7dfSVladimir Kotal if max_threads == -1 : 1444598cc7dfSVladimir Kotal max_threads = 1 1445598cc7dfSVladimir Kotal else : 14463eeb7968SAndy Fiddaman max_threads += int(max_threads/5) 1447598cc7dfSVladimir Kotal 1448598cc7dfSVladimir Kotal # Set signal handler to attempt graceful exit 1449598cc7dfSVladimir Kotal debug("Setting signal handler") 1450598cc7dfSVladimir Kotal signal.signal( signal.SIGINT, discontinue_processing ) 1451598cc7dfSVladimir Kotal 1452598cc7dfSVladimir Kotal # Create and unleash the threads 1453598cc7dfSVladimir Kotal # Only at most max_threads must be running at any moment 1454598cc7dfSVladimir Kotal mythreads = [] 1455598cc7dfSVladimir Kotal debug("Spawning " + str(max_threads) + " threads"); 1456598cc7dfSVladimir Kotal for i in range(max_threads) : 1457598cc7dfSVladimir Kotal thread = workerThread() 1458598cc7dfSVladimir Kotal mythreads.append(thread) 1459598cc7dfSVladimir Kotal mythreads[i].start() 1460598cc7dfSVladimir Kotal 1461598cc7dfSVladimir Kotal # Wait for the threads to finish and do cleanup if interrupted 1462598cc7dfSVladimir Kotal debug("Waiting for the threads to finish") 1463598cc7dfSVladimir Kotal while True: 1464*af92a18eSAndy Fiddaman if not True in [thread.is_alive() for thread in mythreads]: 1465598cc7dfSVladimir Kotal break 1466598cc7dfSVladimir Kotal else: 1467598cc7dfSVladimir Kotal # Some threads are still going 1468598cc7dfSVladimir Kotal time.sleep(1) 1469598cc7dfSVladimir Kotal 1470598cc7dfSVladimir Kotal # Interrupted by SIGINT 1471598cc7dfSVladimir Kotal if keep_processing == False : 1472598cc7dfSVladimir Kotal cleanup(1) 1473598cc7dfSVladimir Kotal 1474598cc7dfSVladimir Kotal # If the list of differences was sorted it is stored in an array 1475598cc7dfSVladimir Kotal if sorted : 1476598cc7dfSVladimir Kotal differentFiles.sort() 1477598cc7dfSVladimir Kotal for f in differentFiles : 1478598cc7dfSVladimir Kotal info(fnFormat(f)) 147996ccc8cbSesaxe 148096ccc8cbSesaxe # We're done, cleanup. 148196ccc8cbSesaxe cleanup(0) 148296ccc8cbSesaxe 148396ccc8cbSesaxeif __name__ == '__main__' : 148496ccc8cbSesaxe try: 148596ccc8cbSesaxe main() 148696ccc8cbSesaxe except KeyboardInterrupt : 148796ccc8cbSesaxe cleanup(1); 148896ccc8cbSesaxe 1489