lsst.daf.base  15.0-1-gf4f1c34+7
citizenContinued.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 from __future__ import absolute_import, division, print_function
25 
26 __all__ = ["setCallbacks", "mortal"]
27 
28 from builtins import range
29 import re
30 
31 from .citizen import Citizen
32 
33 
34 def setCallbacks(new=None, delete=None, both=False):
35  """Set the callback IDs for the Citizen; if both is true, set both new and delete to the same value
36 
37  You probably want to chant the following to gdb:
38  break defaultNewCallback
39  break defaultDeleteCallback
40 
41  You might want to put this in your .gdbinit file.
42 
43  You can retrieve a citizen's signature from python with obj.repr()
44  """
45 
46  if both:
47  if new:
48  if delete and new != delete:
49  raise RuntimeError("You may not specify new, delete, and both")
50  delete = new
51  else:
52  new = delete
53 
54  if new:
55  Citizen.setNewCallbackId(new)
56  if delete:
57  Citizen.setDeleteCallbackId(delete)
58 
59 
60 def mortal(memId0=0, nleakPrintMax=20, first=True, showTypes=None):
61  """!Print leaked memory blocks
62 
63  @param memId0 Only consider blocks allocated after this memId
64  @param nleakPrintMax Maximum number of leaks to print; <= 0 means unlimited
65  @param first Print the first nleakPrintMax blocks; if False print the last blocks.
66  @param showTypes Only print objects matching this regex (if starts with !, print objects that don't match)
67 
68  If you want finer control than nleakPrintMax/first provide, use
69  Citizen.census() to get the entire list
70 
71  You can get the next memId to be allocated with mortal("set"), e.g.
72  memId0 = mortal("set")
73  # work work work
74  mortal(memId0)
75  """
76  if memId0 == 'set':
77  return Citizen.getNextMemId()
78 
79  nleak = Citizen.census(0, memId0)
80  if nleak != 0:
81  print("%d Objects leaked" % Citizen.census(0, memId0))
82 
83  census = Citizen.census()
84  census = [census[i].repr() for i in range(len(census))] # using [i] for some swiggy reason
85  if showTypes:
86  if showTypes[0] == '!':
87  invert = True # invert the matching logic
88  showTypes = showTypes[1:]
89  else:
90  invert = False
91 
92  _census, census = census, []
93  for c in _census:
94  memId, addr, dtype = c.split()
95  memId = int(memId[:-1])
96 
97  if (not invert and re.search(showTypes, dtype)) or \
98  (invert and not re.search(showTypes, dtype)):
99  census.append(c)
100 
101  nleak = len(census)
102  print("%d leaked objects match" % nleak)
103 
104  if nleakPrintMax <= 0 or nleak <= nleakPrintMax:
105  for c in census:
106  memId, addr, type = c.split()
107  memId = int(memId[:-1])
108  if memId >= memId0:
109  print(c)
110  else:
111  print("...")
112  for i in range(nleakPrintMax - 1, -1, -1):
113  print(census[i])
def setCallbacks(new=None, delete=None, both=False)
def mortal(memId0=0, nleakPrintMax=20, first=True, showTypes=None)
Print leaked memory blocks.