ldd2dotlib.dot 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. digraph g {
  2. graph [ranksep=0.2 nodesep=0.1];
  3. "/lib/ld-linux.so.2" [shape="Mrecord" label="{/lib|ld-linux.so.2}"];
  4. "/lib32/libcrypt.so.1" [shape="Mrecord" label="{/lib32|libcrypt.so.1}"];
  5. "/lib/i386-linux-gnu/libc.so.6" [shape="Mrecord" label="{/lib/i386-linux-gnu|libc.so.6}"];
  6. "/lib32/libcrypt.so.1" -> "/lib/i386-linux-gnu/libc.so.6" [color="steelblue" style=dotted arrowsize=0.5];
  7. "/lib/i386-linux-gnu/libc.so.6" -> "/lib/ld-linux.so.2" [color="steelblue" style=dotted arrowsize=0.5];
  8. "/lib32/libcrypt.so.1" -> "/lib/ld-linux.so.2" [color="steelblue" style=dotted arrowsize=0.5];
  9. }
  10. @
  11. # make a graph of the ldd info as dot graph, example:
  12. # python ldd2dot.py /lib64/libcups.so.2 > libcups.dot
  13. import os
  14. import re
  15. import sys
  16. from subprocess import Popen, PIPE
  17. MAXDEPTH = 10
  18. def ldd(depth, tl, nodes, deps):
  19. if depth >= MAXDEPTH:
  20. return
  21. proc = Popen(['ldd', tl], stdout=PIPE)
  22. stdo, stde = proc.communicate()
  23. for line in stdo.split("\n"):
  24. m1 = re.search(r"[ \t]+(.*)[ \t]+=>[ \t]*(.*)[ \t]+\(.*\)", line)
  25. m2 = re.search(r"[ \t]+(.*)[ \t]+\(.*\)", line)
  26. s = ""
  27. if m1:
  28. if m1.group(2):
  29. s = m1.group(2)
  30. else:
  31. s = m1.group(1)
  32. elif m2:
  33. s = m2.group(1)
  34. item = (tl, s)
  35. if s.startswith("/"):
  36. if s not in nodes:
  37. nodes.add(s)
  38. if item not in deps:
  39. deps.append(item)
  40. ldd(depth + 1, s, nodes, deps)
  41. tl = sys.argv[1]
  42. nodes = set([tl])
  43. deps = []
  44. ldd(1, tl, nodes, deps)
  45. print("""\
  46. digraph g {
  47. graph [ranksep=0.2 nodesep=0.1];
  48. """)
  49. for node in nodes:
  50. print(' "%s" [shape="Mrecord" label="{%s|%s}"];' % (
  51. node, os.path.dirname(node), os.path.basename(node)))
  52. for tl, ch in deps:
  53. print(
  54. ' "%s" -> "%s" '
  55. '[color="steelblue" style=dotted arrowsize=0.5];' % (
  56. tl, ch))
  57. print("}")