pygdb now saves absolute breakpoint paths
[pygdb.git] / pygdb.vim
1 "shuber, 2008-06-08
2
3 if ! exists("g:pygdb")
4
5 let g:pygdb = 1
6 let s:ScriptLocation = expand("<sfile>")
7
8
9 python << >>
10
11 import gtk
12 import os
13 import string
14 import sys
15 import threading
16 import vim
17
18 import Configuration
19 import DbgTerminal
20
21
22
23 #Breakpoint positions: List of dictionaries of form
24 #{"signnum" : , "file" : , "lineno":, "cond" : }
25 gdbBps = []
26 signnum = 0
27 clientcmd = ""
28 execsign = None
29
30 def gdbLaunch():
31 global gdbterm, mainctrlwnd, statuswnd, gdbBps, clientcmd, gdbthread
32
33 clientcmd = vim.eval("input('Client commando: ', '%s')" % clientcmd).strip()
34
35 if clientcmd.strip()=="":
36 print "No command given!"
37 return
38
39 #Add the breakpoints to the configuration
40 conf = Configuration.Configuration()
41 conf.load(".pygdb.conf")
42 for bp in gdbBps:
43 conf.addBreak(bp["file"], bp["lineno"], bp["cond"])
44 conf.store(".pygdb.conf")
45
46 vim.command("!python %s/pygdb.py %s &\n" % (pygdbdir, clientcmd))
47
48
49 def gdbToggleBreakpoint(lineno=None, file=None):
50 global gdbBps
51
52 #Set lineno and file if not already set
53 if lineno==None:
54 lineno = vim.current.window.cursor[0]
55 if file==None:
56 file = getCurrentFile()
57
58 #Determine index of breakpoint
59 bpidx = gdbGetBreakpoint( file, lineno )
60
61 if bpidx != None:
62 removeBreakpoint(bpidx)
63 else:
64 addBreakpoint(file, lineno)
65
66
67 def setExecutionLine(file, lineno):
68 global execsign
69
70
71 #Open that file!
72 if file != getCurrentFile():
73 try:
74 os.stat(file)
75 vim.command(":e %s" % file)
76 except OSError:
77 print "Warning: file '%s' does not exist! (Wrong client command?)" % file
78 return
79
80 #Jump to line
81 vim.command(":%d" % lineno)
82
83 #Remove old execsign
84 if execsign != None:
85 delExecutionLine()
86
87 #Set the sign
88 execsign = gdbNewSignnum()
89 vim.command("sign place %d line=%s name=ExecutionLine file=%s"%(execsign, lineno, file))
90
91
92 def delExecutionLine():
93 global execsign
94
95 #Remove old execsign
96 if execsign != None:
97 vim.command("sign unplace %d" % execsign)
98 execsign = None
99
100
101 def addBreakpoint(file, lineno, cond=None):
102 global gdbBps, cmdset
103
104 #If file is not open, open it
105 if not file in [b.name for b in vim.buffers]:
106 try:
107 os.stat(file)
108 vim.command(":e %s" % file)
109 except OSError:
110 print "Warning: file '%s' does not exist! (Wrong client command?)" % file
111 cmdset = False
112 return
113
114
115 #Determine a sign number
116 signnum = gdbNewSignnum()
117
118 #Create breakpoint and add sign
119 b = {"signnum" : signnum, "lineno" : lineno, "file" : file, "cond" : cond}
120 gdbBps += [b]
121
122 if cond == None:
123 vim.command("sign place %(signnum)d line=%(lineno)d name=BreakPoint file=%(file)s" % b)
124 else:
125 vim.command("sign place %(signnum)d line=%(lineno)d name=CondBreakPoint file=%(file)s" % b)
126
127
128
129 def removeBreakpoint(idx):
130 global gdbBps
131
132 vim.command("sign unplace %(signnum)d" % gdbBps[idx])
133 del gdbBps[idx]
134
135
136 def gdbBreakpointCond(lineno=None, file=None, cond=None):
137 global gdbBps
138
139 #Set lineno and file if not already set
140 if lineno==None:
141 lineno = vim.current.window.cursor[0]
142 if file==None:
143 file = getCurrentFile()
144
145 #Determine index of breakpoint
146 bpidx = gdbGetBreakpoint( file, lineno )
147
148 #Alter condition
149 if bpidx != None:
150 gdbBps[bpidx]["cond"] = vim.eval("input('Breakpoint condition: ', '%s')" % gdbBps[bpidx]["cond"])
151 #Set new breakpoint
152 else:
153 #Get condition
154 cond = vim.eval("input('Breakpoint condition: ', '')")
155 #Add the breakpoint
156 addBreakpoint(file, lineno, cond)
157
158
159 def getCurrentFile():
160 return vim.current.window.buffer.name
161
162
163
164 def gdbNewSignnum():
165 global signnum
166 signnum += 1
167 return signnum
168
169
170 def gdbGetBreakpoint(file, lineno):
171 for i in range(len(gdbBps)):
172 if [gdbBps[i]["file"], gdbBps[i]["lineno"]] == [file,lineno]:
173 return i
174 return None
175
176 def gdbShowBreakpoints():
177 global gdbBps
178
179 if len(gdbBps) == 0:
180 print "No breakpoints set."
181 else:
182 print "%d breakpoints set:" % len(gdbBps)
183
184 for bp in gdbBps:
185 if bp["cond"] != None:
186 print "%(file)s:%(lineno)d if %(cond)s" % bp
187 else:
188 print "%(file)s:%(lineno)d" % bp
189
190
191
192 def toAbsPath(path):
193 global clientcmd, cmdset
194
195 #Not a absolute path --> make one
196 if path[0] != os.sep:
197
198 #We need the client command to expand the paths...
199 while clientcmd == "" or not cmdset:
200 clientcmd = vim.eval("input('Client commando: ', '%s')" % clientcmd).strip()
201 cmdset = True
202
203 #Get the dirs where executeable is in
204 relcmd = string.split(clientcmd)[0]
205 abscmd = DbgTerminal.relToAbsPath(getCurrentFile(), relcmd)
206 path = DbgTerminal.relToAbsPath(abscmd, path)
207
208 assert(path[0] == "/")
209
210 return path
211
212
213 def gdbLoadConfig():
214 global clientcmd, gdbBps, cmdset
215
216
217
218
219 #Load configuration
220 conf = Configuration.Configuration()
221 conf.load(".pygdb.conf")
222
223 #Remove all breakpoints
224 while len(gdbBps)>0:
225 removeBreakpoint(0)
226
227 #Add breakpoints from configuration
228 for bp in conf.breakpoints:
229 bp["file"] = toAbsPath( bp["file"] )
230 addBreakpoint(bp["file"], bp["lineno"], bp["cond"])
231
232 #Set current execution line
233 if conf.isCurrposSet():
234 file = toAbsPath(conf.currfile)
235 setExecutionLine(file, conf.currlineno)
236 else:
237 delExecutionLine()
238 >>
239
240 highlight ExecutionLine term=bold ctermbg=DarkGreen ctermfg=Black guibg=LightGreen guifg=Black
241 highlight BreakPoint term=inverse ctermbg=DarkRed ctermfg=Black guibg=LightRed guifg=Black
242
243 sign define ExecutionLine text==> texthl=ExecutionLine linehl=ExecutionLine
244 sign define BreakPoint text=! texthl=BreakPoint linehl=BreakPoint
245 sign define CondBreakPoint text=? texthl=BreakPoint linehl=BreakPoint
246
247
248 command! GDBLaunch :python gdbLaunch()
249 command! GDBToggleBreakpoint :python gdbToggleBreakpoint()
250 command! GDBBreakpointCond :python gdbBreakpointCond()
251 command! GDBShowBreakpoints :python gdbShowBreakpoints()
252 command! GDBLoadConfig :python gdbLoadConfig()
253
254
255
256 function! GDBMapDefaults()
257 nmap <silent> <F5> :GDBLaunch<CR>
258 nmap <silent> <F8> :GDBToggleBreakpoint<CR>
259 nmap <silent> <S-F8> :GDBBreakpointCond<CR>
260 nmap <silent> <F9> :GDBShowBreakpoints<CR>
261 nmap <silent> <S-F9> :GDBLoadConfig<CR>
262 endfunction
263
264
265
266 endif
267
268