Действия
Wiki »
Python-debug¶
Документация¶
https://docs.python.org/2/library/pdb.html
В нужном месте ставим:¶
import pdb; pdb.set_trace()
Основные команды:¶
l - смотреть текущую позицию в коде
n - идти по коду далее
s - идти по коду вглубь
w - смотреть стэк вызовов
u - переключаться по стэку вверх
d - переключаться по стэку вниз
a - смотреть значения параметров текущей функции
p var - смотреть значение переменной.
postgres@k5505-pc-db-t ~ $ /tmp/pg_view.py
> /tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
(Pdb) n
> /tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
(Pdb) s
--Call--
> /tmp/pg_view.py(3183)parse_single_line()
-> def parse_single_line(self, inode):
(Pdb) s
> /tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) bt
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
/tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
/tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
/tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
> /tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) u
> /tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
(Pdb) u
> /tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
(Pdb) u
> /tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
(Pdb) w
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
> /tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
/tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
/tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
/tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) d
> /tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
(Pdb) w
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
/tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
> /tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
/tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
/tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) l
3008 result = None
3009 inodes = fetch_socket_inodes_for_process(pid)
3010 parser = ProcNetParser()
3011 import pdb; pdb.set_trace()
3012
3013 -> result = parser.match_socket_inodes(inodes)
3014 if not result or len(result) == 0:
3015 logger.error('could not detect connection string from /proc/net for postgres process {0}'.format(pid))
3016 return None
3017 return result
3018
(Pdb) w
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
/tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
> /tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
/tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
/tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) a
pid = 1776
(Pdb) d
> /tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
(Pdb) a
self = <__main__.ProcNetParser instance at 0x7f3a801edfc8>
inodes = [26552586, 951, 3269, 3272]
(Pdb) w
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
/tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
/tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
> /tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
/tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) d
> /tmp/pg_view.py(3185)parse_single_line()
-> result = None
(Pdb) a
self = <__main__.ProcNetParser instance at 0x7f3a801edfc8>
inode = 26552586
(Pdb) l
3180 inode = int(fields[inode_idx])
3181 self.sockets[inode] = [socket_type, line]
3182
3183 def parse_single_line(self, inode):
3184 """ apply socket-specific parsing rules """
3185 -> result = None
3186 (socket_type, line) = self.sockets[inode]
3187 if socket_type == 'unix':
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
(Pdb) s
> /tmp/pg_view.py(3186)parse_single_line()
-> (socket_type, line) = self.sockets[inode]
(Pdb) l
3181 self.sockets[inode] = [socket_type, line]
3182
3183 def parse_single_line(self, inode):
3184 """ apply socket-specific parsing rules """
3185 result = None
3186 -> (socket_type, line) = self.sockets[inode]
3187 if socket_type == 'unix':
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
3191 socket_path = fields[-1]
(Pdb) n
> /tmp/pg_view.py(3187)parse_single_line()
-> if socket_type == 'unix':
(Pdb) l
3182
3183 def parse_single_line(self, inode):
3184 """ apply socket-specific parsing rules """
3185 result = None
3186 (socket_type, line) = self.sockets[inode]
3187 -> if socket_type == 'unix':
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
3191 socket_path = fields[-1]
3192 # check that it looks like a PostgreSQL socket
(Pdb) n
> /tmp/pg_view.py(3190)parse_single_line()
-> fields = line.split(None, self.unix_socket_header_len - 1)
(Pdb) l
3185 result = None
3186 (socket_type, line) = self.sockets[inode]
3187 if socket_type == 'unix':
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 -> fields = line.split(None, self.unix_socket_header_len - 1)
3191 socket_path = fields[-1]
3192 # check that it looks like a PostgreSQL socket
3193 match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
3194 if match:
3195 # path - port
(Pdb) n
> /tmp/pg_view.py(3191)parse_single_line()
-> socket_path = fields[-1]
(Pdb) l
3186 (socket_type, line) = self.sockets[inode]
3187 if socket_type == 'unix':
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
3191 -> socket_path = fields[-1]
3192 # check that it looks like a PostgreSQL socket
3193 match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
3194 if match:
3195 # path - port
3196 result = (socket_type,) + match.groups(1)
(Pdb) n
> /tmp/pg_view.py(3193)parse_single_line()
-> match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
(Pdb) l
3188 # we are interested in everything in the last field
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
3191 socket_path = fields[-1]
3192 # check that it looks like a PostgreSQL socket
3193 -> match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
3194 if match:
3195 # path - port
3196 result = (socket_type,) + match.groups(1)
3197 else:
3198 logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
(Pdb) n
> /tmp/pg_view.py(3194)parse_single_line()
-> if match:
(Pdb) l
3189 # note that it may contain spaces or other separator characters
3190 fields = line.split(None, self.unix_socket_header_len - 1)
3191 socket_path = fields[-1]
3192 # check that it looks like a PostgreSQL socket
3193 match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
3194 -> if match:
3195 # path - port
3196 result = (socket_type,) + match.groups(1)
3197 else:
3198 logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
3199 else:
(Pdb) n
> /tmp/pg_view.py(3198)parse_single_line()
-> logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
(Pdb) l
3193 match = re.search(r'(.*?)/\.s\.PGSQL\.(\d+)$', socket_path)
3194 if match:
3195 # path - port
3196 result = (socket_type,) + match.groups(1)
3197 else:
3198 -> logger.warning('unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(socket_path))
3199 else:
3200 address_port = line.split()[1]
3201 (address_hex, port_hex) = address_port.split(':')
3202 port = self._hex_to_int_str(port_hex)
3203 if socket_type == 'tcp6':
(Pdb) n
> /tmp/pg_view.py(3210)parse_single_line()
-> return result
(Pdb) l
3205 elif socket_type == 'tcp':
3206 address = self._hex_to_ip(address_hex)
3207 else:
3208 logger.error('unrecognized socket type: {0}'.format(socket_type))
3209 result = (socket_type, address, port)
3210 -> return result
3211
3212
3213 def main():
3214 user_dbname = options.instance
3215 user_dbver = options.version
(Pdb) p print "%s", result
*** SyntaxError: SyntaxError('invalid syntax', ('<string>', 1, 5, 'print "%s", result'))
(Pdb) p 'print "%s", result'
'print "%s", result'
(Pdb) p result
None
(Pdb) n
--Return--
> /tmp/pg_view.py(3210)parse_single_line()->None
-> return result
(Pdb) l
3205 elif socket_type == 'tcp':
3206 address = self._hex_to_ip(address_hex)
3207 else:
3208 logger.error('unrecognized socket type: {0}'.format(socket_type))
3209 result = (socket_type, address, port)
3210 -> return result
3211
3212
3213 def main():
3214 user_dbname = options.instance
3215 user_dbver = options.version
(Pdb) w
/tmp/pg_view.py(3483)<module>()
-> main()
/tmp/pg_view.py(3241)main()
-> conndata = detect_db_connection_arguments(result_work_dir, ppid, dbver)
/tmp/pg_view.py(3027)detect_db_connection_arguments()
-> conn_args = detect_with_proc_net(pid)
/tmp/pg_view.py(3013)detect_with_proc_net()
-> result = parser.match_socket_inodes(inodes)
/tmp/pg_view.py(3149)match_socket_inodes()
-> addr_tuple = self.parse_single_line(inode)
> /tmp/pg_view.py(3210)parse_single_line()->None
-> return result
(Pdb) n
> /tmp/pg_view.py(3150)match_socket_inodes()
-> socket_type = addr_tuple[0]
(Pdb) l
3145 for inode in inodes:
3146 if inode in self.sockets:
3147 import pdb; pdb.set_trace()
3148
3149 addr_tuple = self.parse_single_line(inode)
3150 -> socket_type = addr_tuple[0]
3151 if socket_type in result:
3152 result[socket_type].append(addr_tuple[1:])
3153 else:
3154 result[socket_type] = [addr_tuple[1:]]
3155 return result
(Pdb) q
PostgreSQL exception
No suitable PostgreSQL instances detected, exiting...
hint: use -v for details, or specify connection parameters manually in the configuration file (-c)
Обновлено Андрей Волков почти 11 года назад · 1 изменени(я, ий)