How to attach the second next process with lldb - lldb

Is is possible to let lldb to attach to the 2nd new process of some executable? We could attach the next new process of some executable with
attach -w -n "/path/to/executable"
But now an action will launch a pair of instances of some executable, I want to only attach to the 2nd process.

lldb doesn't have a "wait for second next version" primitive. But using the SB API's it would be pretty easy to write a Python command that does an attach wait, then detaches after the first attach and immediately waits for the next one.
When you attach to a process in the SB API, you create an SBAttachInfo specifying what you want to attach to. Be sure to call SetIgnoreExisting(True) on the second attach, that way it won't try to attach again to the first process. More details on writing Python based commands is here:
https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function
And the API reference is here:
https://lldb.llvm.org/python_reference/index.html

Related

How to stop a running program in C?

I have a problem.I've written a program in C for armbian.
I am using RTKLIB software for GPS data conversion from ubx to RTCM3.
I get some data from serial port and start str2str(rtklib software).
It creates this command to run
str2str -in tcpsvr://:2101#ubx -out serial://ttyS2:115200#rtcm3
and call system function to run this command. It is successful, but when I send a new command, I want it to stop the str2str software.
I've tried the exit(0) and it stops my software. I don't want to stop my software. I want to stop str2str and create a new command and run it again.
How can I do it? I am not good with the linux environment.
Thanks
I suggest you find out how to search for the str2str process you want to kill, and get the PID. A stackoverflow search will reveal this and then use the PID to kill the process. Unless RTKLIB has a process to do this directly.

make a C program relaunch itself after x seconds

Is it possible to make a program written in C to stop and then relaunch itself after x seconds In windows ?? And if yes, how to make it happen ??
You can accomplish that goal by having your program launch a second program, whose only function is to wait a while and then launch your first program again. In pseudocode, the idea would be:
Program A:
Do whatever the program is supposed to do
Launch program B
exit.
Program B:
Wait predetermined time
Launch program A
exit.
I hope this answers your question adequately.
The way I do this kind of thing is with a command-line option 'startDelay=xx'.
If there is no such command, my app just starts up as normal. If there is, its first action , before attempting to open any files, DB, construct GUI, start threads, start server etc. is to sleep for 'xx' seconds.
If my app needs to restart itself, it copies its own command-line, adds the 'startDelay=xx' to it and launches a new copy of itself, which then immediately sleeps. The original then has plenty of time to shut down normally before the new copy starts the bulk of its run-up.
No need for any other app or Windows scheduler and/or cron crap:)

Less Hacky Way Than Using System() Call?

So I have this old, nasty piece of C code that I inherited on this project from a software engineer that has moved on to greener pastures. The good news is... IT RUNS! Even better news is that it appears to be bug free.
The problem is that it was designed to run on a server with a set of start up parameters input on the command line. Now, there is a NEW requirement that this server is reconfigurable (didn't see that one coming...). Basically, if the server receives a command over UDP, it either starts this program, stops it, or restarts it with new start up parameters passed in via the UDP port.
Basically the code that I'm considering using to run the obfuscated program is something like this (sorry I don't have the actual source in front of me, it's 12:48AM and I can't sleep, so I hope the pseudo-code below will suffice):
//my "bad_process_manager"
int manage_process_of_doom() {
while(true) {
if (socket_has_received_data) {
int return_val = ParsePacket(packet_buffer);
// if statement ordering is just for demonstration, the real one isn't as ugly...
if (packet indicates shutdown) {
system("killall bad_process"); // process name is totally unique so I'm good?
} else if (packet indicates restart) {
system("killall bad_process"); // stop old configuration
// start with new parameters that were from UDP packet...
system("./my_bad_process -a new_param1 -b new_param2 &");
} else { // just start
system("./my_bad_process -a new_param1 -b new_param2 &");
}
}
}
So as a result of the system() calls that I have to make, I'm wondering if there's a neater way of doing so without all the system() calls. I want to make sure that I've exhausted all possible options without having to crack open the C file. I'm afraid that actually manipulating all these values on the fly would result in having to rewrite the whole file I've inherited since it was never designed to be configurable while the program is running.
Also, in terms of starting the process, am I correct to assume that throwing the "&" in the system() call will return immediately, just like I would get control of the terminal back if I ran that line from the command line? Finally, is there a way to ensure that stderr (and maybe even stdout) gets printed to the same terminal screen that the "manager" is running on?
Thanks in advance for your help.
What you need from the server:
Ideally your server process that you're controlling should be creating some sort of PID file. Also ideally, this server process should hold an exclusive lock on the PID file as long as it is still running. This allows us to know if the PID file is still valid or the server has died.
Receive shutdown message:
Try to get a lock on the PID file, if it succeeds, you have nothing to kill (the server has died, if you proceed to the kill regardless, you may kill the wrong process), just remove the old PID file.
If the lock fails, read the PID file and do a kill() on the PID, remove the old PID file.
Receive start message:
You'll need to fork() a new process, then choose your flavor of exec() to start the new server process. The server itself should of course recreate its PID file and take a lock on it.
Receive restart message:
Same as Shutdown followed by Start.

Get System Environmental Variables In C

I've written an application in C that is supposed to be reading environmental variables and handling those changes appropriately. When the application starts, I've set it up to go ahead and initialize the variable (to prevent any null pointers from returning) via setenv("MYVARIABLE", "TEST", 1).
This application loops often and, during those loops, one of its jobs is to check that global variable via getenv("MYVARIABLE").
The plan then was to have either a shell script or a python script change these environmental variables. The C application is full screen, so I have no way of testing this process without another terminal entry. In my other terminal (c2) I run commands such as:
MYVARIABLE="My New Value"
or
export MYVARIABLE="My New Value"
My application doesn't seem to catch the environmental update, though. Instead it continues to insist that MYVARIABLE is "test," and not "My New Value." I'm not sure why these two environments are separate, but I need them to work together. Does anyone know what I'm doing wrong?
My system is running Archlinux for anyone who is interested
The problem you're facing right now is that you've only set your variable in the local shell session's scope. EX:
(Assuming bash) When you set a variable such as:
MYVARIABLE="My New Value"
it only effects the current shell, when you set it as:
export MYVARIABLE="My New Value"
it effects the current shell and all processes started from the current shell.
If you set it in your .bashrc file it will set it permanently for all future sessions, but you'll need to source that file for it to work in your current session.
The best solution is to fork off a process. For example if your program is called a.out you can execute:
> ./a.out &
This will allow you to continue to work in the shell session while the program is running, then you can set variables in that same session.
The only other option I've ever seen is to force your shell session to "auto" source things as they come in:
1) In your first session enter:
trap 'source ~/.bashrc' DEBUG
2) Then start your program in the same session:
./a.out
3) In a second window edit your .bashrc file to add the new env var
4) in the second window source the new version of the file:
source ~/.bashrc
Now the first window running your program has the new var set to its session. I've used that before and I know it works, but I've never tried it on an applications that was already spawned.. but I suspect it should work for you.
Your process environment is not dynamically changeable!!!
Remember this main() prototype...
int main (int argc, char *argv[], char *envp[])
{
char *path;
/*
Searches in this process envp[] only,
There is no way it can access changes happening in Shell command prompt...
User shell does not communicate with this process, if not piped.
This will always return PATH, that was set at the time of starting.
*/
while (1)
{
path = getenv("PATH");
sleep (5);
printf("PATH = %s\n", path);
free(path);
};
}
Your understanding of getenv() library function is WRONG. To achieve your expectation, You need to use any form of IPC.
setenv() works with calling process own environment. That will be passed on to it own child process through fork(), exec() syscalls. This will will not do anything in the parent process, your shell.

output to a new terminal

Suppose I create a thread from my "main" thread, which aims to monitor and print some variables in "main". But "main" is also generating some output, so I want to print the outputs of these two threads separately. How can I redirect the output of the new thread to a new console other than the one "main" thread is using in my c program?
I would output the monitoring information to a file, then use tail -f filename to display it in my other terminal.
That has the advantage that you can run grep or what-have-you on the output as well.
I would go with writing the data to a log file and then using tail -f log_file.txt from another window to watch it since that way you get to keep a copy of the data, but if you decide that you need to use separate terminals then you can just open the new terminal (tty) like a regular file. The main problem with this is that you probably really want it to open a new terminal window for you as well.
Opening a new terminal window to do something like this is tricky and is different between different terminal emulators. Here is another question about opening new terminals from a make file. You could try doing the same thing from your program or from a script that runs your program and run cat or tail in the new terminal to be your log window.
#!/bin/sh
truncate --size=0 ./logfile.txt
xterm "tail -f ./logfile.txt" 2>&1 > /dev/null &
your_program --log-file=./logfile.txt
I don't currently know of a better way to accomplish this.
Another thing you might want to look into is syslog.
What would you expect to happen in such case (externally)?
If you run a program you are attached to a console. You of course don't have to write output to the console (you can use files), but the console cannot be duplicated obviously.
Isn't using a different file descriptor good enough for you?

Resources