Gdb Buffer Overflow; Python won't execute - python

I have a problem with gdb, I cant make a python command run inside. It just hangs forever until i press enter a second time.
gdb$ run $(python -c "print('A'*50)");
Starting program: /home/Myprogram $(python -c "print('A'*50)");
[buf]:
[check] 0x4030201
[Inferior 1 (process 27229) exited normally]
--------------------------------------------------------------------------[regs]
EAX:Error while running hook_stop:
No registers.
I went searching and each time someone uses this same command :
Starting program: /home/Myprogram $(python -c "print('A'*50)");
They have a line 50 'A's just below.
the second part (after [buf] : ) is only shown if i press enter a second time. If i do nothing it just hangs and doesn't execute python.
Any advices?

I finally found the solution :
echo "your python code" > bla.py
python bla.py > output
gdb yourProgram
In gdb :
gdb$ run < output

Related

How to send keys to a python pdb shell from bash script?

I am trying to create a bash script that runs a python3 script with pdb trace set. As such, in my bash script I have the following lines:
python3 path/to/my_script.py
n
What I want to happen is for bash to run the python script, which will then open the python debugger. Then, the bash script will send the key 'n' to the pdb shell so that pdb executes the first line in the python script.
The script does not work as expected and bash waits until the python script has completed (or exited) to execute 'n' in the command line which just opens node.
I thought this might be a problem unique to pdb shells so I tried executing the following in bash:
python3
print("Hello")
However, again, we observe that the script creates a python3 shell and then waits for the shell to exit before executing print("Hello") in the terminal. I understand that I could use python3 -c for this case, but that does not address the case of passing commands to the pdb shell in the context of the running script.
Is there any way to send the 'n' command to the pdb shell that the python script generates?
Your code will try to run two commands. First, it will run your python script, then it will try to run a command called n. Assuming your script needs you read from stdin you can do one of the following:
Use a herestring:
python3 path/to/my_script.py <<< "n"
Use a pipeline:
One example echo "n" | python3 path/to/my_script.py
Echo is not the only command you can use. You can also use printf or even yes for this use case.
You can use a coprocess to send and receive from pdb.
#! /bin/bash
send() {
echo "$1" >&${PDB[1]}
}
recv() {
IFS= read -r -u ${PDB[0]} line
echo $line
}
coproc PDB { /path/to/my_script.py; }
send 'n'
recv
#...
send 'n'
recv

bash script in rc.local different result than executing it in terminal

I'm trying to run a python script from a headless raspberrypi that uses the pynput library.
In order to use pynput on a headleass machine it requires you to do the following steps on every bootup:
export DISPLAY=:0
DISPLAY=:0 python -c 'import pynput'
If I don't do this I get the following error:
ImportError: this platform is not supported: ('failed to acquire X
connection: >
Try one of the following resolutions:
Please make sure that you have an X server running, and that the
DISPLAY env>
If I now try to automaticate this process through a bash script that contains the following lines:
#!/bin/bash
sleep 60
export DISPLAY=:0
sudo chmod 666 /dev/hidg0
DISPLAY=:0 python -c 'import pynput'
python3 /home/pi/Hid.py |& tee -a /home/pi/test.log
exit 0
And execute the file normally like this from terminal:
bash /home/pi/bash.sh
it works perfectly fine.
But when I mention the script in the rc.local file the same way I executed it before, the script is getting executed on boot, but the python script throws the error that was shown at the beginning.
How can there be two different outcomes, when it's getting executed by hand and when it's getting executed by a bash script at boot via. rc.local?
Any help is appreciated!
All commands from rc.local are getting executed by user root.
To execute them from a different user use:
sudo su USERNAME -c 'SCRIPT/PATH'

Preserve return code from python script in Jenkins when used in a bash pipe

Consider this:
$ cat test.py
import sys
print "Doing something ..."
sys.exit(1)
$ python test.py
Doing something ...
$ echo $?
1
$ python test.py | tee log # used in Jenkins, but I need to capture rc of 1 as well
Doing something ...
$ echo $?
0
As you can see I am unable to capture the return code of 1 from the python script if I pipe the output to tee. Is there a way I can accomplish this ? This code is in Build->execute shell section in Jenkins.
Since I am unable to capture the rc of 1 the commands following the line continue to get executed, which I don't want to happen.
In bash you can use the PIPESTATUS array to get the exit status of each command in a pipeline:
python test.py | tee log
echo ${PIPESTATUS[0]}
Regarding "Since I am unable to capture the rc of 1 the commands following the line continue to get executed", you can add set -o errexit and set -o pipefail to the start of your script, it will then terminate directly if you get an error (even inside a piped command). Here a good resource with a more in-depth explanation.

Python 2.7 raw_input() EOF error in Gambas3- Raspberry Pi/Jessie OS

The code,
print 'hello world'
raw_input()
"works" when ran in python idle 2.7 as well as in lxterminal ("python helloworld.py").
I then made an application thru Gambas3 using it as an interpreter for python programming. When simulating, everything works and the lxterminal shows the output and exits when the enter key was pressed. After compiling, making the software as an executable. Ran the application and ran the code it throws the EOF error. I searched the web, found lots of similar problems but different approach. I read something about pipe line something which I couldn't understand. I also added the python usage options -i -tt and -c but no luck. Any idea whats causing this error?
This was also my code for run button in Gambas3 incase someone ask.
Public Sub RunBtn_Click()
TextArea3.Text = ""
Try Kill Application.Path & "tempfile.py"
If Exist(Application.Path & "tempfile.py") Then
hFile = Open Application.Path &/ "tempfile.py" For Write Append
Else
hFile = Open Application.Path &/ "tempfile.py" For Write Create
Endif
Print #hFile, TextArea2.text
Wait 0.3
MyProc = Shell "lxterminal --command python -i -tt -c " & Application.Path &/ "tempfile.py" For Read As "Process"
MyProc1 = Shell "python -i -tt -c " & Application.Path &/ "tempfile.py" For Read As "Process"
End

How limit time of execution and memory limit while execution of program

I am writing a python script to execute a program ( C, C++,Python, Java,C#,...).
I want to make sure the execution is performed within a fixed time limit and using a limited amount of memory.
How can I do this in Python or bash?
Currently the Python I am using is this:
os.system("time ./"+filename+"< input.txt >out.txt")
OK I worked out how to do it, run the following in python and you will see it work, it should fail with malloc for the second command:
import os
commandStr = 'timeout 4 bash -c "ulimit -v 100000000000; python -c \"[5]*10000\""'
print 'Running command with large ulimit'
os.system(commandStr)
commandStr = 'timeout 4 bash -c "ulimit -v 10; python -c \"[5]*10000\""'
print 'Running command with low ulimit'
os.system(commandStr)
The argument passed to ulimit is in kB

Categories

Resources