xref: /titanic_53/usr/src/tools/scripts/wsdiff.py (revision ea7dde8f91382380277a93fb6f69595234291260)
193be19b9SAndy 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.
2393be19b9SAndy Fiddaman# Copyright 2018 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#
4996ccc8cbSesaxe# By invoking nightly(1) with the -w option to NIGHTLY_FLAGS, nightly(1) will use
5096ccc8cbSesaxe# 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#
5896ccc8cbSesaxe# Use the -i option in conjunction with -v and -V to dive deeper and have wsdiff(1)
5996ccc8cbSesaxe# 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
7393be19b9SAndy Fiddamanfrom __future__ import print_function
74*ea7dde8fSAndy 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 *
78*ea7dde8fSAndy Fiddamanfrom subprocess import Popen, PIPE
7996ccc8cbSesaxe
8096ccc8cbSesaxe# Human readable diffs truncated by default if longer than this
8196ccc8cbSesaxe# Specifying -v on the command line will override
8296ccc8cbSesaxediffs_sz_thresh = 4096
8396ccc8cbSesaxe
84598cc7dfSVladimir Kotal# Lock name	 Provides exclusive access to
85598cc7dfSVladimir Kotal# --------------+------------------------------------------------
86598cc7dfSVladimir Kotal# output_lock	 standard output or temporary file (difference())
87598cc7dfSVladimir Kotal# log_lock	 the results file (log_difference())
88598cc7dfSVladimir Kotal# wset_lock	 changedFiles list (workerThread())
89598cc7dfSVladimir Kotaloutput_lock = threading.Lock()
90598cc7dfSVladimir Kotallog_lock = threading.Lock()
91598cc7dfSVladimir Kotalwset_lock = threading.Lock()
92598cc7dfSVladimir Kotal
93598cc7dfSVladimir Kotal# Variable for thread control
94598cc7dfSVladimir Kotalkeep_processing = True
95598cc7dfSVladimir Kotal
9696ccc8cbSesaxe# Default search path for wsdiff
9796ccc8cbSesaxewsdiff_path = [ "/usr/bin",
9896ccc8cbSesaxe		"/usr/ccs/bin",
9996ccc8cbSesaxe		"/lib/svc/bin",
10096ccc8cbSesaxe		"/opt/onbld/bin" ]
10196ccc8cbSesaxe
10296ccc8cbSesaxe# These are objects that wsdiff will notice look different, but will not report.
10396ccc8cbSesaxe# Existence of an exceptions list, and adding things here is *dangerous*,
10496ccc8cbSesaxe# and therefore the *only* reasons why anything would be listed here is because
10596ccc8cbSesaxe# the objects do not build deterministically, yet we *cannot* fix this.
10696ccc8cbSesaxe#
10796ccc8cbSesaxe# These perl libraries use __DATE__ and therefore always look different.
10896ccc8cbSesaxe# Ideally, we would purge use the use of __DATE__ from the source, but because
10996ccc8cbSesaxe# this is source we wish to distribute with Solaris "unchanged", we cannot modify.
11096ccc8cbSesaxe#
11193be19b9SAndy Fiddamanwsdiff_exceptions = [
11293be19b9SAndy Fiddaman	"usr/perl5/5.8.4/lib/sun4-solaris-64int/CORE/libperl.so.1",
11396ccc8cbSesaxe	"usr/perl5/5.6.1/lib/sun4-solaris-64int/CORE/libperl.so.1",
11496ccc8cbSesaxe	"usr/perl5/5.8.4/lib/i86pc-solaris-64int/CORE/libperl.so.1",
11596ccc8cbSesaxe	"usr/perl5/5.6.1/lib/i86pc-solaris-64int/CORE/libperl.so.1"
11696ccc8cbSesaxe]
11796ccc8cbSesaxe
118*ea7dde8fSAndy Fiddamandef getoutput(cmd):
119*ea7dde8fSAndy Fiddaman	p = Popen(cmd, shell=True, stdout=PIPE)
120*ea7dde8fSAndy Fiddaman	output, x = p.communicate()
121*ea7dde8fSAndy Fiddaman	return (p.returncode, output.decode(errors='replace'))
122*ea7dde8fSAndy Fiddaman
12396ccc8cbSesaxe#####
12496ccc8cbSesaxe# Logging routines
12596ccc8cbSesaxe#
12696ccc8cbSesaxe
127598cc7dfSVladimir Kotal# Debug message to be printed to the screen, and the log file
128598cc7dfSVladimir Kotaldef debug(msg) :
129598cc7dfSVladimir Kotal
130598cc7dfSVladimir Kotal	# Add prefix to highlight debugging message
131598cc7dfSVladimir Kotal	msg = "## " + msg
132598cc7dfSVladimir Kotal	if debugon :
133598cc7dfSVladimir Kotal		output_lock.acquire()
13493be19b9SAndy Fiddaman		print(msg)
135598cc7dfSVladimir Kotal		sys.stdout.flush()
136598cc7dfSVladimir Kotal		output_lock.release()
137598cc7dfSVladimir Kotal		if logging :
138598cc7dfSVladimir Kotal			log_lock.acquire()
13993be19b9SAndy Fiddaman			print(msg, file=log)
140598cc7dfSVladimir Kotal			log.flush()
141598cc7dfSVladimir Kotal			log_lock.release()
142598cc7dfSVladimir Kotal
14396ccc8cbSesaxe# Informational message to be printed to the screen, and the log file
14496ccc8cbSesaxedef info(msg) :
14596ccc8cbSesaxe
146598cc7dfSVladimir Kotal	output_lock.acquire()
14793be19b9SAndy Fiddaman	print(msg)
14896ccc8cbSesaxe	sys.stdout.flush()
149598cc7dfSVladimir Kotal	output_lock.release()
150598cc7dfSVladimir Kotal	if logging :
151598cc7dfSVladimir Kotal		log_lock.acquire()
15293be19b9SAndy Fiddaman		print(msg, file=log)
153598cc7dfSVladimir Kotal		log.flush()
154598cc7dfSVladimir Kotal		log_lock.release()
15596ccc8cbSesaxe
15696ccc8cbSesaxe# Error message to be printed to the screen, and the log file
15796ccc8cbSesaxedef error(msg) :
15896ccc8cbSesaxe
159598cc7dfSVladimir Kotal	output_lock.acquire()
16093be19b9SAndy Fiddaman	print("ERROR: " + msg, file=sys.stderr)
16196ccc8cbSesaxe	sys.stderr.flush()
162598cc7dfSVladimir Kotal	output_lock.release()
16396ccc8cbSesaxe	if logging :
164598cc7dfSVladimir Kotal		log_lock.acquire()
16593be19b9SAndy Fiddaman		print("ERROR: " + msg, file=log)
16696ccc8cbSesaxe		log.flush()
167598cc7dfSVladimir Kotal		log_lock.release()
16896ccc8cbSesaxe
16996ccc8cbSesaxe# Informational message to be printed only to the log, if there is one.
17096ccc8cbSesaxedef v_info(msg) :
17196ccc8cbSesaxe
17296ccc8cbSesaxe	if logging :
173598cc7dfSVladimir Kotal		log_lock.acquire()
17493be19b9SAndy Fiddaman		print(msg, file=log)
17596ccc8cbSesaxe		log.flush()
176598cc7dfSVladimir Kotal		log_lock.release()
17796ccc8cbSesaxe
17896ccc8cbSesaxe#
17996ccc8cbSesaxe# Flag a detected file difference
18096ccc8cbSesaxe# Display the fileName to stdout, and log the difference
18196ccc8cbSesaxe#
18296ccc8cbSesaxedef difference(f, dtype, diffs) :
18396ccc8cbSesaxe
18496ccc8cbSesaxe	if f in wsdiff_exceptions :
18596ccc8cbSesaxe		return
18696ccc8cbSesaxe
187598cc7dfSVladimir Kotal	output_lock.acquire()
188598cc7dfSVladimir Kotal	if sorted :
189598cc7dfSVladimir Kotal		differentFiles.append(f)
190598cc7dfSVladimir Kotal	else:
19193be19b9SAndy Fiddaman		print(f)
19296ccc8cbSesaxe		sys.stdout.flush()
193598cc7dfSVladimir Kotal	output_lock.release()
19496ccc8cbSesaxe
19596ccc8cbSesaxe	log_difference(f, dtype, diffs)
19696ccc8cbSesaxe
19796ccc8cbSesaxe#
19896ccc8cbSesaxe# Do the actual logging of the difference to the results file
19996ccc8cbSesaxe#
20096ccc8cbSesaxedef log_difference(f, dtype, diffs) :
201598cc7dfSVladimir Kotal
20296ccc8cbSesaxe	if logging :
203598cc7dfSVladimir Kotal		log_lock.acquire()
20493be19b9SAndy Fiddaman		print(f, file=log)
20593be19b9SAndy Fiddaman		print("NOTE: " + dtype + " difference detected.", file=log)
20696ccc8cbSesaxe
20796ccc8cbSesaxe		difflen = len(diffs)
20896ccc8cbSesaxe		if difflen > 0 :
20993be19b9SAndy Fiddaman			print('', file=log)
21096ccc8cbSesaxe
21196ccc8cbSesaxe			if not vdiffs and difflen > diffs_sz_thresh :
21293be19b9SAndy Fiddaman				print(diffs[:diffs_sz_thresh], file=log)
21393be19b9SAndy Fiddaman				print("... truncated due to length: " +
21493be19b9SAndy Fiddaman				      "use -v to override ...", file=log)
21596ccc8cbSesaxe			else :
21693be19b9SAndy Fiddaman				print(diffs, file=log)
21793be19b9SAndy Fiddaman			print('\n', file=log)
21896ccc8cbSesaxe		log.flush()
219598cc7dfSVladimir Kotal		log_lock.release()
22096ccc8cbSesaxe
22196ccc8cbSesaxe
22296ccc8cbSesaxe#####
22396ccc8cbSesaxe# diff generating routines
22496ccc8cbSesaxe#
22596ccc8cbSesaxe
22696ccc8cbSesaxe#
22796ccc8cbSesaxe# Return human readable diffs from two temporary files
22896ccc8cbSesaxe#
22996ccc8cbSesaxedef diffFileData(tmpf1, tmpf2) :
23096ccc8cbSesaxe
231598cc7dfSVladimir Kotal	binaries = False
232598cc7dfSVladimir Kotal
23396ccc8cbSesaxe	# Filter the data through od(1) if the data is detected
23496ccc8cbSesaxe	# as being binary
23596ccc8cbSesaxe	if isBinary(tmpf1) or isBinary(tmpf2) :
236598cc7dfSVladimir Kotal		binaries = True
23796ccc8cbSesaxe		tmp_od1 = tmpf1 + ".od"
23896ccc8cbSesaxe		tmp_od2 = tmpf2 + ".od"
23996ccc8cbSesaxe
24096ccc8cbSesaxe		cmd = od_cmd + " -c -t x4" + " " + tmpf1 + " > " + tmp_od1
24196ccc8cbSesaxe		os.system(cmd)
24296ccc8cbSesaxe		cmd = od_cmd + " -c -t x4" + " " + tmpf2 + " > " + tmp_od2
24396ccc8cbSesaxe		os.system(cmd)
24496ccc8cbSesaxe
24596ccc8cbSesaxe		tmpf1 = tmp_od1
24696ccc8cbSesaxe		tmpf2 = tmp_od2
24796ccc8cbSesaxe
248598cc7dfSVladimir Kotal	try:
249*ea7dde8fSAndy Fiddaman		rc, data = getoutput(diff_cmd + " " + tmpf1 + " " + tmpf2)
250598cc7dfSVladimir Kotal		# Remove the temp files as we no longer need them.
251598cc7dfSVladimir Kotal		if binaries :
252598cc7dfSVladimir Kotal			try:
253598cc7dfSVladimir Kotal				os.unlink(tmp_od1)
25493be19b9SAndy Fiddaman			except OSError as e:
255598cc7dfSVladimir Kotal				error("diffFileData: unlink failed %s" % e)
256598cc7dfSVladimir Kotal			try:
257598cc7dfSVladimir Kotal				os.unlink(tmp_od2)
25893be19b9SAndy Fiddaman			except OSError as e:
259598cc7dfSVladimir Kotal				error("diffFileData: unlink failed %s" % e)
260598cc7dfSVladimir Kotal	except:
26193be19b9SAndy Fiddaman		error("failed to get output of command: " + diff_cmd + " "
262598cc7dfSVladimir Kotal		    + tmpf1 + " " + tmpf2)
263598cc7dfSVladimir Kotal
264598cc7dfSVladimir Kotal		# Send exception for the failed command up
265598cc7dfSVladimir Kotal		raise
266598cc7dfSVladimir Kotal		return
26796ccc8cbSesaxe
26896ccc8cbSesaxe	return data
26996ccc8cbSesaxe
27096ccc8cbSesaxe#
27196ccc8cbSesaxe# Return human readable diffs betweeen two datasets
27296ccc8cbSesaxe#
273598cc7dfSVladimir Kotaldef diffData(base, ptch, d1, d2) :
27496ccc8cbSesaxe
275598cc7dfSVladimir Kotal	t = threading.currentThread()
276598cc7dfSVladimir Kotal	tmpFile1 = tmpDir1 + os.path.basename(base) + t.getName()
277598cc7dfSVladimir Kotal	tmpFile2 = tmpDir2 + os.path.basename(ptch) + t.getName()
27896ccc8cbSesaxe
27996ccc8cbSesaxe	try:
280*ea7dde8fSAndy Fiddaman		fd1 = io.open(tmpFile1, mode='w', errors='ignore')
28196ccc8cbSesaxe	except:
28296ccc8cbSesaxe		error("failed to open: " + tmpFile1)
28396ccc8cbSesaxe		cleanup(1)
284598cc7dfSVladimir Kotal
28596ccc8cbSesaxe	try:
286*ea7dde8fSAndy Fiddaman		fd2 = io.open(tmpFile2, mode='w', errors='ignore')
28796ccc8cbSesaxe	except:
28896ccc8cbSesaxe		error("failed to open: " + tmpFile2)
28996ccc8cbSesaxe		cleanup(1)
29096ccc8cbSesaxe
29196ccc8cbSesaxe	fd1.write(d1)
29296ccc8cbSesaxe	fd2.write(d2)
29396ccc8cbSesaxe	fd1.close()
29496ccc8cbSesaxe	fd2.close()
29596ccc8cbSesaxe
29696ccc8cbSesaxe	return diffFileData(tmpFile1, tmpFile2)
29796ccc8cbSesaxe
29896ccc8cbSesaxe#####
29996ccc8cbSesaxe# Misc utility functions
30096ccc8cbSesaxe#
30196ccc8cbSesaxe
30296ccc8cbSesaxe# Prune off the leading prefix from string s
30396ccc8cbSesaxedef str_prefix_trunc(s, prefix) :
30496ccc8cbSesaxe	snipLen = len(prefix)
30596ccc8cbSesaxe	return s[snipLen:]
30696ccc8cbSesaxe
30796ccc8cbSesaxe#
30896ccc8cbSesaxe# Prune off leading proto path goo (if there is one) to yield
30996ccc8cbSesaxe# the deliverable's eventual path relative to root
31096ccc8cbSesaxe# e.g. proto.base/root_sparc/usr/src/cmd/prstat => usr/src/cmd/prstat
31196ccc8cbSesaxe#
31296ccc8cbSesaxedef fnFormat(fn) :
31396ccc8cbSesaxe	root_arch_str = "root_" + arch
31496ccc8cbSesaxe
31596ccc8cbSesaxe	pos = fn.find(root_arch_str)
31696ccc8cbSesaxe	if pos == -1 :
31796ccc8cbSesaxe		return fn
31896ccc8cbSesaxe
31996ccc8cbSesaxe	pos = fn.find("/", pos)
32096ccc8cbSesaxe	if pos == -1 :
32196ccc8cbSesaxe		return fn
32296ccc8cbSesaxe
32396ccc8cbSesaxe	return fn[pos + 1:]
32496ccc8cbSesaxe
32596ccc8cbSesaxe#####
32696ccc8cbSesaxe# Usage / argument processing
32796ccc8cbSesaxe#
32896ccc8cbSesaxe
32996ccc8cbSesaxe#
33096ccc8cbSesaxe# Display usage message
33196ccc8cbSesaxe#
33296ccc8cbSesaxedef usage() :
33396ccc8cbSesaxe	sys.stdout.flush()
33493be19b9SAndy Fiddaman	print("""Usage: wsdiff [-dvVst] [-r results ] [-i filelist ] old new
335598cc7dfSVladimir Kotal        -d      Print debug messages about the progress
33696ccc8cbSesaxe        -v      Do not truncate observed diffs in results
33796ccc8cbSesaxe        -V      Log *all* ELF sect diffs vs. logging the first diff found
33896ccc8cbSesaxe        -t      Use onbld tools in $SRC/tools
33996ccc8cbSesaxe        -r      Log results and observed differences
340598cc7dfSVladimir Kotal        -s      Produce sorted list of differences
34193be19b9SAndy Fiddaman        -i      Tell wsdiff which objects to compare via an input file list""",
34293be19b9SAndy Fiddaman	    file=sys.stderr)
34396ccc8cbSesaxe	sys.exit(1)
34496ccc8cbSesaxe
34596ccc8cbSesaxe#
34696ccc8cbSesaxe# Process command line options
34796ccc8cbSesaxe#
34896ccc8cbSesaxedef args() :
34996ccc8cbSesaxe
350598cc7dfSVladimir Kotal	global debugon
35196ccc8cbSesaxe	global logging
35296ccc8cbSesaxe	global vdiffs
35396ccc8cbSesaxe	global reportAllSects
354598cc7dfSVladimir Kotal	global sorted
35596ccc8cbSesaxe
356598cc7dfSVladimir Kotal	validOpts = 'di:r:vVst?'
35796ccc8cbSesaxe
35896ccc8cbSesaxe	baseRoot = ""
35996ccc8cbSesaxe	ptchRoot = ""
36096ccc8cbSesaxe	fileNamesFile = ""
36196ccc8cbSesaxe	results = ""
36296ccc8cbSesaxe	localTools = False
36396ccc8cbSesaxe
36496ccc8cbSesaxe	# getopt.getopt() returns:
36596ccc8cbSesaxe	#	an option/value tuple
36696ccc8cbSesaxe	#	a list of remaining non-option arguments
36796ccc8cbSesaxe	#
36896ccc8cbSesaxe	# A correct wsdiff invocation will have exactly two non option
36996ccc8cbSesaxe	# arguments, the paths to the base (old), ptch (new) proto areas
37096ccc8cbSesaxe	try:
37196ccc8cbSesaxe		optlist, args = getopt.getopt(sys.argv[1:], validOpts)
37293be19b9SAndy Fiddaman	except getopt.error as val:
37396ccc8cbSesaxe		usage()
37496ccc8cbSesaxe
37596ccc8cbSesaxe	if len(args) != 2 :
37696ccc8cbSesaxe		usage();
37796ccc8cbSesaxe
37896ccc8cbSesaxe	for opt,val in optlist :
379598cc7dfSVladimir Kotal		if opt == '-d' :
380598cc7dfSVladimir Kotal			debugon = True
381598cc7dfSVladimir Kotal		elif opt == '-i' :
38296ccc8cbSesaxe			fileNamesFile = val
38396ccc8cbSesaxe		elif opt == '-r' :
38496ccc8cbSesaxe			results = val
38596ccc8cbSesaxe			logging = True
386598cc7dfSVladimir Kotal		elif opt == '-s' :
387598cc7dfSVladimir Kotal			sorted = True
38896ccc8cbSesaxe		elif opt == '-v' :
38996ccc8cbSesaxe			vdiffs = True
39096ccc8cbSesaxe		elif opt == '-V' :
39196ccc8cbSesaxe			reportAllSects = True
39296ccc8cbSesaxe		elif opt == '-t':
39396ccc8cbSesaxe			localTools = True
39496ccc8cbSesaxe		else:
39596ccc8cbSesaxe			usage()
39696ccc8cbSesaxe
39796ccc8cbSesaxe	baseRoot = args[0]
39896ccc8cbSesaxe	ptchRoot = args[1]
39996ccc8cbSesaxe
40096ccc8cbSesaxe	if len(baseRoot) == 0 or len(ptchRoot) == 0 :
40196ccc8cbSesaxe		usage()
40296ccc8cbSesaxe
40396ccc8cbSesaxe	if logging and len(results) == 0 :
40496ccc8cbSesaxe		usage()
40596ccc8cbSesaxe
40696ccc8cbSesaxe	if vdiffs and not logging :
40796ccc8cbSesaxe		error("The -v option requires a results file (-r)")
40896ccc8cbSesaxe		sys.exit(1)
40996ccc8cbSesaxe
41096ccc8cbSesaxe	if reportAllSects and not logging :
41196ccc8cbSesaxe		error("The -V option requires a results file (-r)")
41296ccc8cbSesaxe		sys.exit(1)
41396ccc8cbSesaxe
41496ccc8cbSesaxe	# alphabetical order
41596ccc8cbSesaxe	return	baseRoot, fileNamesFile, localTools, ptchRoot, results
41696ccc8cbSesaxe
41796ccc8cbSesaxe#####
41896ccc8cbSesaxe# File identification
41996ccc8cbSesaxe#
42096ccc8cbSesaxe
42196ccc8cbSesaxe#
42296ccc8cbSesaxe# Identify the file type.
42396ccc8cbSesaxe# If it's not ELF, use the file extension to identify
42496ccc8cbSesaxe# certain file types that require special handling to
42596ccc8cbSesaxe# compare. Otherwise just return a basic "ASCII" type.
42696ccc8cbSesaxe#
42796ccc8cbSesaxedef getTheFileType(f) :
42896ccc8cbSesaxe
42996ccc8cbSesaxe	extensions = { 'a'	:	'ELF Object Archive',
43096ccc8cbSesaxe		       'jar'	:	'Java Archive',
43196ccc8cbSesaxe		       'html'	:	'HTML',
43296ccc8cbSesaxe		       'ln'	:	'Lint Library',
43396ccc8cbSesaxe		       'db'	:	'Sqlite Database' }
43496ccc8cbSesaxe
435619b4598Srotondo	try:
43696ccc8cbSesaxe		if os.stat(f)[ST_SIZE] == 0 :
43796ccc8cbSesaxe			return 'ASCII'
438619b4598Srotondo	except:
439619b4598Srotondo		error("failed to stat " + f)
440619b4598Srotondo		return 'Error'
44196ccc8cbSesaxe
44296ccc8cbSesaxe	if isELF(f) == 1 :
44396ccc8cbSesaxe		return 'ELF'
44496ccc8cbSesaxe
44596ccc8cbSesaxe	fnamelist = f.split('.')
44696ccc8cbSesaxe	if len(fnamelist) > 1 :	# Test the file extension
44796ccc8cbSesaxe		extension = fnamelist[-1]
44896ccc8cbSesaxe		if extension in extensions.keys():
44996ccc8cbSesaxe			return extensions[extension]
45096ccc8cbSesaxe
45196ccc8cbSesaxe	return 'ASCII'
45296ccc8cbSesaxe
45396ccc8cbSesaxe#
45496ccc8cbSesaxe# Return non-zero if "f" is an ELF file
45596ccc8cbSesaxe#
456*ea7dde8fSAndy Fiddamanelfmagic = b'\177ELF'
45796ccc8cbSesaxedef isELF(f) :
45896ccc8cbSesaxe	try:
459*ea7dde8fSAndy Fiddaman		with io.open(f, mode='rb') as fd:
46096ccc8cbSesaxe			magic = fd.read(len(elfmagic))
46196ccc8cbSesaxe
46296ccc8cbSesaxe		if magic == elfmagic :
46396ccc8cbSesaxe			return 1
464*ea7dde8fSAndy Fiddaman	except:
465*ea7dde8fSAndy Fiddaman		pass
46696ccc8cbSesaxe	return 0
46796ccc8cbSesaxe
46896ccc8cbSesaxe#
46996ccc8cbSesaxe# Return non-zero is "f" is binary.
47096ccc8cbSesaxe# Consider the file to be binary if it contains any null characters
47196ccc8cbSesaxe#
47296ccc8cbSesaxedef isBinary(f) :
47396ccc8cbSesaxe	try:
474*ea7dde8fSAndy Fiddaman		with io.open(f, mode='rb') as fd:
47596ccc8cbSesaxe			s = fd.read()
47696ccc8cbSesaxe
477*ea7dde8fSAndy Fiddaman		if s.find(b'\0') == -1 :
47896ccc8cbSesaxe			return 0
479*ea7dde8fSAndy Fiddaman	except:
480*ea7dde8fSAndy Fiddaman		pass
48196ccc8cbSesaxe	return 1
48296ccc8cbSesaxe
48396ccc8cbSesaxe#####
48496ccc8cbSesaxe# Directory traversal and file finding
48596ccc8cbSesaxe#
48696ccc8cbSesaxe
48796ccc8cbSesaxe#
48896ccc8cbSesaxe# Return a sorted list of files found under the specified directory
48996ccc8cbSesaxe#
49096ccc8cbSesaxedef findFiles(d) :
49196ccc8cbSesaxe	for path, subdirs, files in os.walk(d) :
49296ccc8cbSesaxe		files.sort()
49396ccc8cbSesaxe		for name in files :
49496ccc8cbSesaxe			yield os.path.join(path, name)
49596ccc8cbSesaxe
49696ccc8cbSesaxe#
49796ccc8cbSesaxe# Examine all files in base, ptch
49896ccc8cbSesaxe#
49996ccc8cbSesaxe# Return a list of files appearing in both proto areas,
50096ccc8cbSesaxe# a list of new files (files found only in ptch) and
50196ccc8cbSesaxe# a list of deleted files (files found only in base)
50296ccc8cbSesaxe#
50396ccc8cbSesaxedef protoCatalog(base, ptch) :
504598cc7dfSVladimir Kotal
50596ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
50696ccc8cbSesaxe	ptchList = []		# List of file in patch proto area
50796ccc8cbSesaxe
50896ccc8cbSesaxe	newFiles = []		# New files detected
50996ccc8cbSesaxe	deletedFiles = []	# Deleted files
51096ccc8cbSesaxe
511598cc7dfSVladimir Kotal	debug("Getting the list of files in the base area");
51296ccc8cbSesaxe	baseFilesList = list(findFiles(base))
51396ccc8cbSesaxe	baseStringLength = len(base)
514598cc7dfSVladimir Kotal	debug("Found " + str(len(baseFilesList)) + " files")
51596ccc8cbSesaxe
516598cc7dfSVladimir Kotal	debug("Getting the list of files in the patch area");
51796ccc8cbSesaxe	ptchFilesList = list(findFiles(ptch))
51896ccc8cbSesaxe	ptchStringLength = len(ptch)
519598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchFilesList)) + " files")
52096ccc8cbSesaxe
52196ccc8cbSesaxe	# Inventory files in the base proto area
522598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the base area");
52396ccc8cbSesaxe	for fn in baseFilesList :
52496ccc8cbSesaxe		if os.path.islink(fn) :
52596ccc8cbSesaxe			continue
52696ccc8cbSesaxe
52796ccc8cbSesaxe		fileName = fn[baseStringLength:]
52896ccc8cbSesaxe		compFiles.append(fileName)
529598cc7dfSVladimir Kotal	debug("Found " + str(len(compFiles)) + " files")
53096ccc8cbSesaxe
53196ccc8cbSesaxe	# Inventory files in the patch proto area
532598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the patch area");
53396ccc8cbSesaxe	for fn in ptchFilesList :
53496ccc8cbSesaxe		if os.path.islink(fn) :
53596ccc8cbSesaxe			continue
53696ccc8cbSesaxe
53796ccc8cbSesaxe		fileName = fn[ptchStringLength:]
53896ccc8cbSesaxe		ptchList.append(fileName)
539598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchList)) + " files")
54096ccc8cbSesaxe
54196ccc8cbSesaxe	# Deleted files appear in the base area, but not the patch area
542598cc7dfSVladimir Kotal	debug("Searching for deleted files by comparing the lists")
54396ccc8cbSesaxe	for fileName in compFiles :
54496ccc8cbSesaxe		if not fileName in ptchList :
54596ccc8cbSesaxe			deletedFiles.append(fileName)
546598cc7dfSVladimir Kotal	debug("Found " + str(len(deletedFiles)) + " deleted files")
54796ccc8cbSesaxe
54896ccc8cbSesaxe	# Eliminate "deleted" files from the list of objects appearing
54996ccc8cbSesaxe	# in both the base and patch proto areas
550598cc7dfSVladimir Kotal	debug("Eliminating deleted files from the list of objects")
55196ccc8cbSesaxe	for fileName in deletedFiles :
55296ccc8cbSesaxe		try:
55396ccc8cbSesaxe			compFiles.remove(fileName)
55496ccc8cbSesaxe		except:
55596ccc8cbSesaxe			error("filelist.remove() failed")
55693be19b9SAndy Fiddaman	debug("List for comparison reduced to " + str(len(compFiles))
557598cc7dfSVladimir Kotal	    + " files")
55896ccc8cbSesaxe
55996ccc8cbSesaxe	# New files appear in the patch area, but not the base
560598cc7dfSVladimir Kotal	debug("Getting the list of newly added files")
56196ccc8cbSesaxe	for fileName in ptchList :
56296ccc8cbSesaxe		if not fileName in compFiles :
56396ccc8cbSesaxe			newFiles.append(fileName)
564598cc7dfSVladimir Kotal	debug("Found " + str(len(newFiles)) + " new files")
56596ccc8cbSesaxe
56696ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
56796ccc8cbSesaxe
56896ccc8cbSesaxe#
56996ccc8cbSesaxe# Examine the files listed in the input file list
57096ccc8cbSesaxe#
57196ccc8cbSesaxe# Return a list of files appearing in both proto areas,
57296ccc8cbSesaxe# a list of new files (files found only in ptch) and
57396ccc8cbSesaxe# a list of deleted files (files found only in base)
57496ccc8cbSesaxe#
57596ccc8cbSesaxedef flistCatalog(base, ptch, flist) :
57696ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
57796ccc8cbSesaxe	newFiles = []		# New files detected
57896ccc8cbSesaxe	deletedFiles = []	# Deleted files
57996ccc8cbSesaxe
58096ccc8cbSesaxe	try:
58196ccc8cbSesaxe		fd = open(flist, "r")
58296ccc8cbSesaxe	except:
58396ccc8cbSesaxe		error("could not open: " + flist)
58496ccc8cbSesaxe		cleanup(1)
58596ccc8cbSesaxe
58696ccc8cbSesaxe	files = []
58796ccc8cbSesaxe	files = fd.readlines()
588598cc7dfSVladimir Kotal	fd.close()
58996ccc8cbSesaxe
59096ccc8cbSesaxe	for f in files :
59196ccc8cbSesaxe		ptch_present = True
59296ccc8cbSesaxe		base_present = True
59396ccc8cbSesaxe
59496ccc8cbSesaxe		if f == '\n' :
59596ccc8cbSesaxe			continue
59696ccc8cbSesaxe
59796ccc8cbSesaxe		# the fileNames have a trailing '\n'
59896ccc8cbSesaxe		f = f.rstrip()
59996ccc8cbSesaxe
60096ccc8cbSesaxe		# The objects in the file list have paths relative
60196ccc8cbSesaxe		# to $ROOT or to the base/ptch directory specified on
60296ccc8cbSesaxe		# the command line.
60396ccc8cbSesaxe		# If it's relative to $ROOT, we'll need to add back the
60496ccc8cbSesaxe		# root_`uname -p` goo we stripped off in fnFormat()
60596ccc8cbSesaxe		if os.path.exists(base + f) :
60696ccc8cbSesaxe			fn = f;
60796ccc8cbSesaxe		elif os.path.exists(base + "root_" + arch + "/" + f) :
60896ccc8cbSesaxe			fn = "root_" + arch + "/" + f
60996ccc8cbSesaxe		else :
61096ccc8cbSesaxe			base_present = False
61196ccc8cbSesaxe
61296ccc8cbSesaxe		if base_present :
61396ccc8cbSesaxe			if not os.path.exists(ptch + fn) :
61496ccc8cbSesaxe				ptch_present = False
61596ccc8cbSesaxe		else :
61696ccc8cbSesaxe			if os.path.exists(ptch + f) :
61796ccc8cbSesaxe				fn = f
61896ccc8cbSesaxe			elif os.path.exists(ptch + "root_" + arch + "/" + f) :
61996ccc8cbSesaxe				fn = "root_" + arch + "/" + f
62096ccc8cbSesaxe			else :
62196ccc8cbSesaxe				ptch_present = False
62296ccc8cbSesaxe
62396ccc8cbSesaxe		if os.path.islink(base + fn) :	# ignore links
62496ccc8cbSesaxe			base_present = False
62596ccc8cbSesaxe		if os.path.islink(ptch + fn) :
62696ccc8cbSesaxe			ptch_present = False
62796ccc8cbSesaxe
62896ccc8cbSesaxe		if base_present and ptch_present :
62996ccc8cbSesaxe			compFiles.append(fn)
63096ccc8cbSesaxe		elif base_present :
63196ccc8cbSesaxe			deletedFiles.append(fn)
63296ccc8cbSesaxe		elif ptch_present :
63396ccc8cbSesaxe			newFiles.append(fn)
63496ccc8cbSesaxe		else :
63593be19b9SAndy Fiddaman			if (os.path.islink(base + fn) and
63693be19b9SAndy Fiddaman			    os.path.islink(ptch + fn)) :
63796ccc8cbSesaxe				continue
63893be19b9SAndy Fiddaman			error(f + " in file list, but not in either tree. " +
639598cc7dfSVladimir Kotal			    "Skipping...")
64096ccc8cbSesaxe
64196ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
64296ccc8cbSesaxe
64396ccc8cbSesaxe
64496ccc8cbSesaxe#
64596ccc8cbSesaxe# Build a fully qualified path to an external tool/utility.
64696ccc8cbSesaxe# Consider the default system locations. For onbld tools, if
64796ccc8cbSesaxe# the -t option was specified, we'll try to use built tools in $SRC tools,
64896ccc8cbSesaxe# and otherwise, we'll fall back on /opt/onbld/
64996ccc8cbSesaxe#
65096ccc8cbSesaxedef find_tool(tool) :
65196ccc8cbSesaxe
65296ccc8cbSesaxe	# First, check what was passed
65396ccc8cbSesaxe	if os.path.exists(tool) :
65496ccc8cbSesaxe		return tool
65596ccc8cbSesaxe
65696ccc8cbSesaxe	# Next try in wsdiff path
65796ccc8cbSesaxe	for pdir in wsdiff_path :
65896ccc8cbSesaxe		location = pdir + "/" + tool
65996ccc8cbSesaxe		if os.path.exists(location) :
66096ccc8cbSesaxe			return location + " "
66196ccc8cbSesaxe
66296ccc8cbSesaxe		location = pdir + "/" + arch + "/" + tool
66396ccc8cbSesaxe		if os.path.exists(location) :
66496ccc8cbSesaxe			return location + " "
66596ccc8cbSesaxe
66696ccc8cbSesaxe	error("Could not find path to: " + tool);
66796ccc8cbSesaxe	sys.exit(1);
66896ccc8cbSesaxe
66996ccc8cbSesaxe
67096ccc8cbSesaxe#####
67196ccc8cbSesaxe# ELF file comparison helper routines
67296ccc8cbSesaxe#
67396ccc8cbSesaxe
67496ccc8cbSesaxe#
67596ccc8cbSesaxe# Return a dictionary of ELF section types keyed by section name
67696ccc8cbSesaxe#
67796ccc8cbSesaxedef get_elfheader(f) :
67896ccc8cbSesaxe
67996ccc8cbSesaxe	header = {}
68096ccc8cbSesaxe
681*ea7dde8fSAndy Fiddaman	rc, hstring = getoutput(elfdump_cmd + " -c " + f)
68296ccc8cbSesaxe
68396ccc8cbSesaxe	if len(hstring) == 0 :
68496ccc8cbSesaxe		error("Failed to dump ELF header for " + f)
685598cc7dfSVladimir Kotal		raise
68696ccc8cbSesaxe		return
68796ccc8cbSesaxe
68896ccc8cbSesaxe	# elfdump(1) dumps the section headers with the section name
68996ccc8cbSesaxe	# following "sh_name:", and the section type following "sh_type:"
69096ccc8cbSesaxe	sections = hstring.split("Section Header")
69196ccc8cbSesaxe	for sect in sections :
69296ccc8cbSesaxe		datap = sect.find("sh_name:");
69396ccc8cbSesaxe		if datap == -1 :
69496ccc8cbSesaxe			continue
69596ccc8cbSesaxe		section = sect[datap:].split()[1]
69696ccc8cbSesaxe		datap = sect.find("sh_type:");
69796ccc8cbSesaxe		if datap == -1 :
69893be19b9SAndy Fiddaman			error("Could not get type for sect: " + section +
69996ccc8cbSesaxe			      " in " + f)
70096ccc8cbSesaxe		sh_type = sect[datap:].split()[2]
70196ccc8cbSesaxe		header[section] = sh_type
70296ccc8cbSesaxe
70396ccc8cbSesaxe	return header
70496ccc8cbSesaxe
70596ccc8cbSesaxe#
70696ccc8cbSesaxe# Extract data in the specified ELF section from the given file
70796ccc8cbSesaxe#
70896ccc8cbSesaxedef extract_elf_section(f, section) :
70996ccc8cbSesaxe
710*ea7dde8fSAndy Fiddaman	rc, data = getoutput(dump_cmd + " -sn " + section + " " + f)
71196ccc8cbSesaxe
71296ccc8cbSesaxe	if len(data) == 0 :
71393be19b9SAndy Fiddaman		error(dump_cmd + "yielded no data on section " + section +
714598cc7dfSVladimir Kotal		    " of " + f)
715598cc7dfSVladimir Kotal		raise
71696ccc8cbSesaxe		return
71796ccc8cbSesaxe
71896ccc8cbSesaxe	# dump(1) displays the file name to start...
71996ccc8cbSesaxe	# get past it to the data itself
72096ccc8cbSesaxe	dbegin = data.find(":") + 1
72196ccc8cbSesaxe	data = data[dbegin:];
72296ccc8cbSesaxe
72396ccc8cbSesaxe	return (data)
72496ccc8cbSesaxe
72596ccc8cbSesaxe#
72696ccc8cbSesaxe# Return a (hopefully meaningful) human readable set of diffs
72796ccc8cbSesaxe# for the specified ELF section between f1 and f2
72896ccc8cbSesaxe#
72996ccc8cbSesaxe# Depending on the section, various means for dumping and diffing
73096ccc8cbSesaxe# the data may be employed.
73196ccc8cbSesaxe#
73296ccc8cbSesaxetext_sections = [ '.text', '.init', '.fini' ]
73396ccc8cbSesaxedef diff_elf_section(f1, f2, section, sh_type) :
73496ccc8cbSesaxe
735598cc7dfSVladimir Kotal	t = threading.currentThread()
736598cc7dfSVladimir Kotal	tmpFile1 = tmpDir1 + os.path.basename(f1) + t.getName()
737598cc7dfSVladimir Kotal	tmpFile2 = tmpDir2 + os.path.basename(f2) + t.getName()
738598cc7dfSVladimir Kotal
73996ccc8cbSesaxe	if (sh_type == "SHT_RELA") : # sh_type == SHT_RELA
74096ccc8cbSesaxe		cmd1 = elfdump_cmd + " -r " + f1 + " > " + tmpFile1
74196ccc8cbSesaxe		cmd2 = elfdump_cmd + " -r " + f2 + " > " + tmpFile2
74296ccc8cbSesaxe	elif (section == ".group") :
74396ccc8cbSesaxe		cmd1 = elfdump_cmd + " -g " + f1 + " > " + tmpFile1
74496ccc8cbSesaxe		cmd2 = elfdump_cmd + " -g " + f2 + " > " + tmpFile2
74596ccc8cbSesaxe	elif (section == ".hash") :
74696ccc8cbSesaxe		cmd1 = elfdump_cmd + " -h " + f1 + " > " + tmpFile1
74796ccc8cbSesaxe		cmd2 = elfdump_cmd + " -h " + f2 + " > " + tmpFile2
74896ccc8cbSesaxe	elif (section == ".dynamic") :
74996ccc8cbSesaxe		cmd1 = elfdump_cmd + " -d " + f1 + " > " + tmpFile1
75096ccc8cbSesaxe		cmd2 = elfdump_cmd + " -d " + f2 + " > " + tmpFile2
75196ccc8cbSesaxe	elif (section == ".got") :
75296ccc8cbSesaxe		cmd1 = elfdump_cmd + " -G " + f1 + " > " + tmpFile1
75396ccc8cbSesaxe		cmd2 = elfdump_cmd + " -G " + f2 + " > " + tmpFile2
75496ccc8cbSesaxe	elif (section == ".SUNW_cap") :
75596ccc8cbSesaxe		cmd1 = elfdump_cmd + " -H " + f1 + " > " + tmpFile1
75696ccc8cbSesaxe		cmd2 = elfdump_cmd + " -H " + f2 + " > " + tmpFile2
75796ccc8cbSesaxe	elif (section == ".interp") :
75896ccc8cbSesaxe		cmd1 = elfdump_cmd + " -i " + f1 + " > " + tmpFile1
75996ccc8cbSesaxe		cmd2 = elfdump_cmd + " -i " + f2 + " > " + tmpFile2
76096ccc8cbSesaxe	elif (section == ".symtab" or section == ".dynsym") :
76193be19b9SAndy Fiddaman		cmd1 = (elfdump_cmd + " -s -N " + section + " " + f1 +
76293be19b9SAndy Fiddaman		    " > " + tmpFile1)
76393be19b9SAndy Fiddaman		cmd2 = (elfdump_cmd + " -s -N " + section + " " + f2 +
76493be19b9SAndy Fiddaman		    " > " + tmpFile2)
76596ccc8cbSesaxe	elif (section in text_sections) :
76696ccc8cbSesaxe		# dis sometimes complains when it hits something it doesn't
76796ccc8cbSesaxe		# know how to disassemble. Just ignore it, as the output
76896ccc8cbSesaxe		# being generated here is human readable, and we've already
76996ccc8cbSesaxe		# correctly flagged the difference.
77093be19b9SAndy Fiddaman		cmd1 = (dis_cmd + " -t " + section + " " + f1 +
77193be19b9SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile1)
77293be19b9SAndy Fiddaman		cmd2 = (dis_cmd + " -t " + section + " " + f2 +
77393be19b9SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile2)
77496ccc8cbSesaxe	else :
77593be19b9SAndy Fiddaman		cmd1 = (elfdump_cmd + " -w " + tmpFile1 + " -N " +
77693be19b9SAndy Fiddaman		       section + " " + f1)
77793be19b9SAndy Fiddaman		cmd2 = (elfdump_cmd + " -w " + tmpFile2 + " -N " +
77893be19b9SAndy Fiddaman		       section + " " + f2)
77996ccc8cbSesaxe
78096ccc8cbSesaxe	os.system(cmd1)
78196ccc8cbSesaxe	os.system(cmd2)
78296ccc8cbSesaxe
78396ccc8cbSesaxe	data = diffFileData(tmpFile1, tmpFile2)
78496ccc8cbSesaxe
785598cc7dfSVladimir Kotal	# remove temp files as we no longer need them
786598cc7dfSVladimir Kotal	try:
787598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
78893be19b9SAndy Fiddaman	except OSError as e:
789598cc7dfSVladimir Kotal		error("diff_elf_section: unlink failed %s" % e)
790598cc7dfSVladimir Kotal	try:
791598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
79293be19b9SAndy Fiddaman	except OSError as e:
793598cc7dfSVladimir Kotal		error("diff_elf_section: unlink failed %s" % e)
794598cc7dfSVladimir Kotal
79596ccc8cbSesaxe	return (data)
79696ccc8cbSesaxe
79796ccc8cbSesaxe#
79896ccc8cbSesaxe# compare the relevant sections of two ELF binaries
79996ccc8cbSesaxe# and report any differences
80096ccc8cbSesaxe#
80196ccc8cbSesaxe# Returns: 1 if any differenes found
80296ccc8cbSesaxe#          0 if no differences found
80396ccc8cbSesaxe#	  -1 on error
80496ccc8cbSesaxe#
80596ccc8cbSesaxe
80696ccc8cbSesaxe# Sections deliberately not considered when comparing two ELF
80796ccc8cbSesaxe# binaries. Differences observed in these sections are not considered
80896ccc8cbSesaxe# significant where patch deliverable identification is concerned.
80996ccc8cbSesaxesections_to_skip = [ ".SUNW_signature",
81096ccc8cbSesaxe		     ".comment",
81196ccc8cbSesaxe		     ".SUNW_ctf",
81296ccc8cbSesaxe		     ".debug",
81396ccc8cbSesaxe		     ".plt",
81496ccc8cbSesaxe		     ".rela.bss",
81596ccc8cbSesaxe		     ".rela.plt",
81696ccc8cbSesaxe		     ".line",
81796ccc8cbSesaxe		     ".note",
818f6a1d796Sesaxe		     ".compcom",
81996ccc8cbSesaxe		     ]
82096ccc8cbSesaxe
82196ccc8cbSesaxesections_preferred = [ ".rodata.str1.8",
82296ccc8cbSesaxe		       ".rodata.str1.1",
82396ccc8cbSesaxe		       ".rodata",
82496ccc8cbSesaxe		       ".data1",
82596ccc8cbSesaxe		       ".data",
82696ccc8cbSesaxe		       ".text",
82796ccc8cbSesaxe		       ]
82896ccc8cbSesaxe
82996ccc8cbSesaxedef compareElfs(base, ptch, quiet) :
83096ccc8cbSesaxe
83196ccc8cbSesaxe	global logging
83296ccc8cbSesaxe
833598cc7dfSVladimir Kotal	try:
83496ccc8cbSesaxe		base_header = get_elfheader(base)
835598cc7dfSVladimir Kotal	except:
836598cc7dfSVladimir Kotal		return
837*ea7dde8fSAndy Fiddaman	sections = list(base_header.keys())
83896ccc8cbSesaxe
839598cc7dfSVladimir Kotal	try:
84096ccc8cbSesaxe		ptch_header = get_elfheader(ptch)
841598cc7dfSVladimir Kotal	except:
842598cc7dfSVladimir Kotal		return
843*ea7dde8fSAndy Fiddaman	e2_only_sections = list(ptch_header.keys())
84496ccc8cbSesaxe
84596ccc8cbSesaxe	e1_only_sections = []
84696ccc8cbSesaxe
84796ccc8cbSesaxe	fileName = fnFormat(base)
84896ccc8cbSesaxe
84996ccc8cbSesaxe	# Derive the list of ELF sections found only in
85096ccc8cbSesaxe	# either e1 or e2.
85196ccc8cbSesaxe	for sect in sections :
85296ccc8cbSesaxe		if not sect in e2_only_sections :
85396ccc8cbSesaxe			e1_only_sections.append(sect)
85496ccc8cbSesaxe		else :
85596ccc8cbSesaxe			e2_only_sections.remove(sect)
85696ccc8cbSesaxe
85796ccc8cbSesaxe	if len(e1_only_sections) > 0 :
85896ccc8cbSesaxe		if quiet :
85996ccc8cbSesaxe			return 1
86096ccc8cbSesaxe
861598cc7dfSVladimir Kotal		data = ""
862598cc7dfSVladimir Kotal		if logging :
86396ccc8cbSesaxe			slist = ""
86496ccc8cbSesaxe			for sect in e1_only_sections :
86596ccc8cbSesaxe				slist = slist + sect + "\t"
86693be19b9SAndy Fiddaman			data = ("ELF sections found in " +
86793be19b9SAndy Fiddaman				base + " but not in " + ptch +
86893be19b9SAndy Fiddaman				"\n\n" + slist)
869598cc7dfSVladimir Kotal
870598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
87196ccc8cbSesaxe		return 1
87296ccc8cbSesaxe
87396ccc8cbSesaxe	if len(e2_only_sections) > 0 :
87496ccc8cbSesaxe		if quiet :
87596ccc8cbSesaxe			return 1
87696ccc8cbSesaxe
877598cc7dfSVladimir Kotal		data = ""
878598cc7dfSVladimir Kotal		if logging :
87996ccc8cbSesaxe			slist = ""
88096ccc8cbSesaxe			for sect in e2_only_sections :
88196ccc8cbSesaxe				slist = slist + sect + "\t"
88293be19b9SAndy Fiddaman			data = ("ELF sections found in " +
88393be19b9SAndy Fiddaman				ptch + " but not in " + base +
88493be19b9SAndy Fiddaman				"\n\n" + slist)
885598cc7dfSVladimir Kotal
886598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
88796ccc8cbSesaxe		return 1
88896ccc8cbSesaxe
88996ccc8cbSesaxe	# Look for preferred sections, and put those at the
89096ccc8cbSesaxe	# top of the list of sections to compare
89196ccc8cbSesaxe	for psect in sections_preferred :
89296ccc8cbSesaxe		if psect in sections :
89396ccc8cbSesaxe			sections.remove(psect)
89496ccc8cbSesaxe			sections.insert(0, psect)
89596ccc8cbSesaxe
89696ccc8cbSesaxe	# Compare ELF sections
89796ccc8cbSesaxe	first_section = True
89896ccc8cbSesaxe	for sect in sections :
89996ccc8cbSesaxe
90096ccc8cbSesaxe		if sect in sections_to_skip :
90196ccc8cbSesaxe			continue
90296ccc8cbSesaxe
903598cc7dfSVladimir Kotal		try:
90496ccc8cbSesaxe			s1 = extract_elf_section(base, sect);
905598cc7dfSVladimir Kotal		except:
906598cc7dfSVladimir Kotal			return
907598cc7dfSVladimir Kotal
908598cc7dfSVladimir Kotal		try:
90996ccc8cbSesaxe			s2 = extract_elf_section(ptch, sect);
910598cc7dfSVladimir Kotal		except:
911598cc7dfSVladimir Kotal			return
91296ccc8cbSesaxe
91396ccc8cbSesaxe		if len(s1) != len (s2) or s1 != s2:
91496ccc8cbSesaxe			if not quiet:
91596ccc8cbSesaxe				sh_type = base_header[sect]
91693be19b9SAndy Fiddaman				data = diff_elf_section(base, ptch,
917598cc7dfSVladimir Kotal							sect, sh_type)
91896ccc8cbSesaxe
91996ccc8cbSesaxe				# If all ELF sections are being reported, then
92096ccc8cbSesaxe				# invoke difference() to flag the file name to
92196ccc8cbSesaxe				# stdout only once. Any other section differences
92296ccc8cbSesaxe				# should be logged to the results file directly
92396ccc8cbSesaxe				if not first_section :
92493be19b9SAndy Fiddaman					log_difference(fileName,
925598cc7dfSVladimir Kotal					    "ELF " + sect, data)
92696ccc8cbSesaxe				else :
92793be19b9SAndy Fiddaman					difference(fileName, "ELF " + sect,
928598cc7dfSVladimir Kotal					    data)
92996ccc8cbSesaxe
93096ccc8cbSesaxe			if not reportAllSects :
93196ccc8cbSesaxe				return 1
93296ccc8cbSesaxe			first_section = False
933598cc7dfSVladimir Kotal
93496ccc8cbSesaxe	return 0
93596ccc8cbSesaxe
93696ccc8cbSesaxe#####
937598cc7dfSVladimir Kotal# recursively remove 2 directories
938598cc7dfSVladimir Kotal#
939598cc7dfSVladimir Kotal# Used for removal of temporary directory strucures (ignores any errors).
940598cc7dfSVladimir Kotal#
941598cc7dfSVladimir Kotaldef clearTmpDirs(dir1, dir2) :
942598cc7dfSVladimir Kotal
943598cc7dfSVladimir Kotal	if os.path.isdir(dir1) > 0 :
944598cc7dfSVladimir Kotal		shutil.rmtree(dir1, True)
945598cc7dfSVladimir Kotal
946598cc7dfSVladimir Kotal	if os.path.isdir(dir2) > 0 :
947598cc7dfSVladimir Kotal		shutil.rmtree(dir2, True)
948598cc7dfSVladimir Kotal
949598cc7dfSVladimir Kotal
950598cc7dfSVladimir Kotal#####
95196ccc8cbSesaxe# Archive object comparison
95296ccc8cbSesaxe#
95396ccc8cbSesaxe# Returns 1 if difference detected
95496ccc8cbSesaxe#         0 if no difference detected
95596ccc8cbSesaxe#        -1 on error
95696ccc8cbSesaxe#
95796ccc8cbSesaxedef compareArchives(base, ptch, fileType) :
95896ccc8cbSesaxe
95996ccc8cbSesaxe	fileName = fnFormat(base)
960598cc7dfSVladimir Kotal	t = threading.currentThread()
961598cc7dfSVladimir Kotal	ArchTmpDir1 = tmpDir1 + os.path.basename(base) + t.getName()
962598cc7dfSVladimir Kotal	ArchTmpDir2 = tmpDir2 + os.path.basename(base) + t.getName()
96396ccc8cbSesaxe
96496ccc8cbSesaxe	#
96596ccc8cbSesaxe	# Be optimistic and first try a straight file compare
96696ccc8cbSesaxe	# as it will allow us to finish up quickly.
967598cc7dfSVladimir Kotal	#
96896ccc8cbSesaxe	if compareBasic(base, ptch, True, fileType) == 0 :
96996ccc8cbSesaxe		return 0
97096ccc8cbSesaxe
971598cc7dfSVladimir Kotal	try:
972598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir1)
97393be19b9SAndy Fiddaman	except OSError as e:
974598cc7dfSVladimir Kotal		error("compareArchives: makedir failed %s" % e)
975598cc7dfSVladimir Kotal		return -1
976598cc7dfSVladimir Kotal	try:
977598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir2)
97893be19b9SAndy Fiddaman	except OSError as e:
979598cc7dfSVladimir Kotal		error("compareArchives: makedir failed %s" % e)
980598cc7dfSVladimir Kotal		return -1
981598cc7dfSVladimir Kotal
98296ccc8cbSesaxe	# copy over the objects to the temp areas, and
98396ccc8cbSesaxe	# unpack them
984598cc7dfSVladimir Kotal	baseCmd = "cp -fp " + base + " " + ArchTmpDir1
985*ea7dde8fSAndy Fiddaman	rc, output = getoutput(baseCmd)
986*ea7dde8fSAndy Fiddaman	if rc != 0:
98796ccc8cbSesaxe		error(baseCmd + " failed: " + output)
988598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
98996ccc8cbSesaxe		return -1
99096ccc8cbSesaxe
991598cc7dfSVladimir Kotal	ptchCmd = "cp -fp " + ptch + " " + ArchTmpDir2
992*ea7dde8fSAndy Fiddaman	rc, output = getoutput(ptchCmd)
993*ea7dde8fSAndy Fiddaman	if rc != 0:
99496ccc8cbSesaxe		error(ptchCmd + " failed: " + output)
995598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
99696ccc8cbSesaxe		return -1
99796ccc8cbSesaxe
998*ea7dde8fSAndy Fiddaman	bname = fileName.split('/')[-1]
99996ccc8cbSesaxe	if fileType == "Java Archive" :
100093be19b9SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "jar xf " + bname +
100193be19b9SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
100293be19b9SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "jar xf " + bname +
100393be19b9SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
100496ccc8cbSesaxe	elif fileType == "ELF Object Archive" :
100593be19b9SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "/usr/ccs/bin/ar x " +
100693be19b9SAndy Fiddaman			  bname + "; rm -f " + bname)
100793be19b9SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "/usr/ccs/bin/ar x " +
100893be19b9SAndy Fiddaman			  bname + "; rm -f " + bname)
100996ccc8cbSesaxe	else :
101096ccc8cbSesaxe		error("unexpected file type: " + fileType)
1011598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
101296ccc8cbSesaxe		return -1
101396ccc8cbSesaxe
101496ccc8cbSesaxe	os.system(baseCmd)
101596ccc8cbSesaxe	os.system(ptchCmd)
101696ccc8cbSesaxe
1017598cc7dfSVladimir Kotal	baseFlist = list(findFiles(ArchTmpDir1))
1018598cc7dfSVladimir Kotal	ptchFlist = list(findFiles(ArchTmpDir2))
101996ccc8cbSesaxe
102096ccc8cbSesaxe	# Trim leading path off base/ptch file lists
102196ccc8cbSesaxe	flist = []
102296ccc8cbSesaxe	for fn in baseFlist :
1023598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir1))
102496ccc8cbSesaxe	baseFlist = flist
102596ccc8cbSesaxe
102696ccc8cbSesaxe	flist = []
102796ccc8cbSesaxe	for fn in ptchFlist :
1028598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir2))
102996ccc8cbSesaxe	ptchFlist = flist
103096ccc8cbSesaxe
103196ccc8cbSesaxe	for fn in ptchFlist :
103296ccc8cbSesaxe		if not fn in baseFlist :
103393be19b9SAndy Fiddaman			difference(fileName, fileType,
103496ccc8cbSesaxe				   fn + " added to " + fileName)
1035598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
103696ccc8cbSesaxe			return 1
103796ccc8cbSesaxe
103896ccc8cbSesaxe	for fn in baseFlist :
103996ccc8cbSesaxe		if not fn in ptchFlist :
104093be19b9SAndy Fiddaman			difference(fileName, fileType,
104196ccc8cbSesaxe				   fn + " removed from " + fileName)
1042598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
104396ccc8cbSesaxe			return 1
104496ccc8cbSesaxe
104593be19b9SAndy Fiddaman		differs = compareOneFile((ArchTmpDir1 + fn),
1046598cc7dfSVladimir Kotal		    (ArchTmpDir2 + fn), True)
104796ccc8cbSesaxe		if differs :
104893be19b9SAndy Fiddaman			difference(fileName, fileType,
104996ccc8cbSesaxe				   fn + " in " + fileName + " differs")
1050598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
105196ccc8cbSesaxe			return 1
1052598cc7dfSVladimir Kotal
1053598cc7dfSVladimir Kotal	clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
105496ccc8cbSesaxe	return 0
105596ccc8cbSesaxe
105696ccc8cbSesaxe#####
105796ccc8cbSesaxe# (Basic) file comparison
105896ccc8cbSesaxe#
105996ccc8cbSesaxe# There's some special case code here for Javadoc HTML files
106096ccc8cbSesaxe#
106196ccc8cbSesaxe# Returns 1 if difference detected
106296ccc8cbSesaxe#         0 if no difference detected
106396ccc8cbSesaxe#        -1 on error
106496ccc8cbSesaxe#
106596ccc8cbSesaxedef compareBasic(base, ptch, quiet, fileType) :
106696ccc8cbSesaxe
106796ccc8cbSesaxe	fileName = fnFormat(base);
106896ccc8cbSesaxe
106996ccc8cbSesaxe	if quiet and os.stat(base)[ST_SIZE] != os.stat(ptch)[ST_SIZE] :
107096ccc8cbSesaxe		return 1
107196ccc8cbSesaxe
107296ccc8cbSesaxe	try:
1073*ea7dde8fSAndy Fiddaman		baseFile = io.open(base, errors='replace')
107496ccc8cbSesaxe	except:
107596ccc8cbSesaxe		error("could not open " + base)
107696ccc8cbSesaxe		return -1
107796ccc8cbSesaxe	try:
1078*ea7dde8fSAndy Fiddaman		ptchFile = io.open(ptch, errors='replace')
107996ccc8cbSesaxe	except:
108096ccc8cbSesaxe		error("could not open " + ptch)
108196ccc8cbSesaxe		return -1
108296ccc8cbSesaxe
108396ccc8cbSesaxe	baseData = baseFile.read()
108496ccc8cbSesaxe	ptchData = ptchFile.read()
108596ccc8cbSesaxe
108696ccc8cbSesaxe	baseFile.close()
108796ccc8cbSesaxe	ptchFile.close()
108896ccc8cbSesaxe
108996ccc8cbSesaxe	needToSnip = False
109096ccc8cbSesaxe	if fileType == "HTML" :
109196ccc8cbSesaxe		needToSnip = True
109296ccc8cbSesaxe		toSnipBeginStr = "<!-- Generated by javadoc"
109396ccc8cbSesaxe		toSnipEndStr = "-->\n"
109496ccc8cbSesaxe
109596ccc8cbSesaxe	if needToSnip :
1096*ea7dde8fSAndy Fiddaman		toSnipBegin = baseData.find(toSnipBeginStr)
109796ccc8cbSesaxe		if toSnipBegin != -1 :
1098*ea7dde8fSAndy Fiddaman			toSnipEnd = (baseData[toSnipBegin:].find(toSnipEndStr) +
109993be19b9SAndy Fiddaman						len(toSnipEndStr))
110093be19b9SAndy Fiddaman			baseData = (baseData[:toSnipBegin] +
110193be19b9SAndy Fiddaman				   baseData[toSnipBegin + toSnipEnd:])
110293be19b9SAndy Fiddaman			ptchData = (ptchData[:toSnipBegin] +
110393be19b9SAndy Fiddaman				   ptchData[toSnipBegin + toSnipEnd:])
110496ccc8cbSesaxe
110596ccc8cbSesaxe	if quiet :
110696ccc8cbSesaxe		if baseData != ptchData :
110796ccc8cbSesaxe			return 1
110896ccc8cbSesaxe	else :
110996ccc8cbSesaxe		if len(baseData) != len(ptchData) or baseData != ptchData :
1110598cc7dfSVladimir Kotal			diffs = diffData(base, ptch, baseData, ptchData)
111196ccc8cbSesaxe			difference(fileName, fileType, diffs)
111296ccc8cbSesaxe			return 1
111396ccc8cbSesaxe	return 0
111496ccc8cbSesaxe
111596ccc8cbSesaxe
111696ccc8cbSesaxe#####
111796ccc8cbSesaxe# Compare two objects by producing a data dump from
111896ccc8cbSesaxe# each object, and then comparing the dump data
111996ccc8cbSesaxe#
112096ccc8cbSesaxe# Returns: 1 if a difference is detected
112196ccc8cbSesaxe#          0 if no difference detected
112296ccc8cbSesaxe#         -1 upon error
112396ccc8cbSesaxe#
112496ccc8cbSesaxedef compareByDumping(base, ptch, quiet, fileType) :
112596ccc8cbSesaxe
112696ccc8cbSesaxe	fileName = fnFormat(base);
1127598cc7dfSVladimir Kotal	t = threading.currentThread()
1128598cc7dfSVladimir Kotal	tmpFile1 = tmpDir1 + os.path.basename(base) + t.getName()
1129598cc7dfSVladimir Kotal	tmpFile2 = tmpDir2 + os.path.basename(ptch) + t.getName()
113096ccc8cbSesaxe
113196ccc8cbSesaxe	if fileType == "Lint Library" :
113293be19b9SAndy Fiddaman		baseCmd = (lintdump_cmd + " -ir " + base +
113393be19b9SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
113493be19b9SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
113593be19b9SAndy Fiddaman			  " > " + tmpFile1)
113693be19b9SAndy Fiddaman		ptchCmd = (lintdump_cmd + " -ir " + ptch +
113793be19b9SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
113893be19b9SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
113993be19b9SAndy Fiddaman			  " > " + tmpFile2)
114096ccc8cbSesaxe	elif fileType == "Sqlite Database" :
114193be19b9SAndy Fiddaman		baseCmd = ("echo .dump | " + sqlite_cmd + base + " > " +
114293be19b9SAndy Fiddaman			  tmpFile1)
114393be19b9SAndy Fiddaman		ptchCmd = ("echo .dump | " + sqlite_cmd + ptch + " > " +
114493be19b9SAndy Fiddaman			  tmpFile2)
114596ccc8cbSesaxe
114696ccc8cbSesaxe	os.system(baseCmd)
114796ccc8cbSesaxe	os.system(ptchCmd)
114896ccc8cbSesaxe
114996ccc8cbSesaxe	try:
115096ccc8cbSesaxe		baseFile = open(tmpFile1)
115196ccc8cbSesaxe	except:
115296ccc8cbSesaxe		error("could not open: " + tmpFile1)
1153598cc7dfSVladimir Kotal		return
115496ccc8cbSesaxe	try:
115596ccc8cbSesaxe		ptchFile = open(tmpFile2)
115696ccc8cbSesaxe	except:
115796ccc8cbSesaxe		error("could not open: " + tmpFile2)
1158598cc7dfSVladimir Kotal		return
115996ccc8cbSesaxe
116096ccc8cbSesaxe	baseData = baseFile.read()
116196ccc8cbSesaxe	ptchData = ptchFile.read()
116296ccc8cbSesaxe
116396ccc8cbSesaxe	baseFile.close()
116496ccc8cbSesaxe	ptchFile.close()
116596ccc8cbSesaxe
116696ccc8cbSesaxe	if len(baseData) != len(ptchData) or baseData != ptchData :
116796ccc8cbSesaxe		if not quiet :
116896ccc8cbSesaxe			data = diffFileData(tmpFile1, tmpFile2);
1169598cc7dfSVladimir Kotal			try:
1170598cc7dfSVladimir Kotal				os.unlink(tmpFile1)
117193be19b9SAndy Fiddaman			except OSError as e:
1172598cc7dfSVladimir Kotal				error("compareByDumping: unlink failed %s" % e)
1173598cc7dfSVladimir Kotal			try:
1174598cc7dfSVladimir Kotal				os.unlink(tmpFile2)
117593be19b9SAndy Fiddaman			except OSError as e:
1176598cc7dfSVladimir Kotal				error("compareByDumping: unlink failed %s" % e)
117796ccc8cbSesaxe			difference(fileName, fileType, data)
117896ccc8cbSesaxe		return 1
1179598cc7dfSVladimir Kotal
1180598cc7dfSVladimir Kotal	# Remove the temporary files now.
1181598cc7dfSVladimir Kotal	try:
1182598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
118393be19b9SAndy Fiddaman	except OSError as e:
1184598cc7dfSVladimir Kotal		error("compareByDumping: unlink failed %s" % e)
1185598cc7dfSVladimir Kotal	try:
1186598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
118793be19b9SAndy Fiddaman	except OSError as e:
1188598cc7dfSVladimir Kotal		error("compareByDumping: unlink failed %s" % e)
1189598cc7dfSVladimir Kotal
119096ccc8cbSesaxe	return 0
119196ccc8cbSesaxe
119296ccc8cbSesaxe#####
1193619b4598Srotondo#
1194598cc7dfSVladimir Kotal# SIGINT signal handler. Changes thread control variable to tell the threads
1195598cc7dfSVladimir Kotal# to finish their current job and exit.
1196619b4598Srotondo#
1197598cc7dfSVladimir Kotaldef discontinue_processing(signl, frme):
1198598cc7dfSVladimir Kotal	global keep_processing
1199619b4598Srotondo
120093be19b9SAndy Fiddaman	print("Caught Ctrl-C, stopping the threads", file=sys.stderr)
1201598cc7dfSVladimir Kotal	keep_processing = False
1202619b4598Srotondo
1203598cc7dfSVladimir Kotal	return 0
1204619b4598Srotondo
1205598cc7dfSVladimir Kotal#####
1206598cc7dfSVladimir Kotal#
1207598cc7dfSVladimir Kotal# worker thread for changedFiles processing
1208598cc7dfSVladimir Kotal#
1209598cc7dfSVladimir Kotalclass workerThread(threading.Thread) :
1210598cc7dfSVladimir Kotal	def run(self):
1211598cc7dfSVladimir Kotal		global wset_lock
1212598cc7dfSVladimir Kotal		global changedFiles
1213598cc7dfSVladimir Kotal		global baseRoot
1214598cc7dfSVladimir Kotal		global ptchRoot
1215598cc7dfSVladimir Kotal		global keep_processing
1216619b4598Srotondo
1217598cc7dfSVladimir Kotal		while (keep_processing) :
1218598cc7dfSVladimir Kotal			# grab the lock to changedFiles and remove one member
1219598cc7dfSVladimir Kotal			# and process it
1220598cc7dfSVladimir Kotal			wset_lock.acquire()
1221598cc7dfSVladimir Kotal			try :
1222598cc7dfSVladimir Kotal				fn = changedFiles.pop()
1223598cc7dfSVladimir Kotal			except IndexError :
1224598cc7dfSVladimir Kotal				# there is nothing more to do
1225598cc7dfSVladimir Kotal				wset_lock.release()
1226598cc7dfSVladimir Kotal				return
1227598cc7dfSVladimir Kotal			wset_lock.release()
1228598cc7dfSVladimir Kotal
1229598cc7dfSVladimir Kotal			base = baseRoot + fn
1230598cc7dfSVladimir Kotal			ptch = ptchRoot + fn
1231598cc7dfSVladimir Kotal
1232598cc7dfSVladimir Kotal			compareOneFile(base, ptch, False)
1233598cc7dfSVladimir Kotal
1234619b4598Srotondo
1235619b4598Srotondo#####
123696ccc8cbSesaxe# Compare two objects. Detect type changes.
123796ccc8cbSesaxe# Vector off to the appropriate type specific
123896ccc8cbSesaxe# compare routine based on the type.
123996ccc8cbSesaxe#
124096ccc8cbSesaxedef compareOneFile(base, ptch, quiet) :
124196ccc8cbSesaxe
124296ccc8cbSesaxe	# Verify the file types.
124396ccc8cbSesaxe	# If they are different, indicate this and move on
124496ccc8cbSesaxe	btype = getTheFileType(base)
124596ccc8cbSesaxe	ptype = getTheFileType(ptch)
124696ccc8cbSesaxe
1247619b4598Srotondo	if btype == 'Error' or ptype == 'Error' :
1248619b4598Srotondo		return -1
1249619b4598Srotondo
125096ccc8cbSesaxe	fileName = fnFormat(base)
125196ccc8cbSesaxe
125296ccc8cbSesaxe	if (btype != ptype) :
1253619b4598Srotondo		if not quiet :
125496ccc8cbSesaxe			difference(fileName, "file type", btype + " to " + ptype)
125596ccc8cbSesaxe		return 1
125696ccc8cbSesaxe	else :
125796ccc8cbSesaxe		fileType = btype
125896ccc8cbSesaxe
125996ccc8cbSesaxe	if (fileType == 'ELF') :
126096ccc8cbSesaxe		return compareElfs(base, ptch, quiet)
126196ccc8cbSesaxe
126296ccc8cbSesaxe	elif (fileType == 'Java Archive' or fileType == 'ELF Object Archive') :
126396ccc8cbSesaxe		return compareArchives(base, ptch, fileType)
126496ccc8cbSesaxe
126596ccc8cbSesaxe	elif (fileType == 'HTML') :
126696ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
126796ccc8cbSesaxe
126896ccc8cbSesaxe	elif ( fileType == 'Lint Library' ) :
126996ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
127096ccc8cbSesaxe
127196ccc8cbSesaxe	elif ( fileType == 'Sqlite Database' ) :
127296ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
1273619b4598Srotondo
127496ccc8cbSesaxe	else :
127596ccc8cbSesaxe		# it has to be some variety of text file
127696ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
127796ccc8cbSesaxe
127896ccc8cbSesaxe# Cleanup and self-terminate
127996ccc8cbSesaxedef cleanup(ret) :
128096ccc8cbSesaxe
1281598cc7dfSVladimir Kotal	debug("Performing cleanup (" + str(ret) + ")")
1282598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir1) > 0 :
1283598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir1)
128496ccc8cbSesaxe
1285598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir2) > 0 :
1286598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir2)
128796ccc8cbSesaxe
128896ccc8cbSesaxe	if logging :
128996ccc8cbSesaxe		log.close()
129096ccc8cbSesaxe
129196ccc8cbSesaxe	sys.exit(ret)
129296ccc8cbSesaxe
129396ccc8cbSesaxedef main() :
129496ccc8cbSesaxe
129596ccc8cbSesaxe	# Log file handle
129696ccc8cbSesaxe	global log
129796ccc8cbSesaxe
129896ccc8cbSesaxe	# Globals relating to command line options
129996ccc8cbSesaxe	global logging, vdiffs, reportAllSects
130096ccc8cbSesaxe
130196ccc8cbSesaxe	# Named temporary files / directories
1302598cc7dfSVladimir Kotal	global tmpDir1, tmpDir2
130396ccc8cbSesaxe
130496ccc8cbSesaxe	# Command paths
130596ccc8cbSesaxe	global lintdump_cmd, elfdump_cmd, dump_cmd, dis_cmd, od_cmd, diff_cmd, sqlite_cmd
130696ccc8cbSesaxe
130796ccc8cbSesaxe	# Default search path
130896ccc8cbSesaxe	global wsdiff_path
130996ccc8cbSesaxe
131096ccc8cbSesaxe	# Essentially "uname -p"
131196ccc8cbSesaxe	global arch
131296ccc8cbSesaxe
1313598cc7dfSVladimir Kotal	# changed files for worker thread processing
1314598cc7dfSVladimir Kotal	global changedFiles
1315598cc7dfSVladimir Kotal	global baseRoot
1316598cc7dfSVladimir Kotal	global ptchRoot
1317598cc7dfSVladimir Kotal
1318598cc7dfSVladimir Kotal	# Sort the list of files from a temporary file
1319598cc7dfSVladimir Kotal	global sorted
1320598cc7dfSVladimir Kotal	global differentFiles
1321598cc7dfSVladimir Kotal
1322598cc7dfSVladimir Kotal	# Debugging indicator
1323598cc7dfSVladimir Kotal	global debugon
1324598cc7dfSVladimir Kotal
132596ccc8cbSesaxe	# Some globals need to be initialized
1326598cc7dfSVladimir Kotal	debugon = logging = vdiffs = reportAllSects = sorted = False
132796ccc8cbSesaxe
132896ccc8cbSesaxe
132996ccc8cbSesaxe	# Process command line arguments
133096ccc8cbSesaxe	# Return values are returned from args() in alpha order
133196ccc8cbSesaxe	# (Yes, python functions can return multiple values (ewww))
133296ccc8cbSesaxe	# Note that args() also set the globals:
133396ccc8cbSesaxe	#	logging to True if verbose logging (to a file) was enabled
133496ccc8cbSesaxe	#	vdiffs to True if logged differences aren't to be truncated
133596ccc8cbSesaxe	#	reportAllSects to True if all ELF section differences are to be reported
133696ccc8cbSesaxe	#
133796ccc8cbSesaxe	baseRoot, fileNamesFile, localTools, ptchRoot, results = args()
133896ccc8cbSesaxe
133996ccc8cbSesaxe	#
134096ccc8cbSesaxe	# Set up the results/log file
134196ccc8cbSesaxe	#
134296ccc8cbSesaxe	if logging :
134396ccc8cbSesaxe		try:
134496ccc8cbSesaxe			log = open(results, "w")
134596ccc8cbSesaxe		except:
134696ccc8cbSesaxe			logging = False
134796ccc8cbSesaxe			error("failed to open log file: " + log)
134896ccc8cbSesaxe			sys.exit(1)
134996ccc8cbSesaxe
1350ccac5ae3SJosef 'Jeff' Sipek		dateTimeStr= "# %04d-%02d-%02d at %02d:%02d:%02d" % time.localtime()[:6]
135196ccc8cbSesaxe		v_info("# This file was produced by wsdiff")
135296ccc8cbSesaxe		v_info(dateTimeStr)
135396ccc8cbSesaxe
1354598cc7dfSVladimir Kotal	# Changed files (used only for the sorted case)
1355598cc7dfSVladimir Kotal	if sorted :
1356598cc7dfSVladimir Kotal		differentFiles = []
1357598cc7dfSVladimir Kotal
135896ccc8cbSesaxe	#
135996ccc8cbSesaxe	# Build paths to the tools required tools
136096ccc8cbSesaxe	#
136196ccc8cbSesaxe	# Try to look for tools in $SRC/tools if the "-t" option
136296ccc8cbSesaxe	# was specified
136396ccc8cbSesaxe	#
1364*ea7dde8fSAndy Fiddaman	rc, arch = getoutput("uname -p")
1365*ea7dde8fSAndy Fiddaman	arch = arch.rstrip()
136696ccc8cbSesaxe	if localTools :
136796ccc8cbSesaxe		try:
136896ccc8cbSesaxe			src = os.environ['SRC']
136996ccc8cbSesaxe		except:
137096ccc8cbSesaxe			error("-t specified, but $SRC not set. Cannot find $SRC/tools")
137196ccc8cbSesaxe			src = ""
137296ccc8cbSesaxe		if len(src) > 0 :
137396ccc8cbSesaxe			wsdiff_path.insert(0, src + "/tools/proto/opt/onbld/bin")
137496ccc8cbSesaxe
137596ccc8cbSesaxe	lintdump_cmd = find_tool("lintdump")
137696ccc8cbSesaxe	elfdump_cmd = find_tool("elfdump")
137796ccc8cbSesaxe	dump_cmd = find_tool("dump")
137896ccc8cbSesaxe	od_cmd = find_tool("od")
137996ccc8cbSesaxe	dis_cmd = find_tool("dis")
138096ccc8cbSesaxe	diff_cmd = find_tool("diff")
138196ccc8cbSesaxe	sqlite_cmd = find_tool("sqlite")
138296ccc8cbSesaxe
138396ccc8cbSesaxe	#
1384598cc7dfSVladimir Kotal	# Set resource limit for number of open files as high as possible.
1385598cc7dfSVladimir Kotal	# This might get handy with big number of threads.
1386598cc7dfSVladimir Kotal	#
1387598cc7dfSVladimir Kotal	(nofile_soft, nofile_hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
1388598cc7dfSVladimir Kotal	try:
1389598cc7dfSVladimir Kotal		resource.setrlimit(resource.RLIMIT_NOFILE,
1390598cc7dfSVladimir Kotal		    (nofile_hard, nofile_hard))
1391598cc7dfSVladimir Kotal	except:
1392598cc7dfSVladimir Kotal		error("cannot set resource limits for number of open files")
1393598cc7dfSVladimir Kotal		sys.exit(1)
1394598cc7dfSVladimir Kotal
1395598cc7dfSVladimir Kotal	#
139696ccc8cbSesaxe	# validate the base and patch paths
139796ccc8cbSesaxe	#
139896ccc8cbSesaxe	if baseRoot[-1] != '/' :
139996ccc8cbSesaxe		baseRoot += '/'
140096ccc8cbSesaxe
140196ccc8cbSesaxe	if ptchRoot[-1] != '/' :
140296ccc8cbSesaxe		ptchRoot += '/'
140396ccc8cbSesaxe
140496ccc8cbSesaxe	if not os.path.exists(baseRoot) :
140596ccc8cbSesaxe		error("old proto area: " + baseRoot + " does not exist")
140696ccc8cbSesaxe		sys.exit(1)
140796ccc8cbSesaxe
140896ccc8cbSesaxe	if not os.path.exists(ptchRoot) :
140993be19b9SAndy Fiddaman		error("new proto area: " + ptchRoot + " does not exist")
141096ccc8cbSesaxe		sys.exit(1)
141196ccc8cbSesaxe
141296ccc8cbSesaxe	#
141396ccc8cbSesaxe	# log some information identifying the run
141496ccc8cbSesaxe	#
141596ccc8cbSesaxe	v_info("Old proto area: " + baseRoot)
141696ccc8cbSesaxe	v_info("New proto area: " + ptchRoot)
141796ccc8cbSesaxe	v_info("Results file: " + results + "\n")
141896ccc8cbSesaxe
141996ccc8cbSesaxe	#
142096ccc8cbSesaxe	# Set up the temporary directories / files
142196ccc8cbSesaxe	# Could use python's tmpdir routines, but these should
142296ccc8cbSesaxe	# be easier to identify / keep around for debugging
142396ccc8cbSesaxe	pid = os.getpid()
142496ccc8cbSesaxe	tmpDir1 = "/tmp/wsdiff_tmp1_" + str(pid) + "/"
142596ccc8cbSesaxe	tmpDir2 = "/tmp/wsdiff_tmp2_" + str(pid) + "/"
1426598cc7dfSVladimir Kotal	try:
142796ccc8cbSesaxe		os.makedirs(tmpDir1)
142893be19b9SAndy Fiddaman	except OSError as e:
1429598cc7dfSVladimir Kotal		error("main: makedir failed %s" % e)
1430598cc7dfSVladimir Kotal	try:
143196ccc8cbSesaxe		os.makedirs(tmpDir2)
143293be19b9SAndy Fiddaman	except OSError as e:
1433598cc7dfSVladimir Kotal		error("main: makedir failed %s" % e)
143496ccc8cbSesaxe
143596ccc8cbSesaxe	# Derive a catalog of new, deleted, and to-be-compared objects
143696ccc8cbSesaxe	# either from the specified base and patch proto areas, or from
143796ccc8cbSesaxe	# from an input file list
143896ccc8cbSesaxe	newOrDeleted = False
143996ccc8cbSesaxe
144096ccc8cbSesaxe	if fileNamesFile != "" :
144196ccc8cbSesaxe		changedFiles, newFiles, deletedFiles = \
144296ccc8cbSesaxe			      flistCatalog(baseRoot, ptchRoot, fileNamesFile)
144396ccc8cbSesaxe	else :
1444598cc7dfSVladimir Kotal		changedFiles, newFiles, deletedFiles = \
1445598cc7dfSVladimir Kotal				protoCatalog(baseRoot, ptchRoot)
144696ccc8cbSesaxe
144796ccc8cbSesaxe	if len(newFiles) > 0 :
144896ccc8cbSesaxe		newOrDeleted = True
144996ccc8cbSesaxe		info("\nNew objects found: ")
145096ccc8cbSesaxe
1451598cc7dfSVladimir Kotal		if sorted :
1452598cc7dfSVladimir Kotal			newFiles.sort()
145396ccc8cbSesaxe		for fn in newFiles :
145496ccc8cbSesaxe			info(fnFormat(fn))
145596ccc8cbSesaxe
145696ccc8cbSesaxe	if len(deletedFiles) > 0 :
145796ccc8cbSesaxe		newOrDeleted = True
145896ccc8cbSesaxe		info("\nObjects removed: ")
145996ccc8cbSesaxe
1460598cc7dfSVladimir Kotal		if sorted :
1461598cc7dfSVladimir Kotal			deletedFiles.sort()
146296ccc8cbSesaxe		for fn in deletedFiles :
146396ccc8cbSesaxe			info(fnFormat(fn))
146496ccc8cbSesaxe
146596ccc8cbSesaxe	if newOrDeleted :
1466598cc7dfSVladimir Kotal		info("\nChanged objects: ")
1467598cc7dfSVladimir Kotal	if sorted :
1468598cc7dfSVladimir Kotal		debug("The list will appear after the processing is done")
146996ccc8cbSesaxe
147096ccc8cbSesaxe	# Here's where all the heavy lifting happens
147196ccc8cbSesaxe	# Perform a comparison on each object appearing in
147296ccc8cbSesaxe	# both proto areas. compareOneFile will examine the
147396ccc8cbSesaxe	# file types of each object, and will vector off to
147496ccc8cbSesaxe	# the appropriate comparison routine, where the compare
147596ccc8cbSesaxe	# will happen, and any differences will be reported / logged
147696ccc8cbSesaxe
1477598cc7dfSVladimir Kotal	# determine maximum number of worker threads by using
1478598cc7dfSVladimir Kotal	# DMAKE_MAX_JOBS environment variable set by nightly(1)
1479598cc7dfSVladimir Kotal	# or get number of CPUs in the system
1480598cc7dfSVladimir Kotal	try:
1481598cc7dfSVladimir Kotal		max_threads = int(os.environ['DMAKE_MAX_JOBS'])
1482598cc7dfSVladimir Kotal	except:
1483598cc7dfSVladimir Kotal		max_threads = os.sysconf("SC_NPROCESSORS_ONLN")
1484598cc7dfSVladimir Kotal		# If we cannot get number of online CPUs in the system
1485598cc7dfSVladimir Kotal		# run unparallelized otherwise bump the number up 20%
1486598cc7dfSVladimir Kotal		# to achieve best results.
1487598cc7dfSVladimir Kotal		if max_threads == -1 :
1488598cc7dfSVladimir Kotal			max_threads = 1
1489598cc7dfSVladimir Kotal		else :
1490598cc7dfSVladimir Kotal			max_threads += max_threads/5
1491598cc7dfSVladimir Kotal
1492598cc7dfSVladimir Kotal	# Set signal handler to attempt graceful exit
1493598cc7dfSVladimir Kotal	debug("Setting signal handler")
1494598cc7dfSVladimir Kotal	signal.signal( signal.SIGINT, discontinue_processing )
1495598cc7dfSVladimir Kotal
1496598cc7dfSVladimir Kotal	# Create and unleash the threads
1497598cc7dfSVladimir Kotal	# Only at most max_threads must be running at any moment
1498598cc7dfSVladimir Kotal	mythreads = []
1499598cc7dfSVladimir Kotal	debug("Spawning " + str(max_threads) + " threads");
1500598cc7dfSVladimir Kotal	for i in range(max_threads) :
1501598cc7dfSVladimir Kotal		thread = workerThread()
1502598cc7dfSVladimir Kotal		mythreads.append(thread)
1503598cc7dfSVladimir Kotal		mythreads[i].start()
1504598cc7dfSVladimir Kotal
1505598cc7dfSVladimir Kotal	# Wait for the threads to finish and do cleanup if interrupted
1506598cc7dfSVladimir Kotal	debug("Waiting for the threads to finish")
1507598cc7dfSVladimir Kotal	while True:
1508598cc7dfSVladimir Kotal		if not True in [thread.isAlive() for thread in mythreads]:
1509598cc7dfSVladimir Kotal		    break
1510598cc7dfSVladimir Kotal		else:
1511598cc7dfSVladimir Kotal		    # Some threads are still going
1512598cc7dfSVladimir Kotal		    time.sleep(1)
1513598cc7dfSVladimir Kotal
1514598cc7dfSVladimir Kotal	# Interrupted by SIGINT
1515598cc7dfSVladimir Kotal	if keep_processing == False :
1516598cc7dfSVladimir Kotal		cleanup(1)
1517598cc7dfSVladimir Kotal
1518598cc7dfSVladimir Kotal	# If the list of differences was sorted it is stored in an array
1519598cc7dfSVladimir Kotal	if sorted :
1520598cc7dfSVladimir Kotal		differentFiles.sort()
1521598cc7dfSVladimir Kotal		for f in differentFiles :
1522598cc7dfSVladimir Kotal			info(fnFormat(f))
152396ccc8cbSesaxe
152496ccc8cbSesaxe	# We're done, cleanup.
152596ccc8cbSesaxe	cleanup(0)
152696ccc8cbSesaxe
152796ccc8cbSesaxeif __name__ == '__main__' :
152896ccc8cbSesaxe	try:
152996ccc8cbSesaxe		main()
153096ccc8cbSesaxe	except KeyboardInterrupt :
153196ccc8cbSesaxe		cleanup(1);
153296ccc8cbSesaxe
1533