mirror of https://github.com/pwndbg/pwndbg.git
Add support for performance profiling (#1413)
Co-authored-by: Gulshan Singh <gsgx@google.com>pull/1423/head
parent
94d1ebb9bd
commit
02c97693f7
@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import pstats
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = ArgumentParser(description="Print the profiling stats from a pstats file")
|
||||||
|
parser.add_argument("-n", "--num", type=int, help="number of stats to print")
|
||||||
|
parser.add_argument("-f", "--filter", help="regex to match against each entry")
|
||||||
|
parser.add_argument(
|
||||||
|
"--no-strip", action="store_true", help="print the entire file path for each entry"
|
||||||
|
)
|
||||||
|
parser.add_argument("file", help="pstats file to parse")
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--sort",
|
||||||
|
choices=["calls", "ncalls", "cumulative", "time"],
|
||||||
|
default="cumulative",
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args()
|
||||||
|
s = pstats.Stats(args.file)
|
||||||
|
if not args.no_strip:
|
||||||
|
s.strip_dirs()
|
||||||
|
s.sort_stats(args.sort).print_stats(args.filter, args.num)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
import cProfile
|
||||||
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
profiler = None
|
||||||
|
|
||||||
|
|
||||||
|
def init(p: cProfile.Profile, _start_time: Optional[float]):
|
||||||
|
global profiler
|
||||||
|
profiler = Profiler(p)
|
||||||
|
profiler._start_time = _start_time
|
||||||
|
|
||||||
|
|
||||||
|
class Profiler:
|
||||||
|
def __init__(self, p: cProfile.Profile):
|
||||||
|
self._profiler = p
|
||||||
|
self._start_time = None
|
||||||
|
|
||||||
|
def print_time_elapsed(self):
|
||||||
|
assert self._start_time is not None
|
||||||
|
return print("Time Elapsed:", time.time() - self._start_time)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._start_time = time.time()
|
||||||
|
self._profiler.enable()
|
||||||
|
|
||||||
|
def stop(self, filename=None):
|
||||||
|
if not filename:
|
||||||
|
filename = f"pwndbg-{int(time.time())}.pstats"
|
||||||
|
|
||||||
|
self.print_time_elapsed()
|
||||||
|
self._profiler.disable()
|
||||||
|
self._start_time = None
|
||||||
|
|
||||||
|
self._profiler.dump_stats(filename)
|
||||||
Loading…
Reference in new issue