1#!/usr/bin/env python3 2# Copyright 2017 gRPC authors. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# Reads stdin to find chttp2_refcount log lines, and prints reference leaks 17# to stdout 18 19import collections 20import re 21import sys 22 23 24def new_obj(): 25 return ["destroy"] 26 27 28outstanding = collections.defaultdict(new_obj) 29 30# Sample log line: 31# chttp2:unref:0x629000005200 2->1 destroy [src/core/ext/transport/chttp2/transport/chttp2_transport.c:599] 32 33for line in sys.stdin: 34 m = re.search( 35 r"chttp2:( ref|unref):0x([a-fA-F0-9]+) [^ ]+ ([^[]+) \[(.*)\]", line 36 ) 37 if m: 38 if m.group(1) == " ref": 39 outstanding[m.group(2)].append(m.group(3)) 40 else: 41 outstanding[m.group(2)].remove(m.group(3)) 42 43for obj, remaining in list(outstanding.items()): 44 if remaining: 45 print(("LEAKED: %s %r" % (obj, remaining))) 46