How to set GDB debugging print options at once? [duplicate] - c

This question already has answers here:
How to save settings in gdb?
(2 answers)
Closed 3 years ago.
I use GDB very often to debug my C-program and I use the print options like :
set print pretty on
set print array-index on
But at moment I need to set these print options everytime I open a new GDB session.
I am looking for a way such that I can set these print options once forever so that print options are set in every new GDB session.
thanks

You probably want to use a gdbinit file on your home directory.
~/.gdbinit
See: gdbinit manpage
From the manpage:
These files contain GDB commands to automatically execute during GDB startup

Related

How to capture the output of a bash script from a C program? [duplicate]

This question already has answers here:
How can I run an external program from C and parse its output?
(8 answers)
Closed 7 years ago.
I want to save the output from a bash script which is invoked from a C program to a variable declared in the C program. I searched and tried successfully calling a script using system, and I tried this, but it didn't work:
char* a;
system("a=`ls`");
printf("%s",a);
Use popen() system call. You can pass the cmd as the parameter. You will get the command output as text when the function returns. Hope this helps.

How to get a program written in C to execute a command in Terminal or Command Prompt? [duplicate]

This question already has answers here:
Execute program from within a C program
(6 answers)
How do I execute a Shell built-in command with a C function?
(3 answers)
Closed 8 years ago.
I would like to write a program in C that can shutdown a computer. In order for me to do this, I must execute the command "shutdown -h now". This is to be done in the terminal or command prompt. How can I tell the program I would like it to execute the command in the command line?
There is a system-function, which basically does the c equivalent of the assembly systemcall:
It is located in
system("cls");
will clear the command screen for example.
To run a command in simple covenient way from C, use system :
system( "date");
That will execute the command under POSIX OSes, with the shell using your standard environment, so PATH is respected. Unfortunately, "shutdown -h now" requires privileges, so you may need to use sudo and enter a password.
You've got a bunch of functions which can do what you want:
system("shutdown -h now");
the exec* family : execlp("/sbin/shutdown", "shutdown", "-h", "now");
popen("shutdown -h now");
They all have advantages and drawbacks.
For example popen will return a FILE* which contains the output of the executed command.
Exec* will simply replace the memory area with your program by the one of the executed binary, this way if you want to continue the execution of your program you will have to use fork(2).
System is simply a helper function using fork and exec*.

How to send the syslog output to stdout? [duplicate]

This question already has answers here:
Is there a way to redirect syslog messages to stdout?
(2 answers)
Closed 8 years ago.
I m using syslog in myprograme to generate log messages.
Is there a way to send the syslog output of my program to stdout ?
I do not want to use the tail command to see my program log, I would like to see it directly on the console
You'll want to edit your /etc/syslog.conf file.
depending on exactly what facility you're sending to syslogd, you'll need to add a line something like this:
<facility>.debug /dev/console
be sure to check out man 5 syslog.conf for all the details..
To continuously clone file output to a console/shell use the following command in that console/shell:
tail -f <logfile> &
-f makes tail continue printing whatever gets written to the file
& puts the process in the background so you can do other stuff in the window. Omit the & if you want the console to block until you press ctrl+c.

output redirection produces empty file [duplicate]

This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.

How to write on a separate terminal using C in Linux? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to invoke another terminal for output programmatically in C in Linux
I am programming a client-server application and I want to create a debug window.
On the server side I want to print the incoming and outgoing communication on a separate terminal.
I am able to spawn a terminal through gnome-terminal but how to write on it and not on other terminals.
Unless you for some reason really need to print to a terminal, I wouldn't bother, not just for a debug printout.
I would have the server print to a log file (remembering to flush it appropriately often) and then use tail -f in another terminal to follow it. This has the added benefit of giving me a record of what the server debug-printed that I can examine at leisure.
Combining idea of #ibid idea to what you want. Write to log file and than execute:
xterm -e tail "-f" log_file
This will span xterm , which executes "tai -f log file" command.
The "correct" answer to this question is that you can write to /dev/ttyNUM... if you know the right tty number.
But that's only technical correctness, you should do something else. What you're trying to do is wrong.

Resources