• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?
2
3/**********************************************************************
4 *  Copyright (C) 2010
5 *
6 *  Douglas Schilling Landgraf <dougsland@redhat.com>
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation, version 2 of the License.
11 *
12 *  This program is distributed in the hope that it will be useful,
13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *  GNU General Public License for more details.
16 *
17 *  Version: 0.0.1
18 *
19 *  Description:
20 *	This is a quick hack to parse logs from the usbsnoop (usb sniffer)
21 *
22 *  Settings:
23 *	If needed to process a huge log maybe is needed to increase
24 *      the php settings.
25 *
26 *          $ vi /etc/php.ini
27 *            memory_limit = xxM
28 *
29 *  Usage:
30 *         $ php ./usb_snoop ./Usbsnoop > output.txt
31 *
32 *  Example:
33 *
34 *	   009279: 002309 ms 126080 ms c0 0e a0 00 00 00 01 00 <<< 00
35 *         009280: 000007 ms 126087 ms c0 10 a0 00 00 00 01 00 <<< 00
36 *         009281: 000005 ms 126092 ms 40 0e a0 00 00 00 01 00 >>> 99
37 *         009282: 000107 ms 126199 ms c0 0e a0 00 01 00 01 00 <<< 99
38 *         009283: 000015 ms 126214 ms c0 0e a0 00 10 00 01 00 <<< 99
39 *         009284: 000004 ms 126218 ms 40 10 a0 00 00 00 01 00 >>> 00
40 *         009285: 000004 ms 126222 ms 40 0e a0 00 00 00 01 00 >>> 00
41 *
42 ***********************************************************************/
43
44function removeNL($var) {
45
46	$newstr = str_replace("\n", "", $var);
47	return $newstr;
48}
49
50function removeZeros($var) {
51
52	/* Removing 00000000 */
53	$remZeros   = strstr($var, ": ");
54
55	/* Removing : */
56	$remNewLine = str_replace(": ", "", $remZeros);
57
58	/* Removing \n */
59	$newstr = removeNL($remNewLine);
60
61	return $newstr;
62}
63
64/* Main */
65
66	$i = 0;
67	$j = 0;
68	$oldTime   = 0;
69	$direction = "";
70	$ctrlCmds  = "";
71
72
73	if ($argc < 2) {
74		printf("Usage: $argv[0] usbsnoop-log.txt\n");
75		exit;
76	}
77
78	$file = fopen($argv[1], "r") or exit("Unable to open file!\n");
79
80	/* Copying to a temp. buffer */
81	while(!feof($file))
82	{
83		$arrayFile[$i] = fgets($file);
84		$i++;
85	}
86
87	while($j < $i) {
88
89		/* Next position */
90		$j = $j + 1;
91
92		if(!isset($arrayFile[$j])) {
93			break;
94		}
95
96		$str = removeNL($arrayFile[$j]);
97
98		$pieces = explode(" ", $str);
99		/* Defining URB */
100		if (((strstr($str, "<<<")) || (strstr($str, ">>>"))) ) {
101			$URB = $pieces[6] . ":";
102
103		}
104
105		if( $pieces[0] != "--") {
106			$t = $pieces[0];
107			$timeTotalOp = str_replace("[", "", $t);
108			$timeTotalOp = $timeTotalOp . " ms";
109		}
110
111		/* Updating current time*/
112		$currentTime =  $timeTotalOp;
113
114		$str = removeNL($arrayFile[$j]);
115
116		/* Searching for type to analyze*/
117
118		if (strstr($str, "-- URB_FUNCTION_CONTROL_TRANSFER:")) {
119
120			while (1) {
121
122				/* Next line */
123				$j = $j + 1;
124
125				if(!isset($arrayFile[$j])) {
126					break;
127				}
128
129				/* Setting Direction */
130				if (strstr($arrayFile[$j], "TransferFlags")) {
131					if(strstr($arrayFile[$j], "USBD_TRANSFER_DIRECTION_OUT")) {
132						$direction = ">>>";
133					}
134					else {
135						$direction = "<<<";
136					}
137				}
138
139				if (strstr($arrayFile[$j], "TransferBufferMDL")) {
140					$getValues = 1;
141
142					while(1) {
143						/* Next line */
144						$j = $j + 1;
145						if (strstr($arrayFile[$j], "000000")) {
146							$aa = "$arrayFile[$j]\n";
147							if($getValues == 1) {
148								$cmdV = removeZeros($aa);
149							}
150							else {
151								$cmdV .= " " . removeZeros($aa);
152							}
153						}
154						else {
155							break;
156						}
157						$getValues++;
158					}
159					$j = $j - $getValues;
160				}
161				if (strstr($arrayFile[$j], "SetupPacket")) {
162
163					/* To catch the command we increase a line */
164					$j = $j + 1;
165
166					if ($oldTime == 0) {
167						$diff = 0 . " ms";
168					} else {
169						$diff = $timeTotalOp - $oldTime . " ms";
170					}
171
172					$ctrlCmds = removeZeros($arrayFile[$j]);
173
174					if (isset($cmdVD)) {
175						printf ("%06d: %06d ms %06d ms %s %s  %s%s\n",
176						       $URB + 0, $diff, $timeTotalOp, $ctrlCmds, $direction, $cmdVD, $cmdV);
177						$cmdVD = "";
178					}
179					else {
180						printf ("%06d: %06d ms %06d ms %s %s %s\n",
181						       $URB + 0, $diff, $timeTotalOp, $ctrlCmds, $direction, $cmdV);
182					}
183					$oldTime = $timeTotalOp;
184					break;
185				}
186				if (strstr($arrayFile[$j], "[")) {
187					break;
188				}
189			}
190
191		}
192
193		if (strstr($arrayFile[$j], "-- URB_FUNCTION_VENDOR_DEVICE:")) {
194
195			while(1) {
196
197				/* Next Line */
198				$j = $j + 1;
199
200				if (strstr($arrayFile[$j], "TransferBufferMDL")) {
201
202					/* Next Line */
203					$j = $j + 1;
204
205					if (strstr($arrayFile[$j], "UrbLink")) {
206						break;
207					}
208
209					$tmpCMD = "$arrayFile[$j]\n";
210					$cmdVD = removeZeros($tmpCMD);
211				}
212
213				if (strstr($arrayFile[$j], "[")) {
214					break;
215				}
216			}
217		}
218	}
219
220	fclose($file)
221?>
222