git: 7e2852846872 - main - devel/gdb: kgdb: Handle stoppcbs compat

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Tue, 05 Sep 2023 18:24:17 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/ports/commit/?id=7e2852846872215c7d7ad36d9ea15d593bbf343e

commit 7e2852846872215c7d7ad36d9ea15d593bbf343e
Author:     Mark Jonhston <markj@FreeBSD.org>
AuthorDate: 2023-09-05 18:10:40 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-09-05 18:16:32 +0000

    devel/gdb: kgdb: Handle stoppcbs compat
    
    Use __FreeBSD_version to decide whether to dereference the stoppcbs
    symbol.  PORTREVISION will be bumped as part of the next commit, which
    fixes another KBI breakage.
    
    Reviewed by:    jhb, kevans
    Approved by:    jhb (maintainer)
    MFH:            2023Q3
    Differential Revision:  https://reviews.freebsd.org/D39951
---
 devel/gdb/files/kgdb/fbsd-kvm.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/devel/gdb/files/kgdb/fbsd-kvm.c b/devel/gdb/files/kgdb/fbsd-kvm.c
index b5bc0f924612..bd4f71fecddd 100644
--- a/devel/gdb/files/kgdb/fbsd-kvm.c
+++ b/devel/gdb/files/kgdb/fbsd-kvm.c
@@ -279,6 +279,7 @@ fbsd_kvm_target_open (const char *args, int from_tty)
 	kvm_t *nkvm;
 	const char *kernel;
 	std::string filename;
+	int osreldate;
 	bool writeable;
 
 	if (ops == NULL || ops->supply_pcb == NULL || ops->cpu_pcb_addr == NULL)
@@ -340,6 +341,14 @@ fbsd_kvm_target_open (const char *args, int from_tty)
 	}
 #endif
 
+	kvm = nkvm;
+	vmcore = std::move(filename);
+	current_inferior()->push_target (&fbsd_kvm_ops);
+
+	/* Pop the target automatically upon failure. */
+	target_unpush_up unpusher;
+	unpusher.reset (&fbsd_kvm_ops);
+
 	/*
 	 * Determine the first address in KVA.  Newer kernels export
 	 * VM_MAXUSER_ADDRESS and the first kernel address can be
@@ -353,11 +362,29 @@ fbsd_kvm_target_open (const char *args, int from_tty)
 		kernstart = kgdb_lookup("kernbase");
 	}
 
+	try {
+		CORE_ADDR osreldatesym = kgdb_lookup("osreldate");
+		osreldate = read_memory_unsigned_integer(osreldatesym, 4,
+		    gdbarch_byte_order (target_gdbarch ()));
+	} catch (const gdb_exception_error &e) {
+		error ("Failed to look up osreldate");
+	}
+
 	/*
-	 * Lookup symbols needed for stoppcbs[] handling, but don't
+	 * Look up symbols needed for stoppcbs handling, but don't
 	 * fail if they aren't present.
 	 */
 	stoppcbs = kgdb_lookup("stoppcbs");
+	if (osreldate > 1400088) {
+		/* stoppcbs is now a pointer rather than an array. */
+		try {
+			stoppcbs = read_memory_typed_address(stoppcbs,
+			    builtin_type(target_gdbarch())->builtin_data_ptr);
+		} catch (const gdb_exception_error &e) {
+			stoppcbs = 0;
+		}
+	}
+
 	try {
 		pcb_size = parse_and_eval_long("pcb_size");
 	} catch (const gdb_exception_error &e) {
@@ -379,10 +406,6 @@ fbsd_kvm_target_open (const char *args, int from_tty)
 		}
 	}
 
-	kvm = nkvm;
-	vmcore = std::move(filename);
-	current_inferior()->push_target (&fbsd_kvm_ops);
-
 	kgdb_dmesg();
 
 	inf = current_inferior();
@@ -406,6 +429,9 @@ fbsd_kvm_target_open (const char *args, int from_tty)
 
 	reinit_frame_cache ();
 	print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
+
+	/* Keep the target pushed. */
+	unpusher.release ();
 }
 
 void