xiaozhuaia 发表于 2015-4-12 08:11:08

远程调试openstack

  之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰:
  http://blog.iyunv.com/tantexian/article/details/38295599但是其中有一个问题就是需要在源程序代码中添加代码以供调试用,这样调试完了还要去删除这些代码,十分不方便。所以我就想在此基础上做些改进,具体情况如下:
  
  Komodo下载地址:http://pan.baidu.com/s/13i5Zk
  PythonRemoteDebuggingClient下载地址:http://code.activestate.com/komodo/remotedebugging/
  一、本地安装配置Komodo
  1、Edit->Preferences:配置python解释器。
  2、Debug-> Listen for debuging connections:确保勾选上,这样才能远程调试。
  3、Debug->Listener Status:查看配置的远程连接端口。(Edit-> Preferences->Debugger->Connection:可以自定义远程连接端口)
  
  二、远程服务器配置
  解压PythonRemoteDebuggingClient并进入该目录!



cd pythonlib/dbgp
   1.



cp client.py client.py.bak
vim client.py
  在头部添加:



import stat
  在brk函数处进行修改:



def brk(file=None,host = '10.10.24.96', port = 9000, idekey = '',
preloadScript = None, logLevel = logging.WARN):
filepath=os.path.abspath(file)
tmp_file='/tmp/brk_file'
if not os.path.exists(tmp_file):
with open(tmp_file,'a') as f:
  f.write(filepath)
  f.write('\n')
os.chmod(tmp_file,os.stat(tmp_file).st_mode|stat.S_IWGRP|stat.S_IWOTH)
else:
with open(tmp_file,'a') as f:
  f.write(filepath)
  f.write('\n')
  说明:这里设置/tmp/brk_file为临时记录文件,记录那些添加了断点设置代码的文件路径信息。10.10.24.96为Komodo所在的机器IP地址,非调试的代码所在地址。9000为调试设置的端口。
   2.



cp __init__.py __init__.py.bak
vim __init__.py
  在头部添加:



from dbgp.client import brk
  3.



cp -r pythonlib/dbgp /usr/lib/python2.6/site-packages/
cp pydbgp /usr/bin/
chmod a+x /usr/bin/pydbgp
  
  三、设置删除断点程序
  1.



vim brk-clean
  添加如下内容:



#!/usr/bin/python
import subprocess
import os
import sys
def delete_brk(file):
cmd="sed -i "+r"'/import dbgp/d' "+file+";"
cmd=cmd+"sed -i "+r"'/dbgp.brk(__file__)/d' "+file
proc=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,err=proc.communicate()
if proc.returncode:
raise RuntimeError('Failed:\n%s' % err)

def main():
result=True
#if tmp file not exist or is not a file
if not os.path.exists('/tmp/brk_file') or not os.path.isfile('/tmp/brk_file'):
print "/tmp/brk_file doesn't exist!\nCheck if it has been deleted Or you didn't set any breakpoint !"
result=False
return result
#find files with brk
file_list=[]
with open('/tmp/brk_file','r') as f:
file_list=f.readlines()
try:
os.remove('/tmp/brk_file')
except:
print r"Error:Can't delete /tmp/brk_file!"
result=False

for file in file_list:
file=file.rstrip('\n')
if os.path.exists(file) and os.path.isfile(file):
delete_brk(file)
print 'Deleted brk in file: '+file+r"!"
else:
print 'Error:'+file+r"doesn't exist or isn't a file"
result=False
return result

if __name__=='__main__':
#delete the brk in a file
if len(sys.argv)>1:
file=sys.argv
if not os.path.exists(file) or not os.path.isfile(file):
print file+r" doesn't exist!"
sys.exit()
delete_brk(file)
print "Breakpoints in "+file+" have been cleaned up !"
sys.exit()
result=main()
if result:
print 'Brk-Clean Completed !'
   2.



mv brk-clean /usr/bin
chmod a+x /usr/bin/brk-clean
  
  四、在需要设置断点的地方插入断点设置代码



import dbgp
dbgp.brk(__file__)
  
  五、运行需要调试的程序或重启相关服务
  这里提供一个启动openstack服务的脚本,非本人所写 ,不过用起来也不错,参考链接:https://github.com/liuan/openstack-x
  
  六、调试完后记得删除断点设置代码



brk-clean
  
  七、如果有必要,须重启相关服务
  
页: [1]
查看完整版本: 远程调试openstack