Can gdb's "input not from terminal" messages be suppressed? - gdb

I have a gdb script that ends with the command quit.
When I run the script like this:
gdb -x foo.gdb target_program
The final output always has:
Quit anyway? (y or n) [answered Y; input not from terminal]
Is there a way to suppress either the entire message, or even just the part inside the []?
[answered Y; input not from terminal]

Try first execute
set confirm off
in your foo.gdb

Related

Run GDB 'commands' command from terminal?

According to this page, it is possible to have specific commands executed in GDB on breakpoint. Example from the linked page:
break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end
What the above means is break on function foo if x>0, print the value of x and continue with the execution.
Is there a way to specify commands...end from the terminal line? Something like:
gdb -ex=r -ex=bt -ex='b foo commands silent... end' --args myprog
Googling for the term gdb commands leads to a lot of ambiguity and I am not able to find a working example. Thank you.

gdb error handling within a user-defined command

It's really annoying that gdb automatically continues execution on the 'jump' command, so I've set up a short 'j' command in gdb to combine tbreak and jump to set a temporary breakpoint and jump to it with a single command. This works great - except... If the location I specify is not in the current stack frame, the jump command attempts to warn me, but this happens:
Line 2810 is not in `TDAClearFields'. Jump anyway? (y or n) [answered Y; input not from terminal]
and it jumps there anyway. Is there a way in a user-defined command to provide a default answer of 'n' for this error condition so that a typo in my 'j' command doesn't result in crashing the program?

Reading std::cin hangs if no pipe is provided

I have a function of the general form
void readStuff(std::istream& input){
int var;
while(input>>var){
//... do stuff
}
}
where input is passed as either std::cin or std::ifstream, depending on whether a command line option -c is set. Reading from file
myprog file.txt
works fine. If std::cin is used,
cat file.txt | myprog -c
that works fine as well. However, it hangs if the user tries
myprog -c
How can I handle this case from within my code? I've tried checking ifstream's flags, but in all cases they are 0 except for ifstream.good().
Your program isn't hanging; cin is just waiting for input from the terminal.
When you piped cat command's stdout into your program's stdin, there was an EOF in your stdin. This would cause the eof flag to be set which in turn would cause while(input>>var) to fail; hence exiting the loop.
When you execute your program directly from the terminal, cin keeps waiting for input from the terminal until the input stream is closed. You can exit your program by sending an EOF through the terminal which will close the input stream which in turn will break the loop in your code.
If you want your program to exit if it is not in a pipe, you can check using isatty on POSIX systems and _isatty on Windows.

Custom interactive shell

I've run into the following problem: My console utility should be running as a process (hope it's the right term) so every command goes to it directly. Like gnuplot, interactive shells (irb, etc.).
This shows what I'm talking about:
Mikulas-Dites-Mac-2:Web rullaf$ command
Mikulas-Dites-Mac-2:Web rullaf$ irb
>> command
NameError: undefined local variable or method `command' for main:Object
from (irb):1
>> exit
Mikulas-Dites-Mac-2:Web rullaf$
first command is executed as shell command, but after I enter irb, it's not. You get the point.
irb puts console into some special mode, or it simply parses the given input itself in some loop?
Is here any proper way to create such a behavior in c++? Thanks
You have to parse the input yourself. Depending on the complexity of the input, this might be accomplished by some simple string matching for trivial cases. A very simple example:
#include <iostream>
#include <string>
int main()
{
std::string input;
for(;;)
{
std::cout << ">>";
std::cin >> input;
if(input=="exit")
return 0;
else if(input=="test")
std::cout << "Test!\n";
else
std::cout << "Unknown command.\n";
}
}
Obviously, this little program will print a prompt (>>) and understand the commands exit and test and will print Unknown command. on all other commands.
For everything else, you probably want to learn some more about pattern matching or parsing; Google is your friend (take a look at bison for example and a good tutorial).
To parse your command line, you can use Boost.Program_options.

help for gdb's stepi command

I need to trace all instrutions of a program using gdb.
After every execution of a instruction, I want gdb invokes a specified function.
Is it a possiable work? How to achieve this?
I searched internet and found "stepi arg" command in gdb could step arg instructions.
But how to find total number of instructions?
After every instruction, how to make gdb to invoke my function automately?
cat t.c
int main() { int x=1; int y=2; int z=x+y; printf("%d",z); return 0; }
gcc t.c
gdb -q ./a.out
break main
run
(no debugging symbols found)...
Breakpoint 1, 0x0000000000400488 in main ()
set logging on
while 1
>stepi
>info registers
end
quit
Now examine gdb.log: it should contain the info you are seeking.
P.S. This isn't a discussion forum. Please don't append questions as "answers". Instead edit your original question to clarify it, or use comments.
GDB always prints "---Type to continue, or q to quit---" during execution because of the height or pagination parameter.
In order to avoid or disable this you have to give the following command either in gdb prompt or .gdbinit file
set height 0 or set pagination off