Проект

Общее

Профиль

Python-debug » История » Версия 1

Андрей Волков, 2015-02-11 13:00

1 1 Андрей Волков
h1. Python-debug
2
3
h2. Документация
4
5
https://docs.python.org/2/library/pdb.html
6
7
8
h2. В нужном месте ставим:
9
10
<pre>
11
import pdb; pdb.set_trace()
12
</pre>
13
14
h2. Основные команды:
15
16
l - смотреть текущую позицию в коде
17
n - идти по коду далее
18
s - идти по коду вглубь
19
w - смотреть стэк вызовов
20
u - переключаться по стэку вверх
21
d - переключаться по стэку вниз
22
a - смотреть значения параметров текущей функции
23
p var - смотреть значение переменной.
24
25
{{collapse(Пример...)
26
27
<pre>
28
postgres@k5505-pc-db-t ~ $ /tmp/pg_view.py  
29
> /tmp/pg_view.py(3013)detect_with_proc_net()
30
-> result = parser.match_socket_inodes(inodes)
31
(Pdb) n
32
> /tmp/pg_view.py(3149)match_socket_inodes()
33
-> addr_tuple = self.parse_single_line(inode)
34
(Pdb) s
35
--Call--
36
> /tmp/pg_view.py(3183)parse_single_line()
37
-> def parse_single_line(self, inode):
38
(Pdb) s
39
> /tmp/pg_view.py(3185)parse_single_line()
40
-> result = None
41
(Pdb) bt
42
  /tmp/pg_view.py(3483)<module>()
43
-> main()
44
  /tmp/pg_view.py(3241)main()
45
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
46
  /tmp/pg_view.py(3027)detect_db_connection_arguments()
47
-> conn_args = detect_with_proc_net(pid)
48
  /tmp/pg_view.py(3013)detect_with_proc_net()
49
-> result = parser.match_socket_inodes(inodes)
50
  /tmp/pg_view.py(3149)match_socket_inodes()
51
-> addr_tuple = self.parse_single_line(inode)
52
> /tmp/pg_view.py(3185)parse_single_line()
53
-> result = None
54
(Pdb) u
55
> /tmp/pg_view.py(3149)match_socket_inodes()
56
-> addr_tuple = self.parse_single_line(inode)
57
(Pdb) u
58
> /tmp/pg_view.py(3013)detect_with_proc_net()
59
-> result = parser.match_socket_inodes(inodes)
60
(Pdb) u
61
> /tmp/pg_view.py(3027)detect_db_connection_arguments()
62
-> conn_args = detect_with_proc_net(pid)
63
(Pdb) w
64
  /tmp/pg_view.py(3483)<module>()
65
-> main()
66
  /tmp/pg_view.py(3241)main()
67
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
68
> /tmp/pg_view.py(3027)detect_db_connection_arguments()
69
-> conn_args = detect_with_proc_net(pid)
70
  /tmp/pg_view.py(3013)detect_with_proc_net()
71
-> result = parser.match_socket_inodes(inodes)
72
  /tmp/pg_view.py(3149)match_socket_inodes()
73
-> addr_tuple = self.parse_single_line(inode)
74
  /tmp/pg_view.py(3185)parse_single_line()
75
-> result = None
76
(Pdb) d
77
> /tmp/pg_view.py(3013)detect_with_proc_net()
78
-> result = parser.match_socket_inodes(inodes)
79
(Pdb) w
80
  /tmp/pg_view.py(3483)<module>()
81
-> main()
82
  /tmp/pg_view.py(3241)main()
83
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
84
  /tmp/pg_view.py(3027)detect_db_connection_arguments()
85
-> conn_args = detect_with_proc_net(pid)
86
> /tmp/pg_view.py(3013)detect_with_proc_net()
87
-> result = parser.match_socket_inodes(inodes)
88
  /tmp/pg_view.py(3149)match_socket_inodes()
89
-> addr_tuple = self.parse_single_line(inode)
90
  /tmp/pg_view.py(3185)parse_single_line()
91
-> result = None
92
(Pdb) l
93
3008 	    result = None
94
3009 	    inodes = fetch_socket_inodes_for_process(pid)
95
3010 	    parser = ProcNetParser()
96
3011 	    import pdb; pdb.set_trace()
97
3012 	
98
3013 ->	    result = parser.match_socket_inodes(inodes)
99
3014 	    if not result or len(result) == 0:
100
3015 	        logger.error('could not detect connection string from /proc/net for postgres process {0}'.format(pid))
101
3016 	        return None
102
3017 	    return result
103
3018 	
104
(Pdb) w
105
  /tmp/pg_view.py(3483)<module>()
106
-> main()
107
  /tmp/pg_view.py(3241)main()
108
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
109
  /tmp/pg_view.py(3027)detect_db_connection_arguments()
110
-> conn_args = detect_with_proc_net(pid)
111
> /tmp/pg_view.py(3013)detect_with_proc_net()
112
-> result = parser.match_socket_inodes(inodes)
113
  /tmp/pg_view.py(3149)match_socket_inodes()
114
-> addr_tuple = self.parse_single_line(inode)
115
  /tmp/pg_view.py(3185)parse_single_line()
116
-> result = None
117
(Pdb) a
118
pid = 1776
119
(Pdb) d
120
> /tmp/pg_view.py(3149)match_socket_inodes()
121
-> addr_tuple = self.parse_single_line(inode)
122
(Pdb) a
123
self = <__main__.ProcNetParser instance at 0x7f3a801edfc8>
124
inodes = [26552586, 951, 3269, 3272]
125
(Pdb) w
126
  /tmp/pg_view.py(3483)<module>()
127
-> main()
128
  /tmp/pg_view.py(3241)main()
129
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
130
  /tmp/pg_view.py(3027)detect_db_connection_arguments()
131
-> conn_args = detect_with_proc_net(pid)
132
  /tmp/pg_view.py(3013)detect_with_proc_net()
133
-> result = parser.match_socket_inodes(inodes)
134
> /tmp/pg_view.py(3149)match_socket_inodes()
135
-> addr_tuple = self.parse_single_line(inode)
136
  /tmp/pg_view.py(3185)parse_single_line()
137
-> result = None
138
(Pdb) d
139
> /tmp/pg_view.py(3185)parse_single_line()
140
-> result = None
141
(Pdb) a
142
self = <__main__.ProcNetParser instance at 0x7f3a801edfc8>
143
inode = 26552586
144
(Pdb) l
145
3180 	                inode = int(fields[inode_idx])
146
3181 	                self.sockets[inode] = [socket_type, line]
147
3182 	
148
3183 	    def parse_single_line(self, inode):
149
3184 	        """ apply socket-specific parsing rules """
150
3185 ->	        result = None
151
3186 	        (socket_type, line) = self.sockets[inode]
152
3187 	        if socket_type == 'unix':
153
3188 	            # we are interested in everything in the last field
154
3189 	            # note that it may contain spaces or other separator characters
155
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
156
(Pdb) s
157
> /tmp/pg_view.py(3186)parse_single_line()
158
-> (socket_type, line) = self.sockets[inode]
159
(Pdb) l
160
3181 	                self.sockets[inode] = [socket_type, line]
161
3182 	
162
3183 	    def parse_single_line(self, inode):
163
3184 	        """ apply socket-specific parsing rules """
164
3185 	        result = None
165
3186 ->	        (socket_type, line) = self.sockets[inode]
166
3187 	        if socket_type == 'unix':
167
3188 	            # we are interested in everything in the last field
168
3189 	            # note that it may contain spaces or other separator characters
169
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
170
3191 	            socket_path = fields[-1]
171
(Pdb) n
172
> /tmp/pg_view.py(3187)parse_single_line()
173
-> if socket_type == 'unix':
174
(Pdb) l
175
3182 	
176
3183 	    def parse_single_line(self, inode):
177
3184 	        """ apply socket-specific parsing rules """
178
3185 	        result = None
179
3186 	        (socket_type, line) = self.sockets[inode]
180
3187 ->	        if socket_type == 'unix':
181
3188 	            # we are interested in everything in the last field
182
3189 	            # note that it may contain spaces or other separator characters
183
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
184
3191 	            socket_path = fields[-1]
185
3192 	            # check that it looks like a PostgreSQL socket
186
(Pdb) n
187
> /tmp/pg_view.py(3190)parse_single_line()
188
-> fields = line.split(None, self.unix_socket_header_len - 1)
189
(Pdb) l
190
3185 	        result = None
191
3186 	        (socket_type, line) = self.sockets[inode]
192
3187 	        if socket_type == 'unix':
193
3188 	            # we are interested in everything in the last field
194
3189 	            # note that it may contain spaces or other separator characters
195
3190 ->	            fields = line.split(None, self.unix_socket_header_len - 1)
196
3191 	            socket_path = fields[-1]
197
3192 	            # check that it looks like a PostgreSQL socket
198
3193 	            match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
199
3194 	            if match:
200
3195 	                # path - port
201
(Pdb) n
202
> /tmp/pg_view.py(3191)parse_single_line()
203
-> socket_path = fields[-1]
204
(Pdb) l
205
3186 	        (socket_type, line) = self.sockets[inode]
206
3187 	        if socket_type == 'unix':
207
3188 	            # we are interested in everything in the last field
208
3189 	            # note that it may contain spaces or other separator characters
209
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
210
3191 ->	            socket_path = fields[-1]
211
3192 	            # check that it looks like a PostgreSQL socket
212
3193 	            match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
213
3194 	            if match:
214
3195 	                # path - port
215
3196 	                result = (socket_type,) + match.groups(1)
216
(Pdb) n
217
> /tmp/pg_view.py(3193)parse_single_line()
218
-> match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
219
(Pdb) l
220
3188 	            # we are interested in everything in the last field
221
3189 	            # note that it may contain spaces or other separator characters
222
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
223
3191 	            socket_path = fields[-1]
224
3192 	            # check that it looks like a PostgreSQL socket
225
3193 ->	            match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
226
3194 	            if match:
227
3195 	                # path - port
228
3196 	                result = (socket_type,) + match.groups(1)
229
3197 	            else:
230
3198 	                logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
231
(Pdb) n
232
> /tmp/pg_view.py(3194)parse_single_line()
233
-> if match:
234
(Pdb) l
235
3189 	            # note that it may contain spaces or other separator characters
236
3190 	            fields = line.split(None, self.unix_socket_header_len - 1)
237
3191 	            socket_path = fields[-1]
238
3192 	            # check that it looks like a PostgreSQL socket
239
3193 	            match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
240
3194 ->	            if match:
241
3195 	                # path - port
242
3196 	                result = (socket_type,) + match.groups(1)
243
3197 	            else:
244
3198 	                logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
245
3199 	        else:
246
(Pdb) n
247
> /tmp/pg_view.py(3198)parse_single_line()
248
-> logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
249
(Pdb) l
250
3193 	            match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
251
3194 	            if match:
252
3195 	                # path - port
253
3196 	                result = (socket_type,) + match.groups(1)
254
3197 	            else:
255
3198 ->	                logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
256
3199 	        else:
257
3200 	            address_port = line.split()[1]
258
3201 	            (address_hex, port_hex) = address_port.split(':')
259
3202 	            port = self._hex_to_int_str(port_hex)
260
3203 	            if socket_type == 'tcp6':
261
(Pdb) n
262
> /tmp/pg_view.py(3210)parse_single_line()
263
-> return result
264
(Pdb) l
265
3205 	            elif socket_type == 'tcp':
266
3206 	                address = self._hex_to_ip(address_hex)
267
3207 	            else:
268
3208 	                logger.error('unrecognized socket type: {0}'.format(socket_type))
269
3209 	            result = (socket_type, address, port)
270
3210 ->	        return result
271
3211 	
272
3212 	
273
3213 	def main():
274
3214 	    user_dbname = options.instance
275
3215 	    user_dbver = options.version
276
(Pdb) p print "%s", result
277
*** SyntaxError: SyntaxError('invalid syntax', ('<string>', 1, 5, 'print "%s", result'))
278
(Pdb) p 'print "%s", result'
279
'print "%s", result'
280
(Pdb) p result
281
None
282
(Pdb) n
283
--Return--
284
> /tmp/pg_view.py(3210)parse_single_line()->None
285
-> return result
286
(Pdb) l
287
3205 	            elif socket_type == 'tcp':
288
3206 	                address = self._hex_to_ip(address_hex)
289
3207 	            else:
290
3208 	                logger.error('unrecognized socket type: {0}'.format(socket_type))
291
3209 	            result = (socket_type, address, port)
292
3210 ->	        return result
293
3211 	
294
3212 	
295
3213 	def main():
296
3214 	    user_dbname = options.instance
297
3215 	    user_dbver = options.version
298
(Pdb) w
299
  /tmp/pg_view.py(3483)<module>()
300
-> main()
301
  /tmp/pg_view.py(3241)main()
302
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
303
  /tmp/pg_view.py(3027)detect_db_connection_arguments()
304
-> conn_args = detect_with_proc_net(pid)
305
  /tmp/pg_view.py(3013)detect_with_proc_net()
306
-> result = parser.match_socket_inodes(inodes)
307
  /tmp/pg_view.py(3149)match_socket_inodes()
308
-> addr_tuple = self.parse_single_line(inode)
309
> /tmp/pg_view.py(3210)parse_single_line()->None
310
-> return result
311
(Pdb) n
312
> /tmp/pg_view.py(3150)match_socket_inodes()
313
-> socket_type = addr_tuple[0]
314
(Pdb) l
315
3145 	        for inode in inodes:
316
3146 	            if inode in self.sockets:
317
3147 	                import pdb; pdb.set_trace()
318
3148 	
319
3149 	                addr_tuple = self.parse_single_line(inode)
320
3150 ->	                socket_type = addr_tuple[0]
321
3151 	                if socket_type in result:
322
3152 	                    result[socket_type].append(addr_tuple[1:])
323
3153 	                else:
324
3154 	                    result[socket_type] = [addr_tuple[1:]]
325
3155 	        return result
326
(Pdb) q
327
PostgreSQL exception 
328
No suitable PostgreSQL instances detected, exiting...
329
hint: use -v for details, or specify connection parameters manually in the configuration file (-c)
330
</pre>
331
332
}}