import os
import re
def get_map_range(map_range):
map_ranges = map_range.split("-")
range_from = int(map_ranges[0], 16)
range_to = int(map_ranges[1], 16)
return (range_from, range_to)
def show_threads_stack_info(verbose):
(inferior,) = gdb.inferiors()
proc_map_file_name = "/proc/{}/maps".format(inferior.pid)
proc_map_file = open(proc_map_file_name, 'r')
map_ranges = []
while True:
line = proc_map_file.readline()
if not line:
break
map_range = line.split()[0]
(range_from, range_to) = get_map_range(map_range)
map_ranges.append((map_range, range_from, range_to))
totalSizeKB = 0
totalPssKB = 0
for thread in inferior.threads():
thread.switch()
frame = gdb.selected_frame()
rsp_val = int(frame.read_register("rsp"))
if verbose:
print("------------------")
gdb.execute("bt")
for r in map_ranges:
(map_range, range_from, range_to) = r
if rsp_val>=range_from and rsp_val<range_to:
cmd = "grep -A5 {} /proc/{}/smaps".format(map_range, inferior.pid)
res = os.popen(cmd).read().split("\n")
SizeKB = int(re.split(r"\s+", res[1])[1])
PssKB = int(re.split(r"\s+", res[5])[1])
if verbose:
print(res[0])
print(res[1])
print(res[2])
print(res[3])
print(res[4])
print(res[5])
totalSizeKB += SizeKB
totalPssKB += PssKB
print("totalSize: {} KB, totalPss: {} KB".format(totalSizeKB, totalPssKB) )
class ShowThreadsStackInfo (gdb.Command):
"""Show Threads stack info
show-threads-stack-info [1]
"""
def __init__ (self):
super (ShowThreadsStackInfo, self).__init__ ("show-threads-stack-info", gdb.COMMAND_USER)
def invoke (self, arg, from_tty):
show_threads_stack_info(arg=="1")
ShowThreadsStackInfo ()
在gdb里執(zhí)行
show-threads-stack-info 1