libraries/clogger/pctools/unhex.pl
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 #!perl
       
     2 # unhex.pl
       
     3 # 
       
     4 # Copyright (c) 2010 Accenture. All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of the "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 # 
       
    10 # Initial Contributors:
       
    11 # Accenture - Initial contribution
       
    12 #
       
    13 
       
    14 # Takes a standard hexdump of format:
       
    15 # 6640000c: 55 00 39 00 54 cb 00 00 42 41 46 4c 20 2d 20 42 U.9.T...BAFL - B
       
    16 # and outputs the concatenation of the ascii bits
       
    17 #
       
    18 # if -c is specified, it will further try to parse the hexdump as if it is a clogger debug router buffer
       
    19 
       
    20 if (scalar(@ARGV) == 1 && $ARGV[0] eq "-h")
       
    21 	{
       
    22 	print << "ENDHELP";
       
    23 Syntax: perl unhex.pl [-c]
       
    24 Reads a hexdump from stdin, unhexes it and outputs to stdout.
       
    25 If -c is specified, attempts to parse the hexdump as a clogger
       
    26 RDebug::Print crashdumparea buffer.
       
    27 ENDHELP
       
    28 	exit 0;
       
    29 	}
       
    30 my $clogger = scalar(@ARGV) == 1 && $ARGV[0] eq "-c";
       
    31 
       
    32 my $currentBuf = "";
       
    33 
       
    34 while($line = <STDIN>)
       
    35 	{
       
    36 	if ($line =~ /.*: (.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..)  ?(.*)/)
       
    37 		{
       
    38 		my $hex = $1;
       
    39 		my $frag = "";
       
    40 		my $strlen = length($hex);
       
    41 		#print "strlen = $strlen\n";
       
    42 		for (my $i = 0; $i < $strlen; $i+=3)
       
    43 			{
       
    44 			my $c = hex(substr($hex, $i, 2));
       
    45 			#print ("c = $c\n");
       
    46 			if (!$clogger)
       
    47 				{
       
    48 				# Then escape non-ascii
       
    49 				if ($c != 13 && $c != 10 && ($c < 32 || $c > 127)) { $c = ord("."); }
       
    50 				}
       
    51 			$frag = $frag.chr($c);
       
    52 			}
       
    53 			
       
    54 		if (!$clogger)
       
    55 			{
       
    56 			# Just print it
       
    57 			print($frag);
       
    58 			}
       
    59 		else
       
    60 			{
       
    61 			if ($frag eq "\0\0\0\0\0\0\0\0" || $frag eq "\3\3\3\3\3\3\3\3")
       
    62 				{
       
    63 				# This indicates the buffer doesn't have any data in it. 0x03 is used by as-yet unwritten-to chunks, it would appear
       
    64 				$frag = "";
       
    65 				}
       
    66 			$currentBuf .= $frag;
       
    67 			my $len = length($currentBuf);
       
    68 			my $start = index($currentBuf, "U\0");
       
    69 			if ($start == -1) { $start = index($currentBuf, "K\0"); }
       
    70 			if ($start == -1) { $start = index($currentBuf, "P\0"); }
       
    71 			if ($start == -1 || $start + 8 > $len)
       
    72 				{
       
    73 				# No header in buf, or not all of it
       
    74 				}
       
    75 			else
       
    76 				{
       
    77 				my ($type, $entryLen, $timeStamp) = unpack("a1xvV", substr($currentBuf, $start));
       
    78 				#print("type = $type, entryLen = $entryLen, timestamp = $timeStamp\n");
       
    79 				
       
    80 				if (8 + $start + $entryLen <= $len)
       
    81 					{
       
    82 					# Check if there was any junk in the buffer before the first header
       
    83 					if ($start > 0)
       
    84 						{
       
    85 						my $s = substr($currentBuf, 0, $start);
       
    86 						printf("00000000 $s\n");
       
    87 						}
       
    88 					$entry = substr($currentBuf, $start + 8, $entryLen);
       
    89 					printf("%08x %s\n", $timeStamp, $entry);
       
    90 					$currentBuf = substr($currentBuf, $start + 8 + $entryLen);
       
    91 					}
       
    92 				}
       
    93 			}
       
    94 		}
       
    95 	}
       
    96 if (length($currentBuf))
       
    97 	{
       
    98 	# The log is truncated, so just print what we have unparsed
       
    99 	print("$currentBuf\n");
       
   100 	}