diff --git a/kconfigs/Kconfig.libvirt b/kconfigs/Kconfig.libvirt
index cba8abf1e..955399e6c 100644
--- a/kconfigs/Kconfig.libvirt
+++ b/kconfigs/Kconfig.libvirt
@@ -1104,6 +1104,31 @@ config LIBVIRT_STORAGE_POOL_NAME
For instance you may want to use a volume name of "data2" for a path
on a partition on /data2/ or something like that.
+config LIBVIRT_ENABLE_GDB
+ bool "Enable GDB debugging for the guest"
+ output yaml
+ help
+ Select this option if you want to enable debugging support for GDB.
+ By default , it is assumed that gdb is disabled since we dont want
+ to complicate this for the CI runs. If enabled then libvirt guest
+ xml for each guest will be configured to use gdb on a specific
+ tcp port.
+
+config LIBVIRT_GDB_BASEPORT
+ int
+ default $(shell, ./scripts/get_gdb_base_port.sh) if LIBVIRT
+ output yaml
+ depends on LIBVIRT_ENABLE_GDB
+ help
+ This option defines the base port to be used for the GDB.
+ Esentially we need to make QEMU listen for an incoming connection from
+ gdb on a TCP port. The default port is chosen to be 1234. However we
+ introduce variability for assigning the port to each guest by defining
+ a base port and adding an index to it based on the number of libvrt guest
+ nodes. Therefore the base port is extracted from the md5sum of the
+ /scripts/get_gdb_base_port.sh file and use the last 4 digits of the md5sum
+ of the file to assign to libvirt_gdb_baseport.
+
source "kconfigs/Kconfig.libvirt.zns"
source "kconfigs/Kconfig.libvirt.largeio"
source "kconfigs/Kconfig.libvirt.cxl"
diff --git a/playbooks/roles/gen_nodes/defaults/main.yml b/playbooks/roles/gen_nodes/defaults/main.yml
index 8ff9b8799..6a899be45 100644
--- a/playbooks/roles/gen_nodes/defaults/main.yml
+++ b/playbooks/roles/gen_nodes/defaults/main.yml
@@ -27,6 +27,9 @@ vagrant_box: "debian/testing64"
vagrant_box_version: ""
libvirt_vcpus_count: 8
libvirt_mem_mb: 4096
+gdb_port_conflict: False
+libvirt_enable_gdb: False
+libvirt_gdb_baseport: 1234
qemu_bin_path: "/usr/bin/qemu-system-x86_64"
extra_disk_path: ".vagrant/nvme_disks"
extra_disk_driver: "nvme"
diff --git a/playbooks/roles/gen_nodes/tasks/main.yml b/playbooks/roles/gen_nodes/tasks/main.yml
index d541dcbf1..9312fef3e 100644
--- a/playbooks/roles/gen_nodes/tasks/main.yml
+++ b/playbooks/roles/gen_nodes/tasks/main.yml
@@ -561,13 +561,39 @@
- kdevops_enable_guestfs|bool
- pcie_passthrough_enable|bool
+- name: Find if port conflict occur
+ ansible.builtin.shell: "ss -ltn | grep ':{{ (libvirt_gdb_baseport | int) + (idx | int) }} '"
+ register: gdb_port_reg
+ failed_when: false
+ changed_when: false
+ loop: "{{ guestfs_nodes }}"
+ loop_control:
+ index_var: idx
+ when:
+ - kdevops_enable_guestfs|bool
+
+- name: Set the conflict flag on if conflict occur
+ set_fact:
+ gdb_port_conflict: True
+ when: >
+ gdb_port_reg.results is defined and
+ gdb_port_reg.results | selectattr('rc', 'equalto', 0) | list | length > 0
+
+- name: Fail bringup if gdb port conflict occur
+ fail:
+ msg: "GDB port conflict occur, please check the base port number {{ libvirt_gdb_baseport }} and try with another"
+ when: gdb_port_conflict|bool
+
- name: Generate XML files for the libvirt guests
vars:
hostname: "{{ item.name }}"
+ guestidx: "{{ idx }}"
template:
src: "guestfs_{{ libvirt_machine_type }}.j2.xml"
dest: "{{ topdir_path }}/guestfs/{{ hostname }}/{{ hostname }}.xml"
force: yes
with_items: "{{ guestfs_nodes }}"
+ loop_control:
+ index_var: idx
when:
- kdevops_enable_guestfs|bool
diff --git a/playbooks/roles/gen_nodes/templates/guestfs_q35.j2.xml b/playbooks/roles/gen_nodes/templates/guestfs_q35.j2.xml
index adaba9188..698225dd5 100644
--- a/playbooks/roles/gen_nodes/templates/guestfs_q35.j2.xml
+++ b/playbooks/roles/gen_nodes/templates/guestfs_q35.j2.xml
@@ -184,6 +184,10 @@
+ {% if libvirt_enable_gdb %}
+
+
+ {% endif %}
{% include './templates/gen_drives.j2' %}
diff --git a/playbooks/roles/gen_nodes/templates/guestfs_virt.j2.xml b/playbooks/roles/gen_nodes/templates/guestfs_virt.j2.xml
index 1061f2cc4..29818ea85 100644
--- a/playbooks/roles/gen_nodes/templates/guestfs_virt.j2.xml
+++ b/playbooks/roles/gen_nodes/templates/guestfs_virt.j2.xml
@@ -173,6 +173,10 @@
+ {% if libvirt_enable_gdb %}
+
+
+ {% endif %}
{% include './templates/gen_drives.j2' %}
diff --git a/scripts/get_gdb_base_port.sh b/scripts/get_gdb_base_port.sh
new file mode 100755
index 000000000..66f987cc0
--- /dev/null
+++ b/scripts/get_gdb_base_port.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+# This script is used to get the base port for gdbserver
+
+# get the sha1sum of the script
+
+base_md5sum=$(md5sum "$0" | awk '{print $1}')
+digits_only=$(echo $base_md5sum | tr -cd '0-9')
+
+# extract the last 4 digits from md5sum
+
+base_port=${digits_only: -4}
+echo $base_port