I want to write a ruby program that could inspect the variable value of a program by launch the gdb and then print that value. How could I achieve this?
If I were in the shell, I would do this :
shell > gdb test
[...gbd started]
(gdb) p variable
$1 = 20
(gdb)
I also like to hear other ways that can achieve the same goal, not necessaliy to use gdb.
Try open4, which allows you to open stdin, stdout, stderr to an arbitrary command. You could use open4 to run gdb exactly as you describe, I have done this personally and it works quite nicely. Might want to create a wrapper class that handles running commands and returning status, as open4 just gives you file handles.
Related
In command line it is possible to use the output of a command as the stdin of an executable. For example, pbpaste returns the value of the clipboard on OSX. I could run a program using this, e.g. pbpaste | ./program
Is this also possible in LLDB?
lldb only has access to a program's stdio if it launched the program and is sharing the terminal with it. So you can't always do this.
There isn't an lldb command to send text to the debuggee's stdin, but you can write to the process stdin (when that's possible) from Python using SBProcess.PutSTDIN:
https://lldb.llvm.org/python_api/lldb.SBProcess.html#lldb.SBProcess.PutSTDIN
So you could pretty easily cons up Python command that runs the shell command you want, gets the output, and uses this API to write it to the target.
Lazarus can run bash scripts and commands. How to get the output of an executed command as string and later use it, for example print it with ShowMessage? Thanks!
Summary:
use the tprocess class from unit process, that allows to trap console output using pipes.
for straightforward cases use the Runcommand helper functions (also in process, wrap tprocess for simple cases)
Be aware that while you see console output as one stream, in fact there might be two (stdout and stderr)
I am designing a ruby program that needs to run a command and store it a variable.
var = exec('some command');
This doesn't work the way I want it to, it just prints the output from the command prompt and then ends the program.
So is there a function that doesn't end the program, doesn't print the cmd output and stores the information in a variable?
Thanks in advance.
You need to use either Ruby's built in backtick syntax, or use %x
output = `some command`
or
output = %x(some "command")
Open3 grants you access to stdin, stdout, stderr and a thread to wait
the child process when running another program. You can specify
various attributes, redirections, current directory, etc., of the
program as Process.spawn.
See the various ways of executing a command
I'm trying this in ruby.
I have a shell script to which I can pass a command which will be executed by the shell after some initial environment variables have been set. So in ruby code I'm doing this..
# ruby code
my_results = `some_script -allow username -cmd "perform_action"`
The issue is that since the script "some_script" runs "perform_action" in it's own environment, I'm not seeing the result when i output the variable "my_results". So a ruby puts of "my_results" just gives me some initial comments before the script processes the command "perform_action".
Any clues how I can get the output of perform_action into "my_results"?
Thanks.
The backticks will only capture stdout. If you are redirecting stdout, or writing to any other handle (like stderr), it will not show up in its output; otherwise, it should. Whether something goes into stdout or not is not dependent on an environment, only on redirection or direct writing to a different handle.
Try to see whether your script actually prints to stdout from shell:
$ some_script -allow username -cmd "perform_action" > just_stdout.log
$ cat just_stdout.log
In any case, this is not a Ruby question. (Or at least it isn't if I understood you correctly.) You would get the same answer for any language.
My Ruby script is running a shell command and parsing the output from it. However, it seems the command is first executed and output saved in an array. I would like to be able to access the output lines in real time just as they are printed. I've played around with threads, but haven't got it to work. Any suggestions?
You are looking for pipes. Here is an example:
# This example runs the netstat command via a pipe
# and processes the data in Ruby as it come back
pipe = IO.popen("netstat 3")
while (line = pipe.gets)
print line
print "and"
end
When call methods/functions to run system/shell commands, your interpreter spawns another process to run it and waits for it to finish, then gives you the output.
Even if you use threads, the only thing that you would accomplish is not letting your program to hang while the command is run, but you still won't get the output till its done.
I think you can accomplish that with pipes, but I am not sure how.
#Marcel got it.