• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2014 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Simple mock for addr2line.
7
8Outputs mock symbol information, with each symbol being a function of the
9original address (so it is easy to double-check consistency in unittests).
10"""
11
12
13import optparse
14import os
15import posixpath
16import sys
17import time
18
19
20def main(argv):
21  parser = optparse.OptionParser()
22  parser.add_option('-e', '--exe', dest='exe')  # Path of the debug-library.so.
23  # Silently swallow the other unnecessary arguments.
24  parser.add_option('-C', '--demangle', action='store_true')
25  parser.add_option('-f', '--functions', action='store_true')
26  parser.add_option('-i', '--inlines', action='store_true')
27  options, _ = parser.parse_args(argv[1:])
28  lib_file_name = posixpath.basename(options.exe)
29  processed_sym_count = 0
30  crash_every = int(os.environ.get('MOCK_A2L_CRASH_EVERY', 0))
31  hang_every = int(os.environ.get('MOCK_A2L_HANG_EVERY', 0))
32
33  while(True):
34    line = sys.stdin.readline().rstrip('\r')
35    if not line:
36      break
37
38    # An empty line should generate '??,??:0' (is used as marker for inlines).
39    if line == '\n':
40      print('??')
41      print('??:0')
42      sys.stdout.flush()
43      continue
44
45    addr = int(line, 16)
46    processed_sym_count += 1
47    if crash_every and processed_sym_count % crash_every == 0:
48      sys.exit(1)
49    if hang_every and processed_sym_count % hang_every == 0:
50      time.sleep(1)
51
52    # Addresses < 1M will return good mock symbol information.
53    if addr < 1024 * 1024:
54      print('mock_sym_for_addr_%d' % addr)
55      print('mock_src/%s.c:%d' % (lib_file_name, addr))
56
57    # Addresses 1M <= x < 2M will return symbols with a name but a missing path.
58    elif addr < 2 * 1024 * 1024:
59      print('mock_sym_for_addr_%d' % addr)
60      print('??:0')
61
62    # Addresses 2M <= x < 3M will return unknown symbol information.
63    elif addr < 3 * 1024 * 1024:
64      print('??')
65      print('??')
66
67    # Addresses 3M <= x < 4M will return inlines.
68    elif addr < 4 * 1024 * 1024:
69      print('mock_sym_for_addr_%d_inner' % addr)
70      print('mock_src/%s.c:%d' % (lib_file_name, addr))
71      print('mock_sym_for_addr_%d_middle' % addr)
72      print('mock_src/%s.c:%d' % (lib_file_name, addr))
73      print('mock_sym_for_addr_%d_outer' % addr)
74      print('mock_src/%s.c:%d' % (lib_file_name, addr))
75
76    sys.stdout.flush()
77
78
79if __name__ == '__main__':
80  main(sys.argv)