write the commands in a file and execute gdb with -x switch

gdb -x command_file_name

or run the gdb source command while gdb is running if you dont want to specify a command file from command line

>source [-s] [-v] command_file_name
Answer from Kanwar Saad on Stack Overflow
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Command-Files.html
Command Files (Debugging with GDB)
A command file for GDB is a text ... it does not mean to repeat the last command, as it would from the terminal. You can request the execution of a command file with the source command....
🌐
VisualGDB
visualgdb.com › gdbreference › commands › info_source
GDB Command Reference - info source command
<< std::endl; (gdb) info source Current source file is test.cpp Compilation directory is /home/bazis Located in /home/bazis/test.cpp Contains 7 lines. Source language is c++. Compiled with DWARF 2 debugging format. Does not include preprocessor macro info. You can execute the info source command ...
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Source-Path.html
Source Path (Debugging with GDB)
Using the previous example, suppose the foo-1.0 tree has been moved from /usr/src to /mnt/cross, then you can tell GDB to replace /usr/src in all source path names with /mnt/cross. The first lookup will then be /mnt/cross/foo-1.0/lib/foo.c in place of the original location of /usr/src/foo-1.0/lib/foo.c. To define a source path substitution rule, use the set substitute-path command (see set substitute-path).
🌐
Dzyoba
alex.dzyoba.com › blog › gdb-source-path
How to point GDB to your sources | There is no magic here
You can direct GDB to the new source path right in the debug session with directory <dir> command:
🌐
GNU
ftp.gnu.org › old-gnu › Manuals › gdb › html_node › gdb_48.html
Debugging with GDB - Source Path
When you start GDB, its source path includes only `cdir' and `cwd', in that order. To add other directories, use the directory command.
🌐
Apple Developer
developer.apple.com › library › archive › documentation › DeveloperTools › gdb › gdb › gdb_8.html
Debugging with gdb - Examining Source Files
EDITOR=/usr/bin/vi export EDITOR gdb ... ... There are two commands for searching through the current source file for a regular expression.
🌐
VisualGDB
visualgdb.com › gdbreference › commands › info_sources
GDB Command Reference - info sources command
Starting program: /home/bazis/test Temporary breakpoint 1, main (argc=1, argv=0xbffff064) at test.cpp:5 5 std::cout << "Hello!" << std::endl; (gdb) info sources Source files for which symbols have been read in: /home/bazis/test.cpp, /home/bazis/<built-in>, /usr/include/wctype.h, /usr/include/i386-linux-gnu/c++/4.8/bits/atomic_word.h, ...
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Source.html
Source (Debugging with GDB)
When your program stops, GDB spontaneously prints the line where it stopped. Likewise, when you select a stack frame (see Selecting a Frame), GDB prints the line where execution in that frame has stopped. You can print other portions of source files by explicit command.
Find elsewhere
🌐
Qt Forum
forum.qt.io › home › qt development › qt creator and other tools › automatically append source directory to gdb when debugging
Automatically append source directory to gdb when debugging | Qt Forum
July 4, 2023 - Hey, I have external source directories of my library that I want to add to gdb while debugging. In the command line I can do that by calling gdb via gdb --directory=/path/to/sources or by writing directory /path/to/sources while running gdb.
🌐
Lbl
davis.lbl.gov › Manuals › GDB › gdb_8.html
Debugging with GDB: Source
You can abbreviate this command as rev. Executable programs sometimes do not record the directories of the source files from which they were compiled, just the names. Even when they do, the directories could be moved between the compilation and your debugging session. GDB has a list of directories to search for source files; this is called the source path.
🌐
Apple Developer
developer.apple.com › library › archive › documentation › DeveloperTools › gdb › gdb › gdb_21.html
Debugging with gdb - Canned Sequences of Commands
A command file for GDB is a text file made of lines that are GDB commands. Comments (lines starting with #) may also be included. An empty line in a command file does nothing; it does not mean to repeat the last command, as it would from the terminal. You can request the execution of a command file with the source command:
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_developer_toolset › 12 › html › user_guide › chap-gdb
Chapter 8. GNU Debugger (GDB) | User Guide | Red Hat Developer Toolset | 12 | Red Hat Documentation
Type "apropos word" to search for commands related to "word"... Reading symbols from fibonacci...done. (gdb) Copy to Clipboard Copied! ... Copy to Clipboard Copied! ... Before you start the execution of the program you are debugging, gdb displays the first ten lines of the source code, and any subsequent use of this command lists another ten lines.
🌐
Johnnysswlab
johnnysswlab.com › home › gdb: a quick guide to make your debugging easier
GDB: A quick guide to make your debugging easier - Johnny's Software Lab
March 19, 2022 - This is a huge improvement to using list to actually see what code are you debugging! ... Hit Ctrl + X + 2 and you will get multiple windows: source code, assembly code, registers etc.
🌐
GNU
ftp.gnu.org › old-gnu › Manuals › gdb › html_node › gdb_190.html
Debugging with GDB - Command Files
An empty line in a command file does nothing; it does not mean to repeat the last command, as it would from the terminal. When you start GDB, it automatically executes commands from its init files. These are files named `.gdbinit' on Unix and `gdb.ini' on DOS/Windows.