BASH Reading prompt from GDB - linux

My intentions are the following. I am debugging an object file compiled with gcc from a .c script. Lets call this compiled script "foo". When I run the command from my terminal on mac:
gdb -q ./foo
I get the an output of:
Reading symbols from ./foo...Reading symbols from /Users/john/Documents....done.
done.
And immediately I get a prompt from the shell looking like so:
(gdb) "Shell waiting for my input command here from keyboard"
At this point I want to automate the input of certain commands like:
break, list, x/x "symbol in .c file", x/s "symbol in .c file" and many more. For this automation I want to use a little bash script, and so far I have the following:
#!/bin/bash
SCRIPT=$1
gstdbuf -oL gdb -q $SCRIPT |
while read -r LINE
do
echo "$LINE"
done
At the point of executing this bash script, I see the following output on my terminal's shell:
Reading symbols from ./foo...Reading symbols from /Users/john/Documents....done.
done.
But I do not see the:
(gdb) "Shell waiting for my input command here from keyboard"
How can I detect this prompt from the gdb process in my shell script in order to be able to automate the commands I want instead of inputting them manually?
Many Thanks!

You can create a file .gdbinit and put the initial commands there. gdb will execute them on startup if you added the following line to $HOME/.gdbinit:
add-auto-load-safe-path /path/to/project/.gdbinit
Now you can place commands into /path/to/project/.gdbinit, like this:
break main
run --foo=bar

Related

How do I get the output of executable without using "write" command?

I have a hello world code that is compiled. How do I get the output of the executable in a file instead of printing in the terminal where the program runs. Can it be done without including "write" command in code ?
The executable created is "hello.out" and compiled using "mpif90 hello.f90 -o hello.out"
./hello.out > filename
If you still want to see the output on the terminal as well you can pipe it to tee instead:
./hello.out | tee filename
This will write the output to the file and to the terminal.

Linux All Output to a File

Is there any way to tell Linux system put all output(stdout,stderr) to a file?
With out using redirection, pipe or modification the how scrips get called.
Just tell the Linux use a file for output.
for example:
script test1.sh:
#!/bin/bash
echo "Testing 123 "
If i run it like "./test1.sh" (with out redirection or pipe)
i'd like to see "Testing 123" in a file (/tmp/linux_output)
Problem: in the system a binary makes a call to a script and this script call many other scrips. it is not possible to modify each call so If i can modify Linux put "output" to a file i can review the logs.
#!/bin/bash
exec >file 2>&1
echo "Testing 123 "
You can read more about exec here
If you are running the program from a terminal, you can use the command script.
It will open up a sub-shell. Do what you need to do.
It will copy all output to the terminal into a file. When you are done, exit the shell. ^D, or exit.
This does not use redirection or pipes.
You could set your terminal's scrollback buffer to a large number of lines and then see all the output from your commands in the buffer - depending on your terminal window and the options in its menus, there may be an option in there to capture terminal I/O to a file.
Your requirement if taken literally is an impractical one, because it is based in a slight misunderstanding. Fundamentally, to get the output to go in a file, you will have to change something to direct it there - which would violate your literal constraint.
But the practical problem is solvable, because unless explicitly counteracted in the child, the output directions configured in a parent process will be inherited. So you only have to setup the redirection once, using either a shell, or a custom launcher program or intermediary. After that it will be inherited.
So, for example:
cat > test.sh
#/bin/sh
echo "hello on stdout"
rm nosuchfile
./test2.sh
And a child script for it to call
cat > test2.sh
#/bin/sh
echo "hello on stdout from script 2"
rm thisfileisnteither
./nonexistantscript.sh
Run the first script redirecting both stdout and stderr (bash version - and you can do this in many ways such as by writing a C program that redirects its outputs then exec()'s your real program)
./test.sh &> logfile
Now examine the file and see results from stdout and stderr of both parent and child.
cat logfile
hello on stdout
rm: nosuchfile: No such file or directory
hello on stdout from script 2
rm: thisfileisnteither: No such file or directory
./test2.sh: line 4: ./nonexistantscript.sh: No such file or directory
Of course if you really dislike this, you can always always modify the kernel - but again, that is changing something (and a very ungainly solution too).

Launch gdb automatically on Linux

Is there a way to automatically start a process under gdb on Linux? An equivalent of setting the Image File Execution Options on Windows.
I am trying to debug start-up phase of a process that is launched from another one.
I would normally move the real program out of the way, and replace it with a script that launches the program under GDB with the same parameters.
#!/bin/bash
exec gdb -args <realprog> "$#"
If that doesn't work due to the output being redirected to file, or something, then try this:
#!/bin/bash
exec xterm -e gdb -args <realprog> "$#"
That should give you a pop-up terminal with GDB running inside.
You don't have to go through all that registry voodoo on Linux :)
Simply:
1) Rename your program
2) Write a shell script that calls gdb with your (renamed) program and passes any arguments you want. Make sure you "chmod +rx" your script.
3) Name the shell script the original name of your program, and put it in the same directory as your program
4) Execute!

Suppress echo of command invocation in makefile?

I wrote a program for an assignment which is supposed to print its output to stdout. The assignment spec requires the creation of a Makefile which when invoked as make run > outputFile should run the program and write the output to a file, which has a SHA1 fingerprint identical to the one given in the spec.
My problem is that my makefile:
...
run:
java myprogram
also prints the command which runs my program (e.g. java myprogram) to the output file, so that my file includes this extra line causing the fingerprint to be wrong.
Is there any way to execute a command without the command invocation echoing to the command line?
Add # to the beginning of command to tell gmake not to print the command being executed. Like this:
run:
#java myprogram
As Oli suggested, this is a feature of Make and not of Bash.
On the other hand, Bash will never echo commands being executed unless you tell it to do so explicitly (i.e. with -x option).
Even simpler, use make -s (silent mode)!
You can also use .SILENT
.SILENT: run
hi:
echo "Hola!"
run:
java myprogram
In this case, make hi will output command, but make run will not output.
The effect of preceding the command with an # can be extended to a section by extending the command using a trailing backslash on the line. If a .PHONY command is desired to suppress output one can begin the section with:
#printf "..."

How to stop a zsh script from being suspended (tty output)

I have a zsh script that I want to run such that it also loads up my .zshrc file.
I believe I have to run my script in interactive mode?
Thus, my script begins like:
#!/bin/zsh -i
if [ $# = 0 ]
then
echo "need command line paramter..."
exit
fi
However, when I try to run this script in the background, my script becomes suspended (even if I pass in the correct number of parameters):
[1] + suspended (tty output)
My question is: How can I make a script that can run in the background that also loads my startup .zshrc file? If I have to put it into interactive mode, how can I avoid the suspension on tty output problem?
Thanks
Don't use interactive mode as a hash-bang!
Instead, source your zshrc file in the script if you want it:
#!/bin/zsh
source ~/.zshrc
...
For future reference, you can use the disown bultin to detach a previously backgrounded job from the shell so it can't be suspended or anything else. The parent shell can then be closed with no affect on the process:
$ disown %1
You can do this directly from the command line when you start the program by using the &! operator instead of just &:
$ ./my_command &!

Resources