I have a bash script which calls another bash script which calls a (very fast) executable. I would like to know if there's a way of attaching gdb to the executable without modifying the second script or without attaching to PID (given the very quick execution).
The only way I can imagine is to rename your executable (or change the PATH to get same result if you can not modify your exe name) and wrap it with a script that has your executable former name (or which is first in the PATH order) which call gdb. The bash script should be something like :
#!/bin/bash
gdb -q -x gdbCommandsFile --args "$#"
with at least 'run' in gdbCommandsFile, depending on what you want to do ...
my2c
Related
I do not see native GDB commands can do this: check if a gdb script file exists, if so, source the file.
Maybe I need to resort to python, but still want to ask here.
The use case is, I am working on several different computing environments, each one has some different GDB setup. I like to add the above to ~/.gdbinit so that even in each environment I have a different GDB script, it is automatically sourced in my ~/.gdbinit.
There's no built-in way to do this. It can be done via scripting in a couple of different ways.
The classic way is to use shell to test the file existence and have it write out a file that then decides what to do. Something like:
(gdb) shell if test -f blah; then echo source blah; fi > /tmp/F
(gdb) source /tmp/F
This is pretty ugly, but it would work.
Alternatively you can write a Python command to do this quite easily. Or you can dig up the Python ignore-errors command, and just use ignore-errors source whatever.
I have a bunch of qmake-generated makefiles each of which call make inside them (recursive make) in a chained manner.
After my build is over, the qmake-generated makefiles are all on disk so you'd think I could just call make on one of them if I wanted to 'replay' one particular makefile. Wrong.
When I try make-ing one, it fails, probably because there's a bunch of (environment) variables that it normally inherits from the calling makefile during the normal build.
Except for the variables, each qmake-generated makefile is pretty self-contained.
QUESTION
How can I simulate the 'normal' environment for a given recursive make so that I can call it in isolation?
I'm thinking I'd have to do something with the --print-data-base output: parse it and then call make with the same vars and values it had during the normal build.
WHY
I'm doing this because I need to modify the compile commands for ONE makefile but it's all controlled by the top-level .conf and I'm getting in way too deep.
I assume the problem is that you need to find this information before you get a chance to make any changes to the generated makefiles. Therefore, this solution is focused on shell commands (I'm assuming you're on Linux, since you don't say).
After starting your build, the first time, use something like:
ps -ef | head -1
ps -ef | grep make
to find make processes involved in the build. The PID column lists the Process ID of the make process, while the PPID column lists that of its parent. Use this information to find the top-level make process. Then, run:
strings /proc/<pid>/environ | sort > /tmp/make_env
env | sort > /tmp/normal_env
diff /tmp/normal_env /tmp/make_env
This will show you how the make process' environment differs from that of your current shell.
Now, that might not solve your problem, because GNU Make allows variables to be specified as commandline parameters. So, you should also check how it's being run:
strings /proc/<pid>/cmdline
That will print each commandline argument of <pid>, on a separate line.
BTW, when variables are passed to GNU Make via commandline arguments, they're handled by overriding any instance of the same variable, that might be contained within the makefile.
Within a makefile, you can see its environment using:
$(info My environment is $(shell env))
You can see its commandline options using:
$(info MAKEFLAGS = $(MAKEFLAGS))
If you only want to see the overrides, use MAKEOVERRIDES:
$(info MAKEOVERRIDES = $(MAKEOVERRIDES))
Finally, the targets can be seen using:
$(info MAKECMDGOALS = $(MAKECMDGOALS))
I want to create one unix command, which will unzip the folder.
so, I am searching for the code, but I am not aware that how should I use such code to make Unix command?
I have gone through various questions & answers but I don't get any perfect information.
So, can any one please suggest me any code (in C++ or C or any language to make exe) and to use it as a Unix command.
NOTE: I know command like 'unzip' is available in 'Mks toolkit' type of software but we can not use it, so I want to make command which can run through 'command prompt'
If you want to add a command, you only need to create your executable and put its link in the /usr/bin folder.
Just compile your code and set a link to it's executable like this:
ln -s /path/to/your_executable /usr/bin/command_name
If there exists a command that you need to modify, you should set an alias to it. For example, you want ls -1 to run whenever ls is used, then you only need to use the command:
alias ls=ls -1
or put the same command in the .bashrc file in your home directory.
I want to use the Windows CMD tree command in my C++ console application. My code:
system("cd c:/");
system("tree");
The problem is that the command tree will execute on the folder path where the program is running and not on C://. Is there a way to fix this?
Why not :
system("tree c:\");
?
TREE [drive:][path] [/F] [/A]
/F Display the names of the files in each folder.
/A Use ASCII instead of extended characters.
You can use SetCurrentDirectory from windows.h. This page has a demonstration:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806%28v=vs.85%29.aspx
Your problem is that the system("cd c:/") is executed in a shell, and then the shell exits. [It's also wrong, because you use the wrong kind of slash, it should be "cd c:\\" - the double backslash is needed to make one backslash in the output, assuming we're talking about a Windows system].
There are a couple of different ways to do this:
Use chdir() (or SetCurrentDirectory) function call to change the main processes current working directory, and then call system("..."). This is the simplest solution.
Generate all your commands into a batch file, then pass the batch file to system.
Open a command shell with something like _popen() and pass commands into the pipe that you get from that.
Manually create pipes and connect them to the standard in and standard out of a process that runs the command prompt.
Just for programs in Windows, include "windows.h", then
SetCurrentDirectory("c:/");
system("pwd");
While I'm still curious why would you want to do this, you can try to run all commands in one system() call:
system("cd c: && c: && tree");
Second c: is needed to change drive letter, in case if you're not currently on drive c: (because cd doesn't do it).
I wanna create a exe which has the Shell Script and Simple CPP file which calls the Shell Script using system() function. Lets say exe name 'myInstaller' which has files myintsaller.cpp and myShell.sh. When i run the exe myInstaller , it must execute shell script. I want to do like this so i can protect my Shell Script code ,which has over 3000 lines of Code.
How do i do this ... I m in real need of this.
As far as i've searched, there is no way to bundle the script inside the exe. Either it would be Shell Compiler as shelter suggested or plain shell scripts. No work around for this.
I guess it's Windows platform since you speak about exe. In this case you can add your script as a resource to your exe, extract it in run-time and execute.
You could keep the script as a string in your C++ code and call system("sh -c '" + code + "'"). Quoting may be an issue though. Otherwise, write it to a temp location, execute it and then unlink it.