lldb cannot connect to debugserver using python API - remote-debugging

I'm trying to connect to debugserver on Mac, using lldb. While the following set of commands work fine and in the lldb cli, the same set of commands fails with error: the platform is not currently connected when run from python using the debugger.GetCommandInterpreter().HandleCommand.
I also tried using the direct Python APIs without much luck. Though, I'm interested to learn more about the difference and making it work with interpreter in python.enter code here
For more context: https://github.com/vadimcn/vscode-lldb/issues/411
Start debug server:
$ lldb/bin/debugserver 127.0.0.1:1234
...
lldb CLI (works):
(lldb) platform select remote-macosx
(lldb) target create ~/Projects/vscode-lldb/build/debuggee/debuggee
(lldb) process connect connect://127.0.0.1:1234
(lldb) process launch
Process 64302 launched: '/Users/nisarg/Projects/vscode-lldb/build/debuggee/debuggee' (x86_64)
...
Python (error: the platform is not currently connected):
>>> import sys; sys.path.append('/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python3')
>>> from lldb import *
>>> debugger = SBDebugger.Create()
>>> inter = debugger.GetCommandInterpreter()
>>> res = SBCommandReturnObject()
>>> commands = ['platform select remote-macosx', 'target create /Users/nisarg/Projects/vscode-lldb/build/debuggee/debuggee', 'process connect connect://127.0.0.1:1234', 'process launch']
>>> for command in commands: inter.HandleCommand(command, res); print(command); print(res.Succeeded()); print(res.GetOutput(), "\n", res.GetError())
...
2
platform select remote-macosx
True
Platform: remote-macosx
Connected: no
1
target create /Users/nisarg/Projects/vscode-lldb/build/debuggee/debuggee
True
Current executable set to '/Users/nisarg/Projects/vscode-lldb/build/debuggee/debuggee' (x86_64).
1
process connect connect://127.0.0.1:1234
True
There is a running process, kill it and restart?: [Y/n] y
6
process launch
False
error: the platform is not currently connected
The same behaviour is observed when trying to connect to debugserver running locally on Mac or forwarded debugserver from iOS.

Related

LLDB SBProcess stuck on launching Ubuntu 18.04

I'm getting started with LLDB and am following the tutorial here: https://lldb.llvm.org/python_reference/lldb.SBDebugger-class.html. Whenever I Launch a target the process gets stuck in the launching state and will never launch. I've tried lldb in bash and it works perfectly. I've read through the documentation with the tutorial and can't find an explanation about the launching state, or how to discover what is causing it to get stuck.
System is Ubuntu 18.04 64 bit dual core 8GB RAM using python2.7.
The most simple code to reproduce the error is as follows:
import lldb
import os
exe = './a.out'
db = lldb.SBDebugger.Create()
db.SetAsync(False)
target = db.CreateTargetWithFileAndArch(exe, 'x86_64-linux-gnu')
pro = target.LaunchSimple(None,None,os.getcwd())
print(pro)
Running the python code will always give me the following output:
SBProcess: pid = 0, state = launching, threads = 0, executable = a.out
The a.out is just a heloworld.cpp compiled with clang++ -fstandalone-debug
UPDATE:
Thanks to Jim's suggestion I tried SBTarget.Launch() instead and used the error to discover that lldb could not find lldb-server-6.0.0 because it installs as lldb-server-6.0
The Fix
sudo ln -s /usr/bin/lldb-server6.0 /usr/bin/lldb-server6.0.0

How can i execute tcl command [using tclsh prompt] remotely using python 2.7 module?

Here is my set up.
Windows PC1 where Python 2.7 code is running ----> Windows PC2 where tcl is installed.
Windows PC1 and Windows PC2 are connected through LAN and able to access each other.
Now i want to open CMD line (As administrator) of PC2 from PC1 using python and execute windows command.
Yes this can be achieved through paramiko.
Now i want to run "tclsh" command in PC2 command prompt. After this the command line goes inside tcl prompt [%].Here i can execute tcl command like puts "Welcome". So if i try to execute "tclsh" command through paramiko its is in waiting state as the prompt has been changed from windows prompt to %.
My intention is to run tcl command in tclsh prompt [PC2 cmd line] through python [PC1].
Example: From PC1 i want to execute "puts "Hello" to PC2 tcl prompt through python2.7.
Please suggest me some idea what can be used [python module or any other approach] to access tclsh prompt through python 2.7.
Through paramiko normal windows commands are working as expected but tclsh enter into % prompt so paramiko is not working in this scenario.
I want to get access to tcl prompt through python 2.7 remotely.
I want to execute tcl command inside tclsh prompt in remote system through python.Here is the small program i have tried .
host = '192.168.1.4'
user = 'lenovo'
passw = 'XXX'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, password=passw)
stdin, stdout, stderr = ssh.exec_command('tclsh')
print (str('.'.join(stdout.readlines())))
print (str('.'.join(stdin.readlines())))
print (str('.'.join(stderr.readlines())))
But i do not see any output as tclsh goes into new % prompt. How do i solve this? After i have access to tclsh % prompt i want to execute "puts "Welcome""
.Instead of tclsh if give "hostname" windows command i get reply.
I got the solutions to access tclsh prompt for a remote windows pc using pramiko in python 2.7.
Step1: Use invoke shell method for tclsh command for the first time and store the return of invoke_shell to a globa variable.Suppose the return is x.
Step2: Now for any other command for tclsh prompt [like cmd = puts "stack"] use like x.send(cmd+"\n")
Step3: Now we can get output buffer like x.recv(9999)
Note [For windows only]: x.recv(9999) returns actual output with many escape sequences. We need to clean that to get the actual data.

Can't download a file via os.system('scp (...)') whithin a python script launched via a init script

I'm trying to download a remote file using the scp command called by os.system() in a Python script (scp.py) started as a service by a procd init script.
This script is running on OpenWrt 15.05.1 (which uses BusyBox to implement the shell environment). SCP itself is handled by dropbear SSH (https://matt.ucc.asn.au/dropbear/dropbear.html)
The code is quite simple :
import os
import logging
logging.basicConfig(level=logging.DEBUG, filename='/usr/local/www/log/scp_test.log', filemode = 'w')
rc = os.system('scp -i /root/.ssh/id_rsa root#vps500141.ovh.net:/root/simon/test /tmp/hello')
error_code, signal = rc >> 8, rc & 0xFF
logging.debug('error_code -> {}'.format(error_code))
logging.debug('signal -> {}'.format(signal))
When launched in the terminal using the command "python scp.py", the program's behave well. The download is successful and produces the following output :
root#S096C08:/usr/local/bin# python scp.py
test 100% 24 0.0KB/s 00:00
root#S096C08:/usr/local/bin# cat /usr/local/www/log/scp_test.log
DEBUG:root:error_code -> 0
DEBUG:root:signal -> 0
But this program fails to download anything when launched as a service using the command /etc/init.d/scp_test start
root#S096C08:/usr/local/bin# /etc/init.d/scp_test start
root#S096C08:/usr/local/bin# cat /usr/local/www/log/scp_test.log
DEBUG:root:error_code -> 1
DEBUG:root:signal -> 0
Furthermore, a quick investigation of the syslogs using the logread command shows this :
Wed May 15 10:24:58 2019 daemon.err python[3156]: Host 'vps500141.ovh.net' is not in the trusted hosts file.
Wed May 15 10:24:58 2019 daemon.err python[3156]: (ssh-rsa fingerprint md5 41:aa:2b:57:48:be:01:81:48:a3:d0:ac:b6:56:16:34)
Wed May 15 10:24:58 2019 daemon.err python[3156]: Do you want to continue connecting? (y/n)
Wed May 15 10:24:58 2019 daemon.err python[3156]: /usr/bin/dbclient: Connection to root#vps500141.ovh.net:22 exited: Didn't validate host key
vps500141.ovh.net already have an entry in /root/.ssh/known_hosts so this is quite surprising that it is not found when the script is running.
I've also tried to add the "-o StrictHostKeyChecking=no" option to the scp command but it doesn't seems to work either.
NB : this is the content of the init script /etc/init.d/scp_test
#!/bin/sh /etc/rc.common
# Copyright (C) 2015 CZ.NIC z.s.p.o. (http://www.nic.cz/)
START=99
STOP=0
USE_PROCD=1
SCRIPT="/usr/local/bin/scp.py"
start_service() {
procd_open_instance
procd_set_param user root
procd_set_param command python "$SCRIPT"
procd_set_param stderr 1
procd_close_instance
}
Problem resolved after moving /root/.ssh/known_hosts to /.ssh/known_hosts

create vm using pyvbox module

I'm using the pyvbox, the complete implementation of the virtualBox main API. In my use case i just want to write a python code in order to start and stop VMs. When i use python GUI every things works fine.But when i try to run the python code from the CMD i got this error:
echec de l'ouverture de session pour la machine 'test'
Microsoft Visual C++ Runtime Library
Runtime Error!
Program: C:\Program Files\Oracle\VirtualBox\VBoxSVC.exe
R6025
-pure virtual function call
PS:
python version 2.7.11
pyvbox 1.0.0
OS windows 8
startvm.py:
import virtualbox
vbox = virtualbox.VirtualBox()
session = virtualbox.Session()
vm = vbox.find_machine('test_vm')
progress = vm.launch_vm_process(session, 'gui', '')
probably you don't need any help on this anymore :D, but I write it here anyway, maybe it can be helpful to someone.
Starting and stopping a VM with the python pyvbox library is very simple:
import virtualbox
from vboxapi import VirtualBoxManager
vbox = virtualbox.VirtualBox()
session = virtualbox.Session()
# This part starts the machine:
# machine_name is a simple string containing the name of your VM. For example "ubuntu"
machine = vbox.find_machine(machine_name)
proc = machine.launch_vm_process(session, "headless")
proc.wait_for_completion(timeout=-1)
# This part stops it:
process, unused_variable = session.machine.take_snapshot("snapshot_name", "snapshot_description", False)
# False means "do not pause the machine while snapshotting"
process.wait_for_completion(timeout=-1)
session.unlock_machine()
I created a simple script to automatically snapshot my virtual machines.
GitHub:
https://github.com/Meru3m/virtualbox-snapshotter

GDB command to know whether the program is running or stopped

I am trying to automate a GDB Debugging session, I want to know whether is there any command or any other way in GDB which will help me know whether a program is running or stopped ?
Use gdb.selected_inferior().threads()[0].is_running() from the GDB Python API:
$ gdb -q /bin/true
(gdb) python from __future__ import print_function
(gdb) python print([ t.is_running() for t in gdb.selected_inferior().threads() ])
[True]
References
GDB Python API: Threads, Inferiors
I added a new command into gdb to know whether the program is running or stopped.
if(is_running (inferior_ptid))
{
fprintf_filtered (gdb_stdout, "running\n");
}
else
{
fprintf_filtered (gdb_stdout, "stopped\n");
}