LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
citizen.py
Go to the documentation of this file.
1 import re
2 import lsst.daf.base as dafBase
3 
4 def setCallbacks(new=None, delete=None, both=False):
5  """Set the callback IDs for the Citizen; if both is true, set both new and delete to the same value
6 
7 You probably want to chant the following to gdb:
8  break defaultNewCallback
9  break defaultDeleteCallback
10 
11 You might want to put this in your .gdbinit file.
12 
13 You can retrieve a citizen's signature from python with obj.repr()
14  """
15 
16  if both:
17  if new:
18  if delete and new != delete:
19  raise RuntimeError, "You may not specify new, delete, and both"
20  delete = new
21  else:
22  new = delete
23 
24  if new:
25  dafBase.Citizen.setNewCallbackId(new)
26  if delete:
27  dafBase.Citizen.setDeleteCallbackId(delete)
28 
29 def mortal(memId0=0, nleakPrintMax=20, first=True, showTypes=None):
30  """Print leaked memory blocks
31  @param memId0 Only consider blocks allocated after this memId
32  @param nleakPrintMax Maximum number of leaks to print; <= 0 means unlimited
33  @param first Print the first nleakPrintMax blocks; if False print the last blocks.
34  @param showTypes Only print objects matching this regex (if starts with !, print objects that don't match)
35 
36  If you want finer control than nleakPrintMax/first provide, use
37  dafBase.Citizen.census() to get the entire list
38 
39 You can get the next memId to be allocated with mortal("set"), e.g.
40  memId0 = mortal("set")
41  # work work work
42  mortal(memId0)
43  """
44 
45  if memId0 == 'set':
46  return dafBase.Citizen.getNextMemId()
47 
48  nleak = dafBase.Citizen.census(0, memId0)
49  if nleak != 0:
50  print "%d Objects leaked" % dafBase.Citizen.census(0, memId0)
51 
52  census = dafBase.Citizen.census()
53  census = [census[i].repr() for i in range(len(census))] # using [i] for some swiggy reason
54  if showTypes:
55  if showTypes[0] == '!':
56  invert = True # invert the matching logic
57  showTypes = showTypes[1:]
58  else:
59  invert = False
60 
61  _census, census = census, []
62  for c in _census:
63  memId, addr, dtype = c.split()
64  memId = int(memId[:-1])
65 
66  if \
67  (not invert and re.search(showTypes, dtype)) or \
68  (invert and not re.search(showTypes, dtype)):
69  census.append(c)
70 
71  nleak = len(census)
72  print "%d leaked objects match" % nleak
73 
74  if nleakPrintMax <= 0 or nleak <= nleakPrintMax:
75  for c in census:
76  memId, addr, type = c.split()
77  memId = int(memId[:-1])
78  if memId >= memId0:
79  print c
80  else:
81  print "..."
82  for i in range(nleakPrintMax - 1, -1, -1):
83  print census[i]