Skip to content
Snippets Groups Projects
Commit 0f477a6f authored by Tomasz Grabiec's avatar Tomasz Grabiec Committed by Pekka Enberg
Browse files

trace: introduce pcap-dump and tcpdump commands


The 'scripts/trace.py pcap-dump' command will process all packet
capturing samples and output a pcap stream. This is the format used by
tcpdump. It can be used together with tcpdump to display packets in
human-readable form like this:

  scripts/trace.py pcap-dump | tcpdump -r - | less

Because pcap stream is often used together with tcpdump, I also
introduce a shorthand for the above:

  scripts/trace.py tcpdump

Signed-off-by: default avatarTomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
parent f30ba40d
No related branches found
No related tags found
No related merge requests found
......@@ -217,6 +217,41 @@ def extract(args):
def prof_wait(args):
show_profile(args, get_wait_profile)
def pcap_dump(args, target=None):
if not target:
target = sys.stdout
try:
import dpkt
except ImportError:
raise Exception("""Cannot import dpkt. If you don't have it installed you can get it from
https://code.google.com/p/dpkt/downloads""")
pcap_file = dpkt.pcap.Writer(target)
try:
with get_trace_reader(args) as reader:
for sample in reader.get_traces():
ts = sample.time / 1e9
if sample.name == "net_packet_eth":
pcap_file.writepkt(sample.data[1], ts=ts)
elif sample.name == "net_packet_loopback":
pkt = dpkt.ethernet.Ethernet()
pkt.data = sample.data[1]
pcap_file.writepkt(pkt, ts=ts)
finally:
pcap_file.close()
def tcpdump(args):
proc = subprocess.Popen(['tcpdump', '-n', '-r', '-'], stdin=subprocess.PIPE, stdout=sys.stdout,
stderr=subprocess.STDOUT)
try:
pcap_dump(args, target=proc.stdin)
except:
proc.kill()
raise
proc.wait()
def prof_hit(args):
if args.tracepoint:
filter = lambda trace: trace.name == args.tracepoint
......@@ -430,6 +465,14 @@ if __name__ == "__main__":
add_trace_source_options(cmd_extract)
cmd_extract.set_defaults(func=extract)
cmd_pcap_dump = subparsers.add_parser("pcap-dump")
add_trace_source_options(cmd_pcap_dump)
cmd_pcap_dump.set_defaults(func=pcap_dump)
cmd_tcpdump = subparsers.add_parser("tcpdump")
add_trace_source_options(cmd_tcpdump)
cmd_tcpdump.set_defaults(func=tcpdump, paginate=True)
args = parser.parse_args()
if getattr(args, 'paginate', False):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment