summaryrefslogtreecommitdiffstats
path: root/src/tools/debug/simics-debug-framework.py
blob: 359df50d9d159a7b2d3129ac826f01c7d11ebb6f (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
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/tools/debug/simics-debug-framework.py $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 2016,2019
# [+] International Business Machines Corp.
#
#
# 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
import os
import os.path
import subprocess
import re
import random
import sys
import imp
import struct

def getMachineName():
   try:
       sbeScriptsPath = simenv.sbe_scripts_path
       macineType = "axone"
   except:
       macineType = "nimbus"
   finally:
       return macineType

if getMachineName() == "axone":
    SBE_TOOLS_PATH = simenv.sbe_scripts_path
else:
    SBE_TOOLS_PATH = os.environ['SBE_TOOLS_PATH']
print "SBE_TOOLS_PATH = " +  SBE_TOOLS_PATH

testIstepAuto = imp.load_source("testIstepAuto", SBE_TOOLS_PATH + "/testIstepAuto.py")
sbeDebug = imp.load_source("sbeDebug", SBE_TOOLS_PATH + "/sbe-debug.py")
err = False
g_isFleetwood = 0

syms = {};

def check_sbe_tools_path ():
    global g_isFleetwood
    #Figure out if it's a fleetwood system

    #TODO: Assuming that axone cannot be fleetwood (multinode system). Confirm
    #this before merging the code.
    if getMachineName() == "axone":
        g_isFleetwood = 0
    else:
        cmd1 = "pipe \"echo $cec_type\" \"cat > temp1.map\""
        ( rc, out )  =   quiet_run_command( cmd1, output_modes.regular )
        if ( rc ):
          print "simics ERROR running %s: %d "%( cmd1, rc )
        with open('temp1.map', 'r') as f:
          map = f.read().strip()
          if(map == "p9_fleetwood"):
              g_isFleetwood = 1
          else:
              g_isFleetwood = 0

def get_dd_level(procNr = 0, nodeNr = 0):
    global g_isFleetwood

    if getMachineName() == "axone":
        ddlevel = 'AXONE'
    else:
        if g_isFleetwood:
            node = nodeNr + 1
            cmd = "pipe \"D"+str(node)+"Proc"+str(procNr)+".sbe.mibo_space.map\" \"cat > temp.map\""
        else:
            cmd = "pipe \"p9Proc"+str(procNr)+".sbe.mibo_space.map\" \"cat > temp.map\""

        print "simics running %s: "%( cmd)
        ( rc, out )  =   quiet_run_command( cmd, output_modes.regular )
        if ( rc ):
            print "simics ERROR running %s: %d "%( cmd, rc )
        ddlevel = 'DD1'
        with open('temp.map', 'r') as f:
            map = f.read()
            map = map.split()
            if g_isFleetwood:
                if map[map.index('D'+str(node)+'Proc'+str(procNr)+'.sbe.fi2c_bo:fi2cfsm')-1] == '0x80000000':
                    ddlevel = "DD1"
                if map[map.index('D'+str(node)+'Proc'+str(procNr)+'.sbe.fi2c_bo:fi2cfsm')-1] == '0xff800000':
                    ddlevel = "DD2"
            else:
                if map[map.index('p9Proc'+str(procNr)+'.sbe.fi2c_bo:fi2cfsm')-1] == '0x80000000':
                    ddlevel = "DD1"
                if map[map.index('p9Proc'+str(procNr)+'.sbe.fi2c_bo:fi2cfsm')-1] == '0xff800000':
                    ddlevel = "DD2"
    print "running image - ["+ddlevel+"]"
    return ddlevel

def register_sbe_debug_framework_tools():
    check_sbe_tools_path ()
    fillSymTable()
    # Create command hook.
    new_command("sbe-istep",istep_func,
                 args = [arg(float_t, "Major/start istep"), arg(float_t, "Minor/end istep"), arg(int_t, "arg", "?", 0)],
                 alias = "istep",
                 type = ["sbe-commands"],
                 short = "Runs the debug framework for istep ",
                 doc = "")
    new_command("sbe-trace", collectTrace,
                args = [arg(int_t, "procNr"), arg(int_t, "arg", "?", 0)],
                alias = "strace",
                type = ["sbe-commands"],
                short = "Runs the debug framework for trace ",
                doc = "")
    new_command("sbe-stack", collectStackUsage,
                args = [arg(int_t, "procNr"), arg(int_t, "arg", "?", 0)],
                alias = "sstack",
                type = ["sbe-commands"],
                short = "Runs the debug framework for stack usage ",
                doc = "")
    new_command("sbe-ddlevel", get_dd_level,
                args = [arg(int_t, "procNr"), arg(int_t, "arg", "?", 0)],
                alias = "sddlevel",
                type = ["sbe-commands"],
                short = "Runs the debug framework for dd level ",
                doc = "")
    new_command("sbe-attrdump", collectAttr,
                args = [arg(int_t, "procNr"), arg(int_t, "arg", "?", 0)],
                alias = "sattrdump",
                type = ["sbe-commands"],
                short = "Runs the debug framework for runtime attribute dump ",
                doc = "")
    new_command("sbe-regffdc", collectRegFfdc,
                args = [arg(int_t, "procNr"), arg(int_t, "arg", "?", 0)],
                alias = "sregffdc",
                type = ["sbe-commands"],
                short = "Runs the debug framework for register ffdc ",
                doc = "")

    print "SBE Debug Framework: Registered tool:", "sbe-istep"
    print "SBE Debug Framework: Registered tool:", "sbe-trace"
    print "SBE Debug Framework: Registered tool:", "sbe-stack"
    print "SBE Debug Framework: Registered tool:", "sbe-ddlevel"
    print "SBE Debug Framework: Registered tool:", "sbe-attrdump"
    print "SBE Debug Framework: Registered tool:", "sbe-regffdc"


def fillSymTable():
    symFile = SBE_TOOLS_PATH + "/sbe_"+get_dd_level()+".syms"
    f = open( symFile, 'r')
    for line in f:
        words = line.split()
        if( len( words ) == 4 ):
            syms[words[3]] = [words[0], words[1]]

# Print least available stack of each thread in SBE during a Run.
#
# Logic is - during init, ppe kernel fills the stack memory with '0xEFCDAB03'.
# So while traversing the stack, starting from lowest memory to the top,
# the first memory address where the pattern('0xEFCDAB03') is broken,
# will be the deepest stack usage point of tht thread during the run
def collectStackUsage ( procNr, nodeNr=0 ):
  global g_isFleetwood
  threads = ('sbeSyncCommandProcessor_stack',
             'sbeCommandReceiver_stack',
             'sbe_Kernel_NCInt_stack',
             'sbeAsyncCommandProcessor_stack')
  print "==================================Stack usage==================================="
  # Dump stack memory to binary files
  for thread in threads:
    if g_isFleetwood:
        node = nodeNr + 1
        cmd = "pipe \"D"+str(node)+"Proc"+str(procNr)+".sbe.mibo_space.x 0x" + syms[thread][0] + " 0x"+syms[thread][1]+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> "+thread+"\""
    else:
        if getMachineName() == "axone":
            cmd = "pipe \"backplane0.proc" + `procNr` + ".pib_cmp.sbe_mibo.x 0x" + syms[thread][0] + " 0x"+syms[thread][1]+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> "+thread+"\""
        else:
            cmd = "pipe \"p9Proc" + `procNr` + ".sbe.mibo_space.x 0x" + syms[thread][0] + " 0x"+syms[thread][1]+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> "+thread+"\""
    print "simics running %s: "%( cmd)
    ( rc, out )  =   quiet_run_command( cmd, output_modes.regular )
    if ( rc ):
        print "simics ERROR running %s: %d "%( cmd, rc )

  print "Thread".ljust(40)+"Least Available[bytes]".ljust(30)+"Max usage[%]"
  for thread in threads:
    with open(thread, "rb") as f:
        word = struct.unpack('I', f.read(4))[0]
        leastAvailable = 0
        while (1):
            if (word == int("0xEFCDAB03", 16)):
                leastAvailable += 4
                word = struct.unpack('I', f.read(4))[0]
            else:
                break
        print str("["+thread+"]").ljust(40) + str(leastAvailable).ljust(30) + str("%.2f" % (100 * (1 - (leastAvailable/float(int("0x"+syms[thread][1], 16))))))

def collectAttr( procNr, nodeNr=0 ):
  global g_isFleetwood
  if g_isFleetwood:
    node = nodeNr + 1
    cmd= "pipe \"D"+str(node)+"Proc"+str(procNr)+".sbe.mibo_space.x " + '0xFFFE8000' + " "+hex(96*1024)+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> DumpFullPIBMEM\""
  else:
    cmd= "pipe \"p9Proc" + `procNr` + ".sbe.mibo_space.x " + '0xFFFE8000' + " "+hex(96*1024)+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> DumpFullPIBMEM\""
  print "simics running %s: "%( cmd)
  ( rc, out )  =   quiet_run_command( cmd, output_modes.regular )
  if ( rc ):
    print "simics ERROR running %s: %d "%( cmd, rc )
  ddlevel = get_dd_level(procNr, nodeNr)
  sbeDebug.ddsuffix = ddlevel
  sbeDebug.target = 'FILE'
  sbeDebug.file_path = 'DumpFullPIBMEM'
  sbeDebug.fillSymTable()
  sbeDebug.collectAttr()

def collectRegFfdc( procNr, nodeNr=0 ):
  global g_isFleetwood
  if g_isFleetwood:
    node = nodeNr + 1
    cmd= "pipe \"D"+str(node)+"Proc"+str(procNr)+".sbe.mibo_space.x " + '0xFFFE8000' + " "+hex(96*1024)+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> DumpFullPIBMEM\""
  else:
    if getMachineName() == "axone":
      cmd = "pipe \"backplane0.proc" + `procNr` + ".pib_cmp.sbe_mibo.x " + '0xFFFE8000' + " "+hex(96*1024)+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> DumpFullPIBMEM\""
    else:
      cmd= "pipe \"p9Proc" + `procNr` + ".sbe.mibo_space.x " + '0xFFFE8000' + " "+hex(96*1024)+"\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> DumpFullPIBMEM\""
  print "simics running %s: "%( cmd)
  ( rc, out )  =   quiet_run_command( cmd, output_modes.regular )
  if ( rc ):
    print "simics ERROR running %s: %d "%( cmd, rc )
  ddlevel = get_dd_level(procNr, nodeNr)
  sbeDebug.ddsuffix = ddlevel
  sbeDebug.target = 'FILE'
  sbeDebug.file_path = 'DumpFullPIBMEM'
  sbeDebug.fillSymTable()
  sbeDebug.ppeStateFfdc()

def istep_func ( majorIstep, minorIstep, nodeNr=0 ):
  global g_isFleetwood
  testIstepAuto.sbe_istep_func(majorIstep, minorIstep, nodeNr, g_isFleetwood)

def collectTrace ( procNr, nodeNr=0 ):
  global g_isFleetwood
  if g_isFleetwood:
    node = nodeNr + 1
    fileName = "sbe_" + `nodeNr`+"_"+`procNr` + "_tracMERG"
    cmd1 = "pipe \"D"+str(node)+"Proc"+str(procNr)+".sbe.mibo_space.x 0x" + syms['g_pk_trace_buf'][0] + " 0x2028\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> ppetrace.bin\""
  else:
    fileName = "sbe_" + `procNr` + "_tracMERG"
    if getMachineName() == "axone":
        cmd1 = "pipe \"backplane0.proc" + `procNr` + ".pib_cmp.sbe_mibo.x 0x" + syms['g_pk_trace_buf'][0] + " 0x2028\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> ppetrace.bin\""
    else:
        cmd1 = "pipe \"p9Proc" + `procNr` + ".sbe.mibo_space.x 0x" + syms['g_pk_trace_buf'][0] + " 0x2028\" \"sed 's/^p:0x........ //g' | sed 's/ ................$//g' | sed 's/ //g' | xxd -r -p> ppetrace.bin\""
  cmd2 = "shell \"" + SBE_TOOLS_PATH + "/ppe2fsp ppetrace.bin sbetrace.bin \""
  if getMachineName() == "axone":
    cmd3 = "shell \"" + SBE_TOOLS_PATH + "/fsp-trace -s " + SBE_TOOLS_PATH + "/sbeStringFile_"+get_dd_level(procNr, nodeNr)+" sbetrace.bin >" +  fileName + "\""
  else:
    cmd3 = "shell \"fsp-trace -s " + SBE_TOOLS_PATH + "/sbeStringFile_"+get_dd_level(procNr, nodeNr)+" sbetrace.bin >" +  fileName + "\""
  cmd4 = "shell \"" + "cat " + fileName + "\""

  print "simics running %s: "%( cmd1)
  ( rc, out )  =   quiet_run_command( cmd1, output_modes.regular )
  if ( rc ):
    print "simics ERROR running %s: %d "%( cmd1, rc )

  run_command ( cmd2 )
  run_command ( cmd3 )
  run_command ( cmd4 )

# MAGIC_INSTRUCTION hap handler
# arg contains the integer parameter n passed to MAGIC_INSTRUCTION(n)
# See src/include/arch/ppc.H for the definitions of the magic args.
# SBE  magic args should range 8000..8190.
def sbe_magic_instruction_callback(user_arg, cpu, inst_num):
    # Check if its for ppe processor
    if(SIM_get_class_name(SIM_object_class(cpu)) != "ppe"):
        return;

    if inst_num == 8000: #MAGIC_SIMICS_CHECK
        iface = SIM_get_interface(cpu, "int_register")
        iface.write(iface.get_number("r3"), 1)
        print "SBE::isSimicsRunning = true"

# Run the registration automatically whenever this script is loaded.
register_sbe_debug_framework_tools()

# Register the magic instruction hap handler (a callback).
# Currently registering a range does not work on simics for sbe
SIM_hap_add_callback( "Core_Magic_Instruction", sbe_magic_instruction_callback, None )

OpenPOWER on IntegriCloud