summaryrefslogtreecommitdiffstats
path: root/src/build/vpo/hb-virtdebug.pl
blob: 780cd907fbb955b52e8e8db38d19d8e82210b3aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/vpo/hb-virtdebug.pl $
#
# OpenPOWER HostBoot Project
#
# COPYRIGHT International Business Machines Corp. 2011,2014
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG
#
# Purpose:  This perl script works on VBU and will dump either the entire L3 or
# relevant data such as the code version, kernel printk buffer & component traces.
#
# Author: CamVan Nguyen

##################################################################################
#
# Version 1.1   05-09-2012   Start version history
#                            - Use HB_VBUTOOLS env if set
#                            - Use proc_thread_control_wrap in place of
#                              p8_ins_stop/start/query
#
# Version 1.2   06-11-2012   Change "-c3" to "-cft" (first avail core)
#
##################################################################################

#------------------------------------------------------------------------------
# Specify perl modules to use
#------------------------------------------------------------------------------
use strict;
use warnings;
use POSIX;
use Cwd;


#------------------------------------------------------------------------------
# Constants
#------------------------------------------------------------------------------
use constant MAX_NUM_TRACE_BUFFERS => 48;
use constant DESC_ARRAY_ENTRY_ADDR_SIZE => 8;
use constant DESC_ARRAY_ENTRY_COMP_NAME_SIZE => 16;
use constant TRAC_DEFAULT_BUFFER_SIZE => 0x0800;
use constant CACHE_LINE_SIZE => 128;
use constant TRAC_BUFFER_SIZE_OFFSET => 20;
use constant TRAC_BUFFER_SIZE_SIZE => 4;
use constant NUMTHREADS => 8;


#------------------------------------------------------------------------------
# Forward Declaration
#------------------------------------------------------------------------------
sub getAddrNSize;
sub readBinFile;
sub readStringBinFile;
sub writeBinFile;
sub appendBinFile;
sub printUsage;


#==============================================================================
# MAIN
#==============================================================================

#------------------------------------------------------------------------------
# Parse optional input arguments
#------------------------------------------------------------------------------
my $numArgs = $#ARGV + 1;
#print "num args = $numArgs\n";
#print "arg list: @ARGV\n";

#Initialize default settings
my $hbSymsFile = "hbicore.syms";  #Use hbicore.syms
my $hbStringFile = "hbotStringFile";
my $hbErrlParser = "errlparser";
my $dumpPrintk = 0;               #Flag to dump printk
my $dumpTrace = 0;                #Flag to dump trace buffers
my $dumpErrl = 0;                 #Flag to dump error logs
my $dumpErrlList = 1;             #Flag to dump a listing of all error logs
my $dumpErrlDtl = 0;              #Flag to dump error log detail data
my $errLogId = "all";             #Error log id; default = all
my $dumpAll = 1;                  #Flag to dump everything
my @comp;                         #Array of component trace buffers to dump
my @symsLines;                    #Array to store the .syms file data
my $outDir = getcwd();            #Default = current working directory
my @ecmdOpt;                      #Array of ecmd options
my $core = "ft";                  #Default is first core ("ft")
my @threadState = ();             #Array to store the thread states

# Use HB_VBUTOOLS if it's set; otherwise, try to use the latest tool directory
my $vbuToolDir = $ENV{'HB_VBUTOOLS'};
if (defined ($vbuToolDir))
{
    unless ($vbuToolDir ne "")
    {
        $vbuToolDir = "/gsa/ausgsa/projects/h/hostboot/vbutools/latest";
    }
}

my $hbDir = $ENV{'HB_IMGDIR'};
if (defined ($hbDir))
{
    unless ($hbDir ne "")
    {
        $hbDir = '.';             #Set to current directory
    }
}
else
{
    $hbDir = '.';                 #Set to current directory
}

for (my $i=0; $i<$numArgs; $i++)
{
    if (($ARGV[$i] eq "--help") || ($ARGV[$i] eq "-h"))
    {
        #Print command line help
        printUsage();
        exit (0);
    }
    elsif ($ARGV[$i] eq "--img-path")
    {
        if (($i + 1) >= $numArgs)
        {
            die "No value given for --img-path parameter.\n";
        }
        $i++;
        $hbDir = $ARGV[$i];
    }
    elsif ($ARGV[$i] eq "--out-path")
    {
        if (($i + 1) >= $numArgs)
        {
            die "No value given for --out-path parameter.\n";
        }
        $i++;
        $outDir = $ARGV[$i];
    }
    elsif ($ARGV[$i] eq "--test")
    {
        #Use hbicore_test.syms
        $hbSymsFile = 'hbicore_test.syms';
    }
    elsif ($ARGV[$i] eq "--printk")
    {
        #Set flag to dump printk
        $dumpPrintk = 1;
        $dumpAll = 0;
    }
    elsif ($ARGV[$i] eq "--trace")
    {
        #Set flag to dump component trace buffers
        $dumpTrace = 1;
        $dumpAll = 0;

        for ( $i += 1; $i < $numArgs; $i++)
        {
            if (substr($ARGV[$i], 0, 1) eq '-')
            {
                $i -=1;
                last;
            }

            push (@comp, $ARGV[$i]);
        }
    }
    elsif ($ARGV[$i] eq "--errl")
    {
        #Set flag to dump the error logs
        $dumpErrl = 1;
        $dumpAll = 0;

        last if (($i + 1) >= $numArgs);
        $i++;

        if ($ARGV[$i] eq "-d")
        {
            $dumpErrlList = 0;
            $dumpErrlDtl = 1;

            last if (($i + 1) >= $numArgs);
            $i++;

            if (substr($ARGV[$i], 0, 1) eq '-')
            {
                $i--;
            }
            else
            {
                if (isdigit($ARGV[$i]))
                {
                    $errLogId = $ARGV[$i];
                }
                else
                {
                    die "ERROR:  Enter logid or 'all'"
                        unless ($ARGV[$i] =~ /all/i);
                }
            }
        }
        elsif ($ARGV[$i] ne "-l")
        {
            $i--;
        }
    }
    elsif ($ARGV[$i] =~ m/^-[c](\d+)/)
    {
        $core = $1;
    }
    elsif ($ARGV[$i] =~ m/^-[knsp]\d+/)
    {
        push(@ecmdOpt, $ARGV[$i]);
    }
    else
    {
        print "Invalid argument entered:  $ARGV[$i]\n";
        printUsage();
        exit(1);
    }
}

push(@ecmdOpt, "-c$core");
#print "ecmd options = @ecmdOpt\n";


#------------------------------------------------------------------------------
# Check for files needed to dump printk and component traces
#------------------------------------------------------------------------------
if (!$dumpAll)
{
    #Need .syms file for error logs, printk and traces
    if (!(-e "$hbDir/$hbSymsFile"))
    {
      die "Cannot find $hbDir/$hbSymsFile\n";
    }

    #Need string file for traces
    if (!(-e "$hbDir/$hbStringFile") && $dumpTrace)
    {
      die "Cannot find $hbDir/$hbStringFile\n";
    }

    #Need errlparser for error logs
    if (!(-e "$hbDir/$hbErrlParser") && $dumpErrl)
    {
      die "Cannot find $hbDir/$hbErrlParser\n";
    }

    #Print the files that will be used
    print "hostboot syms file: $hbDir/$hbSymsFile\n";

    if ($dumpTrace)
    {
        print "hostboot string file: $hbDir/$hbStringFile\n";
    }

    if ($dumpErrl)
    {
        print "hostboot error log parser: $hbDir/$hbErrlParser\n";
    }
    #------------------------------------------------------------------------------
    # Open and read the .syms file
    #------------------------------------------------------------------------------
    open SYMSFILE, "$hbDir/$hbSymsFile" or
        die "ERROR: $hbDir/$hbSymsFile not found : $!";
    @symsLines = <SYMSFILE>;        # Read it into an array
    close(SYMSFILE);                   # Close the file

    unless (@symsLines)
    {
        print "ERROR: $hbDir/$hbSymsFile is empty\n";
        exit (1);
    }
}

#------------------------------------------------------------------------------
# Save the original thread states and stop instructions
#------------------------------------------------------------------------------
saveThreadStates();
stopInstructions("all");

#------------------------------------------------------------------------------
#Flush L2 - this step is needed in order to dump L3 quickly
#------------------------------------------------------------------------------
my $command = "";
$command = "$vbuToolDir/proc_l2_flush_wrap.x86 @ecmdOpt -quiet";
#print "$command\n";
die "ERROR: cannot flush L2" if (system("$command") != 0);


#------------------------------------------------------------------------------
# Dump the kernel printk buffer
#------------------------------------------------------------------------------
my $buffer = 0;
my $offset = 0;
my $cacheLines = 0;
my $string = "";
my $addr = 0;
my $size = 0;

if ($dumpPrintk)
{
    #Find address and size of the kernel_printk_buffer from the .syms file
    $string = 'kernel_printk_buffer';
    ($addr, $size) = getAddrNSize($string, \@symsLines);

    if ((0 != $addr) && (0 != $size))
    {
        print "\nReading the kernel printk buffer...\n\n";

        $string = "$outDir/$string";

        $offset = $addr % CACHE_LINE_SIZE;
        $cacheLines = ceil($size / CACHE_LINE_SIZE);
        if ($offset != 0)
        {
            $cacheLines += 1;
        }
        #print "addr $addr, offset $offset, size $size, cacheLines $cacheLines\n";

        #Read the kernel printk buffer from L3 and save to file
        $command = sprintf ("$vbuToolDir/p8_dump_l3 %x $cacheLines -f $string -b @ecmdOpt",
                            $addr);
        print "$command\n";
        die if (system("$command") != 0);

        if (-s $string)
        {
            #Extract and save just the kernel printk buffer
            $buffer = readStringBinFile($string, $offset);

            writeBinFile($string, $buffer);

            #Output to screen
            print "\nKernel printk buffer:";
            print "\n=====================\n\n$buffer\n";
            print "\n=====================\n\n";
            print "Data saved to file $string\n\n";
        }
        else
        {
            print "\nWARNING: Cannot read the kernel printk buffer.\n";
        }
    }
}


#------------------------------------------------------------------------------
# Extract the component traces
#------------------------------------------------------------------------------
if ($dumpTrace)
{
    #Find address and size of the g_desc_array from the .syms file
    $string = 'g_desc_array';
    ($addr, $size) = getAddrNSize($string, \@symsLines);

    if ((0 != $addr) && (0 != $size))
    {
        print "\nReading the component trace buffer(s)...\n\n";

        $string = "$outDir/$string";

        #Read the g_desc_array from L3 and save to file
        $offset = $addr % CACHE_LINE_SIZE;
        $cacheLines = ceil($size / CACHE_LINE_SIZE);
        if ($offset != 0)
        {
            $cacheLines += 1;
        }
        #print "addr $addr, offset $offset, size $size, cacheLines $cacheLines\n";

        $command = sprintf ("$vbuToolDir/p8_dump_l3 %x $cacheLines -f $string -b @ecmdOpt",
                            $addr);
        print "$command\n";
        die if (system("$command") != 0);

        #Save the trace buffers
        $addr = $offset;
        for (my $i = 0; ($i < MAX_NUM_TRACE_BUFFERS) && (-s $string); $i++)
        {
            #Get the component name
            my $compName = readStringBinFile($string, $addr);
            chomp $compName;
            last if ($compName eq "");

            #Get the component trace buffer address
            $addr += DESC_ARRAY_ENTRY_COMP_NAME_SIZE;
            $buffer = readBinFile($string, $addr, DESC_ARRAY_ENTRY_ADDR_SIZE);
            my $compBufAddr= unpack('H*',$buffer);
            $compBufAddr = hex $compBufAddr;
            $addr += DESC_ARRAY_ENTRY_ADDR_SIZE;

            if ((grep $_ eq $compName, @comp) || (@comp == 0))
            {
                #Read the component trace buffer and save to file
                $offset = $compBufAddr % CACHE_LINE_SIZE;
                $cacheLines = ceil(TRAC_DEFAULT_BUFFER_SIZE / CACHE_LINE_SIZE);
                if ($offset != 0)
                {
                    $cacheLines += 1;
                }
                #print "$compName, addr $compBufAddr, offset $offset, cacheLines $cacheLines\n";

                $command = sprintf ("$vbuToolDir/p8_dump_l3 %x $cacheLines -f $outDir/trace.out -b @ecmdOpt",
                                   $compBufAddr);
                print "$command\n";
                die if (system("$command") != 0);

                # Get the length of the buffer from the component trace header
                $buffer = readBinFile( "$outDir/trace.out",
                                       $offset+TRAC_BUFFER_SIZE_OFFSET,
                                       TRAC_BUFFER_SIZE_SIZE );

                my $compBufferSize=unpack('H*',$buffer);
                $compBufferSize = hex $compBufferSize;

                # Re-read trace buffer using correct buffer size
                $offset = $compBufAddr % CACHE_LINE_SIZE;
                $cacheLines = ceil( $compBufferSize / CACHE_LINE_SIZE);
                if ($offset != 0)
                {
                    $cacheLines += 1;
                }
                #print "$compName, addr $compBufAddr, offset $offset, cacheLines $cacheLines\n";
                $command = sprintf ("$vbuToolDir/p8_dump_l3 %x $cacheLines -f $outDir/trace.out -b @ecmdOpt",
                                   $compBufAddr);
                print "$command\n";
                die if (system("$command") != 0);

                #Extract just the component trace
                $buffer = readBinFile("$outDir/trace.out", $offset, $compBufferSize);

                #Append to tracBIN
                appendBinFile("$outDir/tracBIN", $buffer);
                unlink "$outDir/trace.out";
            }
        }

        #Check if file exists and is not empty
        if (-z $string)
        {
            print "\nWARNING: Cannot read the component trace buffers.\n";
        }
        elsif (-s "$outDir/tracBIN")
        {
            print "\n";

            #create tracMERG file
            `fsp-trace -s $hbDir/$hbStringFile $outDir/tracBIN | tee $outDir/tracMERG`;

            #Check if file exists and is not empty
            #This will be false if the fsp-trace tool cannot be found
            if (-s "$outDir/tracMERG")
            {
                open FILE, "$outDir/tracMERG" or die "ERROR: $!";
                my @lines = <FILE>;        # Read it into an array
                close(FILE);               # Close the file
                print "\nComponent trace buffer(s):";
                print "\n==========================\n\n";
                print "@lines\n";        # Output to screen
                print "\n==========================\n\n";
                print "Data saved to $outDir/tracMERG\n\n";

                #delete tracBIN file
                unlink "$outDir/tracBIN";
            }
            else
            {
                print "\nData saved to $outDir/tracBIN\n\n";
            }
        }
        else
        {
            print "\nComponent trace buffer(s) not found.\n\n";
        }

        #Delete g_desc_array file
        unlink "$string";
    }
}


#------------------------------------------------------------------------------
# Dump the error logs
#------------------------------------------------------------------------------
if ($dumpErrl)
{
    #Find address and size of the g_ErrlStorage from the .syms file
    #TODO: This is broken - Expect all this to disappear with Story 47517
    $string = 'g_ErrlStorage';
    ($addr, $size) = getAddrNSize($string, \@symsLines);

    if ((0 != $addr) && (0 != $size))
    {
        print "\nReading the error log(s)...\n\n";

        $string = "$outDir/$string";

        #Read the binary error log buffer from L3 and save to file
        $offset = $addr % CACHE_LINE_SIZE;
        $cacheLines = ceil($size / CACHE_LINE_SIZE);
        if ($offset != 0)
        {
            $cacheLines += 1;
        }
        #print "addr $addr, offset $offset, size $size, cacheLines $cacheLines\n";

        $command = sprintf ("$vbuToolDir/p8_dump_l3 %x $cacheLines -f $string -b @ecmdOpt",
                            $addr);
        print "$command\n";
        die if (system("$command") != 0);

        if (-s $string)
        {
            #Extract and save just the error log buffer
            $buffer = readBinFile($string, $offset, $size);
            writeBinFile($string, $buffer);

            #Parse error log buffer and save to file
            my $hbErrlFile = "$outDir/Errorlogs";
            if ($dumpErrlList)
            {
                $command = sprintf("$hbDir/$hbErrlParser $string|tee $hbErrlFile");
            }
            else
            {
                $command = sprintf("$hbDir/$hbErrlParser $string -d $errLogId |tee $hbErrlFile");
            }
            die if (system("$command") != 0);

            if (-s $hbErrlFile)
            {
                print "\n\nData saved to file $hbErrlFile\n\n";
                unlink "$string";
            }
        }
        else
        {
            print "\nWARNING: Cannot read the error logs.\n";
        }
    }
}


#------------------------------------------------------------------------------
#Dump the entire L3 to a file
#------------------------------------------------------------------------------
if ($dumpAll)
{
    print "\nDumping L3...\n\n";

    #Get current timestamp
    my $timeStamp = strftime "%Y%m%d%H%M\n", localtime;
    chomp $timeStamp;
    #print "timestamp: $timeStamp\n";

    #Dump L3 to file
    my $hbDumpFile = "$outDir/hbdump.$timeStamp";
    $command = "$vbuToolDir/p8_dump_l3 0 65536 -f $hbDumpFile -b @ecmdOpt";
    #print "$command\n";
    die "ERROR: cannot dump L3" if (system("$command") != 0);

    #Check if hbDumpFile exists and is not empty
    if (-s "$hbDumpFile")
    {
        print "\nHostBoot dump saved to $hbDumpFile.\n";
        print "Use the hb-dump-debug program to parse the dump.\n";
    }
    else
    {
        print "\nWARNING: Cannot dump L3.  Did you stop instructions?\n\n";
        unlink $hbDumpFile;
    }
}

#------------------------------------------------------------------------------
# Restore the original thread states
#------------------------------------------------------------------------------
restoreThreadStates();



#==============================================================================
# SUBROUTINES
#==============================================================================

#------------------------------------------------------------------------------
# Stop instructions
#------------------------------------------------------------------------------
sub stopInstructions
{
    my $thread = shift;

    #Stopping all threads
    my $command = "$vbuToolDir/proc_thread_control_wrap.x86";
    $command .= " @ecmdOpt -stop -t$thread -quiet";
    die "ERROR: cannot stop instructions" if (system("$command") != 0);
}

#------------------------------------------------------------------------------
# Start instructions
#------------------------------------------------------------------------------
sub startInstructions
{
    my $thread = shift;

    #Starting all threads
    my $command = "$vbuToolDir/proc_thread_control_wrap.x86";
    $command .= " @ecmdOpt -start -t$thread -quiet";
    die "ERROR: cannot start instructions" if (system("$command") != 0);
}

#------------------------------------------------------------------------------
# Query thread state
# @brief query whether thread state is quiesced or running
#------------------------------------------------------------------------------
sub queryThreadState
{
    my $thread = shift;
    #print "thread $thread\n";

    my $command = "$vbuToolDir/proc_thread_control_wrap.x86 @ecmdOpt -query -t$thread";
    my $result = `$command`;
    #print "result:\n $result";
    if ($result =~ m/Quiesced/)
    {
        #print "Thread $thread is quiesced\n";
        return "Quiesced";
    }
    #print "Thread $thread is running\n";
    return "Running";
}

#------------------------------------------------------------------------------
# Save thread states
# @brief Save the thread states
#------------------------------------------------------------------------------
sub saveThreadStates
{
    for (my $i = 0; $i < NUMTHREADS; $i++)
    {
        push (@threadState, queryThreadState($i));
    }
}

#------------------------------------------------------------------------------
# Restore thread states
# @brief Restore the thread states
#------------------------------------------------------------------------------
sub restoreThreadStates
{
    for (my $i = 0; $i < NUMTHREADS; $i++)
    {
        my $curState = queryThreadState($i);
        if ($threadState[$i] ne $curState)
        {
            if ("Quiesced" eq $curState)
            {
                startInstructions($i);
            }
            else
            {
                stopInstructions($i);
            }
        }
    }
}

#------------------------------------------------------------------------------
# Parse the .syms data to find the relevant address and size for the data
# requested.
#------------------------------------------------------------------------------
sub getAddrNSize($\@)
{
    my $addr = 0;
    my $size = 0;

    my $string = $_[0];
    my (@array) = @{$_[1]};
    #print "$string\n";
    #print "@array\n";

    #search for string in array
    my @line = grep /$string/,@array;
    #print "@line\n";

    #if found string
    if (@line)
    {
        my @list = split(/,+/,$line[0]);
        #print "@list\n";

        $addr = hex $list[1];
        $size = hex $list[3];
        #print "$addr\n";
        #print "$size\n";
    }

    return($addr, $size);
}

#------------------------------------------------------------------------------
# Read a block of data from a binary file.
#------------------------------------------------------------------------------
sub readBinFile($$$)
{
    my ($file, $addr, $size) = @_;
    #print "$file, $addr, $size\n";

    #Open the dump file for reading
    open FILE, $file or die "ERROR: $file not found : $!";
    binmode FILE;

    seek FILE, $addr, 0 or die "Couldn't seek to $addr in $file: $!\n";
    #print tell FILE; print "\n";
    my $bytesRead = read(FILE, my $buffer, $size);
    #print tell FILE; print "\n";
    #print "#bytes read: $bytesRead\n";
    #print "buffer: $buffer\n";

    close (FILE);
    return ($buffer);
}

#------------------------------------------------------------------------------
# Read a NULL terminated string from a binary file.
#------------------------------------------------------------------------------
sub readStringBinFile($$)
{
    my ($file, $addr) = @_;
    #print "$file, $addr\n";

    #Open the dump file for reading
    open FILE, $file or die "ERROR: $file not found : $!";
    binmode FILE;

    local $/ = "\0";       #Set to NULL termination
    #my $tmp = $/;
    #$/ = "\0";            #Set to NULL termination
    seek FILE, $addr, 0 or die "Couldn't seek to $addr in $file: $!\n";
    #print tell FILE; print "\n";
    my $string = <FILE>;
    #print tell FILE; print "\n";
    chomp $string;         #Remove NULL termination
    #print "$string\n";
    #$/ = $tmp;            #Restore $/

    close (FILE);
    return ($string);
}

#------------------------------------------------------------------------------
# Write a block of data to a binary file.
#------------------------------------------------------------------------------
sub writeBinFile($$)
{
    my ($file, $buffer) = @_;
    open (FILE, ">$file") or die "ERROR: $file cannot be opened: $!";
    binmode FILE;
    print FILE $buffer;
    close (FILE);
}

#------------------------------------------------------------------------------
# Append a block of data to a binary file.
#------------------------------------------------------------------------------
sub appendBinFile($$)
{
    my ($file, $buffer) = @_;
    open (FILE, ">>$file") or die "ERROR: $file cannot be opened: $!";
    binmode FILE;
    print FILE $buffer;
    close (FILE);
}

#------------------------------------------------------------------------------
# Print command line help
#------------------------------------------------------------------------------
sub printUsage()
{
    print ("\nUsage: hb-virtdebug.pl [--help] |\n");
    print ("                  [--img-path <path to .syms file, hbotStringFile & errlparser>]\n");
    print ("                  [--out-path <path to save output data>]\n");
    print ("                  [--test] [--errl [-l | -d [<logid>|all]] [--printk]\n");
    print ("                  [--trace [<compName1 compName2 compName3 ...>]]\n");
    print ("                  [-k#] [-n#] [-s#] [-p#] [-c#]\n\n");
    print ("  This program retrieves the user requested data from L3.\n");
    print ("  If no options are specified, this program will dump the entire L3 to a file.\n");
    print ("  Use the hb-dump-debug program to expand and view data in the file.\n\n");
    print ("  User should copy the relevant .syms file, hbotStringFile & errlparser\n");
    print ("  to the current directory or set the env variable HB_IMGDIR to the path\n");
    print ("  of the files.\n\n");
    print ("  User should also set the env variable PATH to include the path to the fsp-trace program.\n\n");
    print ("  --help            Prints usage information\n");
    print ("  --img-path        Overrides the automatically detected .syms file,\n");
    print ("                    hbotStringFile & errlparser in HB_IMGDIR or the current\n");
    print ("                    directory.  This program will search for the files in\n");
    print ("                    the following order:\n");
    print ("                        1.  from the path specified by user\n");
    print ("                        2.  from HB_IMGDIR if it is defined\n");
    print ("                        3.  from the current directory\n");
    print ("  --out-path        Directory where the output data will be saved\n");
    print ("                    Default path is the current directory\n");
    print ("  --test            Use the hbicore_test.syms file vs the hbicore.syms file\n");
    print ("  --errl            Dumps the error logs\n");
    print ("      -l            Dumps a listing of all the error logs\n");
    print ("      -d <logid>    Dumps detailed data of the specified error log\n");
    print ("      -d [all]      Dumps detailed data of all error logs\n");
    print ("  --printk          Dumps the kernel printk buffer only\n");
    print ("  --trace           Dumps all or just the user specified component trace buffer(s)\n");
    print ("  -k#               Specify which cage to act on (default = 0)\n");
    print ("  -n#               Specify which node to act on (default = 0)\n");
    print ("  -s#               Specify which slot to act on (default = 0)\n");
    print ("  -p#               Specify which chip position to act on (default = 0)\n");
    print ("  -c#               Specify which core/chipUnit to act on (default = ft)\n");
}

OpenPOWER on IntegriCloud