Unable to input data to C program while debugging in separate terminal with gdb & tty command - c

I am debugging a C program with gdb. I have used the tty command to send the output to a new terminal window but am unable to enter input while the program is running.
If I debug in the same window it works fine, but when using a separate terminal window, input doesn't do anything.
I can still kill the process with ctr-c but once I hit the input line, it waits for input and doesn't do anything when I press return.
I have looked around but am haven't found the same problem online. Any ideas?

Start your program in one window. While your program is waiting for input, start gdb in another window. Use the gdb attach command to attach to and debug your program.

Related

How do I make a C executable for Windows written on Linux not close the cmd as soon as it finishes running?

So, I've written a C executable on Linux for Windows using mingw32-gcc. It is a basic Input-Output program, you type an input and get an answer.
Problem is the cmd shuts down immediately, so the user can't see the output.
Assuming I cannot use Windows to edit the executable there, what should I change in my code/ what flags should I use when compiling it?
inb4:
the user is supposed to click and run it, so running it from cmd won't help.
adding getchar()/scanf() at the end of my code doesn't work, and it feels a bit like cheating.
SOLVED: so all I had to do was to actually add a getchar() after every scanf() and one more at the end of the code for the user input to close the cmd.
Waiting for input at the end is not cheating, but common practice. How else should program know for how long it should stay opened? Closing program by closing console window directly is more cheating than waiting for user input to finish.
You can for example prompt user to hit any key like Press any key to exit... or something similar.
If you want some specific delay, you can use Sleep() from windows.h instead of waiting for input.
See https://stackoverflow.com/a/3379146/3035795

GDB monitor inside TMUX garbled

I was debugging in terminal using gdb, and I typed inlayout src,then I entered the UI mode.
Most of the time, it worked fine, but sometimes, the whole terminal text code got mixed.(I post the picture below.).
what's more, sometimes I typed in some shell command in gdb like !pmap <pid>, its output's format was also strange.
I tried to set TERM to screen-256color but it doesn't work.
I solved it in some sense.
I redirect the output of my program to another tty.

using gdb to debug a interactive program that reads input from stdin

I'm writing a client/server program in C.
My client has a thread reading input from stdin, it's just a while(1) loop to read input from stdin. Whenever it reads a line, it deliver it to another thread that handles message parsing and framing.
As I enter gdb, the command line is occupied by gdb prompt and I can no longer input lines into stdin.
Is there a way to do it? (I don't want to redirect stdin to an input file because I've tried this method and it didn't work)
Run your program in one terminal and attach to it from gdb in another terminal.
To attach to a running program, find the process ID (PID) of the program you want to attach to, then execute gdb <executable> <PID>.
As an addition to Jonathan Reinhart's answer, here is a oneliner to attach to a running program by name:
gdb -p $(pgrep <executable-name>)
As a clarification you don't need the executable name if you do know the process id of the program. This will allow you to attach a program directly.
gdb -p PID

Opening a new terminal window & executing a command

I've been trying to open a new terminal window from my application and execute a command on this second window as specified by the user. I've built some debugging software and I would like to execute the user's program on a separate window so my debugging output doesn't get intermixed with the programs output.
I am using fork() and exec(). The command I am executing is gnome-terminal -e 'the program to be executed'.
I have 2 questions:
Calling gnome-terminal means the user has to be running a gnome graphical environment. Is there a more cross-platform command to use (I am only interested in Linux machines though)?
After the command finishes executing the second terminal also finishes executing and closes. Is there any way to pause it, or just let it continue normal operation by waiting for input?
You probably want something like xterm -hold.
1) gnome-terminal should work reasonably also without the whole gnome environonment, anyway the old plain "xterm" is enough.
2) you can execute a short bash script that launch your program and at the end reads a line:
bash -c 'my program ... ; read a'
(or also 'xterm -e ...')

Why does my program's output flash and close in Windows?

I'm trying to build an .exe file for the K&R "Hello, world". The code given in the book is:
#include <stdio.h>
main()
{
printf("Hello, world!\n");
}
When I build & run from Code::Blocks (under Windows XP), I get the prompt window with the "hello world" message. It stays open until I close it manually. However, when I double click the .exe file, the prompt just flashes and disappears, why is that?
No one is explicitly telling you this, so I will:
What you see when you double-click the file is normal. What your IDE does (keeping the window open) is a feature to help you debug the application.
Why is this so?
Since you're developing a console application, there has to be a console for your application to display its output on. If there is none yet, a new console is created (which is the black window).
If you launch your program from inside a console (say, from cmd.exe), it will just inherit the console of the parent without creating a new one[1].
After the last application using the console exits (which, in the first case is just your program), the console closes. You will notice this all the time for console applications that print nothing but a help text when run without parameters. If you double-click them from explorer, a black window with some text will flash and then immediately close.
Sometimes, a program that does something and them immediately closes is what you want. For example, you can call these applications from scripts.
On the other hand, your application could be interactive: waiting for user input, doing some thing, and only exiting when the user tells it to. You cannot script these applications, obviously, as you will need to have a human present at the keyboard to tell the application what to do.
Now we get to the IDE part: let's say you're developing an application of the first kind, one that does something and then immediately closes. It's not very convenient to have the screen flash and disappear every time you run it, because how can you tell if the program worked? Assuming you can tell this from the output it generates.
You could of course start a command-line window and run the application from there, but the program would execute separately from the IDE, and you would lose live debugging capabilities.
So, IDE makers came up with a feature for console applications: when you run the application directly from your IDE, they afterwards, usually waiting for a keypress. This gives you the opportunity to inspect the window with the output, to confirm that the application is working properly.
[1] Esoterica: unless you go through an application that does not inherit the console. Any console app launched by that application will not inherit the console, since the inheritance was broken by the GUI app. For example, start.exe does this. Compare:
foo.exe (inherits the console)
start foo.exe (start.exe is a GUI app, so foo.exe is launched in a new console)
If you're not running a command line exe from an already open command line window, Windows will automatically close the windows after the program has terminated. Try opening cmd.exe, navigating to your program's directory and running it from there, the window should stay open.
When running from IDE's like this, they run the program and when its done running, they close it.
Since your program's only function is to print out a value, it does that and closes.
You should try to add something that asks for user input at the end or compile it into an .exe and run itself from the command line yourself.
Since you are starting I would recommend to just run it from the command line yourself. You will eventually learn about user input and there you can have the command line window open when you use your program.
Normal behavior.
Your program executes every action in the order of the main. So it prints, and then moves on to the next operation, there is none, so it exits. Since the console window is tied to your .exe, the command window closes with the program.
If you don't want your program to exit right away, you can make it sleep, or wait for user input before exiting.
When double clicking a .exe in Windows, you are launching a new process. Windows has 2 basic process types: Window and Command line. The hello world sample you've written is a command line process.
A command line process will launch a new command window on startup. This is the window that pops up which is largely a black background with white text. Upon completion of a program the window will close down.
Add getch(); before the closing brace. This will prompt for an input after the output is printed. Once you key in a character the window will close. This should solve your problem.
The preferred solution is to run the executable from the command line.
Try running your binary from the command line.
That is because the executable file opens its own dialog box. When the executable has completed running it shuts down the dialog box that it opened in order to run. However, when YOU are the one that opened the dialog box, it disappears when YOU close it.
So if you were to open up a command prompt and then run the executable, the dialog box would not automatically close.
That is because from the executable, it executes your code in a new window and then the process is done, it has no reason to stay open, what you wanted to do is complete. There are a couple of things you can do. You can execute it from the cmd.exe command line, or you could even put something at the end of your code that listens for a key press, and once the key stroke is detected, allow the program to exit.
just add
system("pause");
line before return. it`s not the best, but universal method.
Here is my take on this:
// Hello sweetie (Spoilers)
#include <iostream>
using namespace std;
int main()
{
// Print the text to screen
cout << "************************************";
cout << "\n";
cout << "Hello World!";
cout << "\n";
cout << "You may close me by pressing Enter";
cout << "\n";
cout << "************************************";
cout << "\n";
cout << "\n";
/*
This will prompt for an input after the output is printed.
Once you hit the Enter key the window will close.
*/
if (cin.get() == '\n')
return 0;
}

Resources