• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2#
3# Author: Franklin Meng <fmeng2002@yahoo.com>
4# Parser for USB snoops captured from SniffUSB 2.0.
5#
6
7use strict;
8use warnings;
9use Data::Dumper;
10
11my $urbhash;
12
13foreach my $curfile (@ARGV) {
14	parsefile($curfile);
15	#we can only process 1 file
16	exit;
17}
18
19sub parsefile {
20	my $curfile = shift;
21
22	my $s1 = 0;
23	my $t1 = 0;
24	my $tmp1 = "";
25	my $printurb = 0;
26	my $cururb = 0;
27
28	open(FD, $curfile) || die("Error: $!\n");
29
30	while(<FD>) {
31		chomp;
32		if (/URB\s+(\d+)\s+going\s+down/) {
33			# print previous urb if available
34			if($printurb != 0 && exists($urbhash->{$printurb}->{'SetupPacket'})) {
35				print "$urbhash->{$printurb}->{'SetupPacket'} $urbhash->{$printurb}->{'Direction'} $urbhash->{$printurb}->{'TransferBufferMDL'}\n";
36#				# delete urb information
37#				delete($urbhash->{$printurb});
38			}
39			# delete urb information
40			delete($urbhash->{$printurb});
41			$printurb = 0;  #reset here
42			$tmp1 = "";
43			$s1 = 0;
44			$t1 = 0;
45			# store next urb info here
46			$cururb = $1;
47			$urbhash->{$1} = undef;
48			next;
49		} elsif (/URB\s+(\d+)\s+coming\s+back/) {
50			# print previous urb if available
51			if($printurb != 0 && exists($urbhash->{$printurb}->{'SetupPacket'})) {
52				print "$urbhash->{$printurb}->{'SetupPacket'} $urbhash->{$printurb}->{'Direction'} $urbhash->{$printurb}->{'TransferBufferMDL'}\n";
53#				# delete urb information
54#				delete($urbhash->{$printurb});
55			}
56			# delete urb information
57			delete($urbhash->{$printurb});
58			$printurb = 0;  #reset here
59			$tmp1 = "";
60			$s1 = 0;
61			$t1 = 0;
62			# flag next urb for print out
63			if(exists($urbhash->{$1})) {
64				$printurb = $1;
65			} else {
66				warn "Error: cannot match urb!!\n";
67			}
68			$cururb = $1;
69			next;
70		} elsif (/\-{2}\s+(URB_FUN.+)\:/) {  # store urb function (used for debugging)
71			if(!exists($urbhash->{$cururb}->{'Function'})) {
72				$urbhash->{$cururb}->{'Function'} = $1;
73			}
74			next;
75		} elsif (/USBD_TRANSFER_DIRECTION_IN/) {  #store in direction
76			#check if we already stored a value
77			if(!exists($urbhash->{$cururb}->{'Direction'})) {
78				$urbhash->{$cururb}->{'Direction'} = "<<<";
79			}
80			next;
81		} elsif (/USBD_TRANSFER_DIRECTION_OUT/) { #store out direction
82			#check if we already stored a value
83			if(!exists($urbhash->{$cururb}->{'Direction'})) {
84				$urbhash->{$cururb}->{'Direction'} = ">>>";
85			}
86			next;
87		} elsif (/TransferBufferMDL\s+=\s+/) {  #flag data packet
88			$t1 = 1;
89			next;
90		} elsif (/SetupPacket\s+=/) {  #flag setup packet
91			$s1 = 1;
92			next;
93		} elsif (/(.+\s+\=|ms\])/ && ($s1 || $t1)) { #save data packet and reset
94			if($s1 && ($tmp1 ne "")) {
95				$tmp1 =~ s/^\s+//;
96				$urbhash->{$cururb}->{'SetupPacket'} = $tmp1;
97			} elsif($t1 && ($tmp1 ne "")) {
98				$tmp1 =~ s/^\s+//;
99				$urbhash->{$cururb}->{'TransferBufferMDL'} = $tmp1
100			}
101			$tmp1 = "";
102			$s1 = 0;
103			$t1 = 0;
104			next;
105		} elsif (/^\s+\d+\:(.+)/ && ($s1 || $t1)) { #capture packet
106			$tmp1 = $tmp1 . $1;
107		}
108
109	}
110
111	# print remaining URB
112	if($printurb != 0 && exists($urbhash->{$printurb}->{'SetupPacket'})) {
113		print "$urbhash->{$printurb}->{'SetupPacket'} $urbhash->{$printurb}->{'Direction'} $urbhash->{$printurb}->{'TransferBufferMDL'}\n";
114#		# delete urb information
115#		delete($urbhash->{$printurb});
116	}
117	# delete urb information
118	delete($urbhash->{$printurb});
119
120	# Maybe we should warn for the URB's that did not have matches?
121
122	# print out stuff remaining in the hash for debugging
123	#print Dumper($urbhash);
124}
125
126