Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:47

0001 #!/usr/bin/python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 ## NB system python on macOS
0023 
0024 g4lldb.py
0025 =============
0026 
0027 
0028 python interface inside gdb ? yes since version 7
0029 ---------------------------------------------------
0030 
0031 * :google:`gdb python scripting`
0032 
0033 * https://fy.blackhats.net.au/blog/html/2017/08/04/so_you_want_to_script_gdb_with_python.html
0034 * https://sourceware.org/gdb/onlinedocs/gdb/Python.html#Python
0035 * https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html
0036 * https://devguide.python.org/gdb/
0037 * https://medium.com/@tarun27sh/python-scripts-for-gdb-9b17ca090ac5
0038 * https://stripe.com/en-it/blog/exploring-python-using-gdb
0039 
0040 
0041 THOUGHTS ON CODE FOR DEBUGGING
0042 ------------------------------
0043 
0044 * resist adding code just for debugging access, instead find where 
0045   the state you want to get at is already 
0046 
0047 
0048 Using lldb breakpoint python scripting
0049 ---------------------------------------
0050 
0051 ::
0052 
0053     tboolean-;tboolean-box --okg4 --align --mask 1230 --pindex 0 -DD
0054 
0055         # special -DD debug launching: 
0056         #
0057         # 1. update the breakpoint setup, via parsing this file
0058         # 2. source the commands on starting lldb 
0059 
0060 
0061 Background on g4lldb python scripting
0062 -----------------------------------
0063 
0064 * moved to env-/lldb-vi as got too long 
0065 * see also env-/tools/lldb_/standalone.py for development of evaluation functions
0066 
0067 
0068 ::
0069 
0070     >>> from opticks.tools.evaluate import EV ; ev = EV(lldb.frame.FindVariable("this"))
0071 
0072 
0073 """
0074 
0075 import os, sys, logging, re, inspect
0076 
0077 from opticks.ana.ucf import UCF
0078 from opticks.ana.bouncelog import BounceLog
0079 
0080 from opticks.class="varlink" href="/lxr/search?_filestring=tools">tools.lldb_ import lldb
0081 from opticks.class="varlink" href="/lxr/search?_filestring=tools">tools.autobreakpoint import AutoBreakPoint
0082 from opticks.class="varlink" href="/lxr/search?_filestring=tools">tools.evaluate import Evaluate, Value, EV
0083 from opticks.class="varlink" href="/lxr/search?_filestring=tools">tools.loc import Loc
0084 
0085 log = logging.getLogger(__name__)
0086 
0087 ENGINE = None
0088 def CRandomEngine_cc_preTrack(frame, bp_loc, sess):
0089     """
0090     preTrack label
0091     """
0092     global ENGINE
0093     ENGINE = CRandomEngine()
0094     ploc = Loc(sys._getframe(), __name__)
0095     return ENGINE.preTrack(ploc, frame, bp_loc, sess)
0096 
0097 def CRandomEngine_cc_flat(frame, bp_loc, sess):
0098     """
0099     flat label
0100     """
0101     ploc = Loc(sys._getframe(), __name__)
0102     return ENGINE.flat(ploc,frame, bp_loc, sess)
0103     
0104 def CRandomEngine_cc_jump(frame, bp_loc, sess):
0105     """
0106     jump label
0107     """
0108     ploc = Loc(sys._getframe(), __name__)
0109     return ENGINE.jump(ploc,frame, bp_loc, sess)
0110 
0111 def CRandomEngine_cc_postStep(frame, bp_loc, sess):
0112     """
0113     postStep label
0114     """
0115     ploc = Loc(sys._getframe(), __name__)
0116     return ENGINE.postStep(ploc,frame, bp_loc, sess)
0117  
0118 def CRandomEngine_cc_postTrack(frame, bp_loc, sess):
0119     """
0120     postTrack label
0121     """
0122     ploc = Loc(sys._getframe(), __name__)
0123     return ENGINE.postTrack(ploc,frame, bp_loc, sess)
0124 
0125 
0126 
0127 
0128 
0129 def _G4SteppingManager_cc_191(frame, bp_loc, sess):
0130     """
0131     After DefinePhysicalStepLength() sets PhysicalStep and fStepStatus, before InvokeAlongStepDoItProcs()
0132 
0133     g4-;g4-cls G4SteppingManager 
0134     """
0135     ploc = Loc(sys._getframe(), __name__)
0136 
0137     self = EV(frame.FindVariable("this"))
0138     fStepStatus = self.ev("fStepStatus")
0139 
0140     print "%s : %20s : %s " % (ploc.tag, fStepStatus, ploc.label)
0141     return False
0142 
0143 def _DsG4OpBoundaryProcess_cc_StepTooSmall(frame, bp_loc, sess):
0144     ploc = Loc(sys._getframe(), __name__)
0145     self = EV(frame.FindVariable("this"))
0146     theStatus = self.ev("theStatus")
0147 
0148     print "%s : %20s : %s " % (ploc.tag, theStatus, ploc.label)
0149     return True
0150 
0151 
0152 def _CSteppingAction_cc_setStep(frame, bp_loc, sess):
0153     """ 
0154     """
0155     ploc = Loc(sys._getframe(), __name__)
0156     print "%s :  %s " % (ploc.tag, ploc.label)
0157     self = EV(frame.FindVariable("this"))
0158     return False
0159 
0160 
0161 def _CRec_cc_add(frame, bp_loc, sess):
0162     """ 
0163     
0164     """
0165     ploc = Loc(sys._getframe(), __name__)
0166 
0167     self = EV(frame.FindVariable("this"))
0168     bst = self.ev("m_boundary_status")
0169     pri = self.ev("m_prior_boundary_status")
0170     print "%s : bst:%20s pri:%20s : %s " % (ploc.tag, bst, pri, ploc.label)
0171     return False
0172 
0173 
0174 
0175 
0176 class CRandomEngine(EV):
0177     """
0178     """
0179     def __init__(self):
0180         EV.__init__(self, None)
0181 
0182     def preTrack(self, ploc, frame, bp_loc, sess):
0183         self.v = frame.FindVariable("this")
0184         pindex = self.ev("m_curand_index") 
0185         assert type(pindex) is int 
0186 
0187         ucf = UCF( pindex )
0188         lucf = len(ucf) 
0189         
0190         self.bounce_log = BounceLog( pindex )
0191          
0192         self.ucf = ucf 
0193         self.lucf = lucf
0194         self.pindex = pindex
0195         print "%s lucf:%d pindex:%d" % (ploc.tag, lucf, pindex ) 
0196         return False
0197 
0198     def postTrack(self, ploc, frame, bp_loc, sess):
0199         print ploc.hdr
0200         self.v = frame.FindVariable("this")
0201         pindex = self.ev("m_curand_index") 
0202         assert pindex == self.pindex
0203         print "%s pindex:%d" % (ploc.tag, pindex ) 
0204         return False
0205 
0206     def jump(self, ploc, frame, bp_loc, sess):
0207         self.v = frame.FindVariable("this")
0208 
0209         cursor_old = self.ev("m_cursor_old") 
0210         jump_ = self.ev("m_jump") 
0211         jump_count = self.ev("m_jump_count") 
0212         cursor = self.ev("m_cursor") 
0213 
0214         print "%s cursor_old:%d jump_:%d jump_count:%d cursor:%d " % (ploc.tag, cursor_old, jump_, jump_count, cursor ) 
0215         return False
0216 
0217     def postStep(self, ploc, frame, bp_loc, sess):
0218         self.v = frame.FindVariable("this")
0219 
0220         sid = self.ev(".m_ctx._step_id")
0221         pri = self.ev(".m_ctx._prior_boundary_status")
0222         bst = self.ev(".m_ctx._boundary_status")
0223         opt = self.ev("m_okevt_pt")
0224 
0225         print "%s step_id:%d bst:%15s pri:%15s okevt_pt:%s " % (ploc.tag, sid, bst, pri, opt ) 
0226         bounce = self.bounce_log.get(sid, None) 
0227         dump = False
0228         if bounce is not None and dump:
0229             print bounce
0230         pass
0231         print "--"  # postStep spacer 
0232         return False
0233 
0234     def flat(self, ploc, frame, bp_loc, sess):
0235         self.v = frame.FindVariable("this")
0236 
0237         ug4 = self.ev("m_flat") 
0238         lg4 = self.ev("m_location") 
0239         cur = self.ev("m_cursor") 
0240         crf = self.ev("m_current_record_flat_count") 
0241         csf = self.ev("m_current_step_flat_count") 
0242         cix = self.ev("m_curand_index") 
0243 
0244         idx = cur  
0245         # correspondence between sequences 
0246         #    crf: gets offset by the kludge
0247         #    cur: is always the real flat cursor index
0248 
0249         assert type(crf) is int 
0250         assert type(csf) is int 
0251         assert type(cix) is int and cix == self.pindex
0252 
0253         u = self.ucf[idx] if idx < self.lucf else None 
0254         uok = u.fval if u is not None else -1
0255         lok = u.lab  if u is not None else "ucf-overflow" 
0256 
0257         df = abs(ug4 - uok) 
0258         misrng = df > 1e-6 
0259         misloc = lok != lg4
0260         mrk = "%s%s" % ( "*" if misrng else "-", "#" if misloc else "-")
0261 
0262         f_ = lambda f:'{0:.9f}'.format(f)
0263         g_ = lambda g:'{0:.10g}'.format(g)
0264 
0265         print "%s cix:%5d mrk:%2s cur:%2d crf:%2d csf:%2d lg4/ok: ( %30s %30s ) df:%18s ug4/ok:( %s %s ) " % ( ploc.tag, cix, mrk, cur, crf, csf, lg4, lok, g_(df), f_(ug4), f_(uok)   )
0266         #print u 
0267 
0268         stop = mrk != "--"
0269         #return stop
0270         return False
0271 
0272 
0273 
0274 if __name__ == '__main__':
0275     print AutoBreakPoint(path=__file__, module="opticks.ana.g4lldb")
0276 
0277 
0278 
0279 
0280 
0281 
0282 
0283 
0284 
0285 
0286