+ /* If user asked to ignore the mismatch, do nothing. */
+ if (exec_file_mismatch_mode == exec_file_mismatch_off)
+ return;
+
+ const char *current_exec_file = get_exec_file (0);
+ struct inferior *inf = current_inferior ();
+ /* Try to determine a filename from the process itself. */
+ const char *pid_exec_file = target_pid_to_exec_file (inf->pid);
+ bool build_id_mismatch = false;
+
+ /* If we cannot validate the exec file, return. */
+ if (current_exec_file == NULL || pid_exec_file == NULL)
+ return;
+
+ /* Try validating via build-id, if available. This is the most
+ reliable check. */
+ const bfd_build_id *exec_file_build_id = build_id_bfd_get (exec_bfd);
+ if (exec_file_build_id != nullptr)
+ {
+ /* Prepend the target prefix, to force gdb_bfd_open to open the
+ file on the remote file system (if indeed remote). */
+ std::string target_pid_exec_file
+ = std::string (TARGET_SYSROOT_PREFIX) + pid_exec_file;
+
+ gdb_bfd_ref_ptr abfd (gdb_bfd_open (target_pid_exec_file.c_str (),
+ gnutarget, -1, false));
+ if (abfd != nullptr)
+ {
+ const bfd_build_id *target_exec_file_build_id
+ = build_id_bfd_get (abfd.get ());
+
+ if (target_exec_file_build_id != nullptr)
+ {
+ if (exec_file_build_id->size == target_exec_file_build_id->size
+ && memcmp (exec_file_build_id->data,
+ target_exec_file_build_id->data,
+ exec_file_build_id->size) == 0)
+ {
+ /* Match. */
+ return;
+ }
+ else
+ build_id_mismatch = true;
+ }
+ }
+ }
+
+ if (build_id_mismatch)
+ {
+ std::string exec_file_target (pid_exec_file);
+
+ /* In case the exec file is not local, exec_file_target has to point at
+ the target file system. */
+ if (is_target_filename (current_exec_file) && !target_filesystem_is_local ())
+ exec_file_target = TARGET_SYSROOT_PREFIX + exec_file_target;
+
+ warning
+ (_("Mismatch between current exec-file %ps\n"
+ "and automatically determined exec-file %ps\n"
+ "exec-file-mismatch handling is currently \"%s\""),
+ styled_string (file_name_style.style (), current_exec_file),
+ styled_string (file_name_style.style (), exec_file_target.c_str ()),
+ exec_file_mismatch_names[exec_file_mismatch_mode]);
+ if (exec_file_mismatch_mode == exec_file_mismatch_ask)
+ {
+ symfile_add_flags add_flags = SYMFILE_MAINLINE;
+ if (from_tty)
+ add_flags |= SYMFILE_VERBOSE;
+ try
+ {
+ symbol_file_add_main (exec_file_target.c_str (), add_flags);
+ exec_file_attach (exec_file_target.c_str (), from_tty);
+ }
+ catch (gdb_exception_error &err)
+ {
+ warning (_("loading %ps %s"),
+ styled_string (file_name_style.style (),
+ exec_file_target.c_str ()),
+ err.message != NULL ? err.what () : "error");
+ }
+ }
+ }
+}
+
+/* See gdbcore.h. */
+
+void
+exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
+{
+ char *exec_file_target;
+ symfile_add_flags add_flags = 0;
+
+ /* Do nothing if we already have an executable filename. */
+ if (get_exec_file (0) != NULL)
+ return;
+
+ /* Try to determine a filename from the process itself. */
+ exec_file_target = target_pid_to_exec_file (pid);
+ if (exec_file_target == NULL)
+ {
+ warning (_("No executable has been specified and target does not "
+ "support\n"
+ "determining executable automatically. "
+ "Try using the \"file\" command."));
+ return;
+ }
+
+ gdb::unique_xmalloc_ptr<char> exec_file_host
+ = exec_file_find (exec_file_target, NULL);
+
+ if (defer_bp_reset)
+ add_flags |= SYMFILE_DEFER_BP_RESET;