diff: fix mdb breakpoints bug (attempt #2)

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 8 21:50:09 AEDT 1999


Estimated hours taken: 2

Fix a bug where mdb was sometimes failing to stop at breakpoints
(my previous fix for this was incomplete).

trace/mercury_trace_spy.c:
	Introduce a new function MR_compare_addr() and use it for the
	the comparison argument to MR_prepare_for_sorted_insert()
	and MR_bsearch.  This fixes the following problems:
		- the comparison argument for those functions
		  needs to have type `int', not `Unsigned';
		- the sense of the comparison argument to
		  MR_prepare_for_sorted_insert() was inverted.

runtime/mercury_array_macros.h:
	Document that the comparison argument to MR_prepare_for_sorted_insert()
	and MR_bsearch needs to have type `int'.

Index: runtime/mercury_array_macros.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_array_macros.h,v
retrieving revision 1.2
diff -u -r1.2 mercury_array_macros.h
--- mercury_array_macros.h	1998/11/05 16:35:29	1.2
+++ mercury_array_macros.h	1999/02/08 10:25:12
@@ -134,9 +134,9 @@
 ** `element' will be clobbered.
 **
 ** The number of the elements in the array is given by the `next' parameter.
-** The `COMPARE' parameter should be an expression which compares
+** The `COMPARE' parameter should be an expression of type int which compares
 ** the value at the index specified by the current value of `element'
-** with the desired value, and returns <0, 0, or >0 according to whether
+** with the desired value, and returns <0, 0, or >0 according to whether 
 ** it is less than, equal to, or greater than the desired value.
 **
 ** The name of the array to be searched is not explicitly a parameter;
@@ -184,7 +184,7 @@
 ** The `next' parameter holds the number of elements in the array;
 ** it is incremented by this macro. The macro returns the index of the slot
 ** at which the new item should be inserted in the `element' parameter.
-** The `COMPARE' parameter should be an expression which compares
+** The `COMPARE' parameter should be an expression of type int which compares
 ** the item at the index specified by the current value of `element' with
 ** the item being inserted, and returns <0, 0, or >0 according to whether
 ** it is less than, equal to, or greater than the item being inserted.
Index: trace/mercury_trace_spy.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_spy.c,v
retrieving revision 1.2
diff -u -r1.2 mercury_trace_spy.c
--- mercury_trace_spy.c	1999/02/04 16:59:07	1.2
+++ mercury_trace_spy.c	1999/02/08 10:39:39
@@ -28,13 +28,38 @@
 
 #define	INIT_SPY_TABLE_SIZE	10
 
+static	int	MR_compare_addr(const void *address1, const void *address2);
+static	int	MR_search_spy_table_for_proc(const MR_Stack_Layout_Entry
+			*entry);
+
+/*
+** Compare two addresses, and return an integer which is <0, 0, or >0
+** depending on whether the first address is less than, equal to, or
+** greater than the second.  Suitable for use with MR_bsearch() and
+** MR_prepare_insert_into_sorted().
+*/
+static int
+MR_compare_addr(const void *address1, const void *address2)
+{
+	/*
+	** Note that we can't just compare the pointers, because
+	** because on a segmented architecture, that might
+	** only compare the segments, not the offsets (ANSI C
+	** doesn't require pointer comparisons to work unless
+	** the pointers point into the same array, which is not
+	** necessarily going to be the case here).
+	** So instead we need to cast the pointers to integers
+	** and compare the integers.
+	*/
+	Unsigned num1 = (Unsigned) address1;
+	Unsigned num2 = (Unsigned) address2;
+	return (num1 > num2 ? 1 : num1 == num2 ? 0 : -1);
+}
+
 /*
 ** Return the index of the entry in MR_spied_procs whose spy_proc field
 ** is entry, or a negative number if absent.
 */
-static	int	MR_search_spy_table_for_proc(const MR_Stack_Layout_Entry
-			*entry);
-
 static int
 MR_search_spy_table_for_proc(const MR_Stack_Layout_Entry *entry)
 {
@@ -42,7 +67,7 @@
 	bool	found;
 
 	MR_bsearch(MR_spied_proc_next, slot, found,
-		(Unsigned) MR_spied_procs[slot].spy_proc - (Unsigned) entry);
+		MR_compare_addr(MR_spied_procs[slot].spy_proc, entry));
 	if (found) {
 		return slot;
 	} else {
@@ -137,8 +162,7 @@
 			INIT_SPY_TABLE_SIZE);
 		MR_prepare_insert_into_sorted(MR_spied_procs,
 			MR_spied_proc_next, slot,
-			(Unsigned) entry -
-			(Unsigned) MR_spied_procs[slot].spy_proc);
+			MR_compare_addr(MR_spied_procs[slot].spy_proc, entry));
 		MR_spied_procs[slot].spy_proc = entry;
 		MR_spied_procs[slot].spy_points = NULL;
 	}

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh at 128.250.37.3        |     -- leaked Microsoft memo.



More information about the developers mailing list