One catch be careful of on Gen 2 devices is that SWD is enabled in the bootloader, but is turned off in normal operating mode. Thus you either need to reset halt the device, or boot into DFU mode, which causes the device to stay in the bootloader with SWD enabled. In order to get reset halt to work reliably on the STM32 you may need to connect the RESET line to the debugger and enable hardware reset as software reset doesn’t always work.
(On Gen 3, a SWD CMSIS-DAP debugger can always reset halt the device even when it’s in normal operating mode.)