gdb command to check the variable exist - linux

In GDB Scripting, how can I check if a stack variable exists?
I've GDB script to walk through stack and access variables (on stack).
But if the variable does not exist the script exits with the following error:
< No symbol "variable" in current context >
I was wondering if there is a gdb command to check if the variable exists? Is there a way to catch these exceptions and exit cleanly?

By far the simplest way to do this is to script gdb using Python. Python has been available for years now.
However, maybe it can be done in the ordinary gdb command language. It's not very scriptable but sometimes things can be done with tricks.
Since you're only looking at stack variables, I'd suggest redirecting the output of "info args" and "info locals" to a file. Then shell out to a script to rewrite this list into a new list of commands. By shelling out you can also easily filter out the not-found variables. Then, have gdb "source" this new list of commands to do whatever you like.
Let me reiterate, though, that this is 1000x simpler from Python. You can even take the quick-and-dirty approach and find the Python "ignore-errors" script -- this will let your script ignore errors from gdb commands.

Related

Launching Programs (example: Vim) from Haskell

Using the Turtle shell scripting library I am trying to launch a program, i.e:
shell "vim" empty
The problem is that this yields the warning Warning: Input is not from a terminal and causes Vim to lag for a few seconds before finally launching.
Questions:
Is shell the best Turtle function to launch an external program from haskell?
If so, is there any way to get around errors like the above?
You want to use functions from the process library, specifically createProcess or runProcess.
Relevant turtle thread on the issue here.
Example usage.
You could try manually setting up I/O to the vty. E.g. in bash: vim < $TTY > $TTY. I guess turtle is doing that with its own file descriptors under the hood, based on the warning, so you should be able to manually set up those redirects (or just use the command I gave via shell). You just need to make sure you've got a TTY environment var around.

Kill Linux processes based on commands present

I am learning Linux and need to write a shell script that will kill all processes whenever another command runs on it. I know how to write a script with a variable, but cannot seem to find ways of doing it for a command.
I would imagine that I need to find a way to evaluate if the command is true or not and to use that as the condition for an if statement. But anything I try returns errors.
Linux is the first CS related thing that I am learning and I am absolutely stuck at this step. I tried searching for this but am not quiet sure what to write.
It's the sh shell.
Edit: Whenever a program is executed with a certain command I need the script to terminate it right away.
In general you can use this construct in a bash conditional:
if [ "$(whoami)" -ne "0" ] then;
Where the whoami is an example for a an existing script. Obviously the same is possible with many other notations, not only the if conditional. Take a look at the test command, it is very helpful for such things.
For more details take a look at the "man page" bash brings along: man bash
The "Linux man pages" offer a wealth of information.

Launch interactive shell programmatically

I need to do a interactive shell programmatically (in Node.js, but any language will do).
The most important is that I want to start a REPL of any language (node, ruby, erlang etc.) and be able to get autocompletion
pri\t
which I want to get output in stderr (for instance)
print() println() printf()
And because it's in language REPL compgen cannot be used.
I've tried many ways but it seems that it fails because shell can't be interactive while opened programmatically.
Launchin /bin/sh -i just yeilds errors like:
bash: cannot set terminal process group (XXXX): Inappropriate ioctl for device
I've heard that I can't start interactive shell without the terminal, but when doing SSH autocompletion does work which means it can be emulated in some way.
I'd appreciate Your help. I can't continue my work without finding a solution to this.
You can emulate a terminal using a pseudoterminal. This is how ssh does it. There's a lot to using a pseudoterminal though and the SO answer box isn't big enough for all of it. So check the man page for pty(7).

How to track file creation and modification

We have put together a perl script that essentially looks at the argument that is being passed to it checks if is creating or modifying a file then it saves that in a mysql database so that it is easily accessible later. Here is the interesting part, how do I make this perl script run before all of the commands typed in the terminal. I need to make this dummy proof so people don't forget to run it.
Sorry I didn't formulate this question properly. What I want to do is prepend to each command such that each command will run like so "./run.pl ls" for example. That way I can track file changes if the command is mv or it creates an out file for example. The script pretty much takes care of that but I just don't know how to run it seamlessly to the user.
I am running ubuntu server with the bash terminal.
Thanks
If I understood correctly you need to execute a function before running every command, something similar to preexec and precmd in zsh.
Unfortunately bash doesn't have a native support for this but you can do it using DEBUG trap.
Here is a sample code applying this method.
This page also provide some useful information.
You can modify the ~/.bashrc file and launch your script there. Do note that each user would (and should) still have the privelege to modify this file, potentially removing the script invocation.
The /etc/bash.bashrc file is system-wide and only changeable by root.
These .bashrcs are executed when a new instance of bash is created (e.g. new terminal).
It is not the same as sh, the system shell, that is dash on Ubuntu systems.

Why is exported variable blank after script is over?

I have a simple command in a Linux shell script (say foo.sh). In it I do this:
export INSTALL_DIR=/mnt/share/TEST_Linux
I run the script with:
> sh foo.sh
When it finishes I try to get the variable but the value is blank.
> echo $INSTALL_DIR
If I type the command directly the exported var becomes global to the opened terminal window. I'm using Ubuntu.
Setting environment variables is local to the child bash process running your script. To achieve what you want, you need to source it like this: source foo.sh. It means that it's run by your main bash process. Then, the setting of a variable will remain after the script is finished.
The variable is exported only in the new shell you are starting. You probably want to execute your script with source.
source foo.sh
I don't know the answer but i know how to overcome it.
# source ./foo.sh
# echo $INSTALL_DIR
And it's like magic.
I think it's because that script gets executed in it's own "shell". Not sure.
Because the process you are running (the shell running your script) can do whatever it wants, but its actions won't affect the parent process (your current shell).
A somewhat weird analogy would be: I can take 5 tequila shots and my environment will become blurry and gravity laws would be affected according to my perception. But to my father, his environment is the same, he doesn't get drunk because of my actions.
If you want that variables created/altered in your script affect your current shell, you should source the script as other answers pointed out. Please do note that doing this also may change the resulting working dir in your shell if the script does cd /whatever/path, that any other functions setted, but also altered or removed, would get affected in the same way in your shell.
A really weird and not very good analogy would be if I take 5 tekila shots and then my father kills me and drinks my blood.
Am I disturbed or what? ;-)

Resources