gdb_pretty_printer_test.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import gdb
  2. import re
  3. import sys
  4. # this test should test the gdb pretty printers of the nim
  5. # library. But be aware this test is not complete. It only tests the
  6. # command line version of gdb. It does not test anything for the
  7. # machine interface of gdb. This means if if this test passes gdb
  8. # frontends might still be broken.
  9. gdb.execute("set python print-stack full")
  10. gdb.execute("source ../../../tools/debug/nim-gdb.py")
  11. # debug all instances of the generic function `myDebug`, should be 14
  12. gdb.execute("rbreak myDebug")
  13. gdb.execute("run")
  14. outputs = [
  15. 'meTwo',
  16. '""',
  17. '"meTwo"',
  18. '{meOne, meThree}',
  19. 'MyOtherEnum(1)',
  20. '{MyOtherEnum(0), MyOtherEnum(2)}',
  21. 'array = {1, 2, 3, 4, 5}',
  22. 'seq(0, 0)',
  23. 'seq(0, 10)',
  24. 'array = {"one", "two"}',
  25. 'seq(3, 3) = {1, 2, 3}',
  26. 'seq(3, 3) = {"one", "two", "three"}',
  27. 'Table(3, 64) = {[4] = "four", [5] = "five", [6] = "six"}',
  28. 'Table(3, 8) = {["two"] = 2, ["three"] = 3, ["one"] = 1}',
  29. '{a = 1, b = "some string"}',
  30. '("hello", 42)'
  31. ]
  32. argRegex = re.compile("^.* = (?:No suitable Nim \$ operator found for type: \w+\s*)*(.*)$")
  33. # Remove this error message which can pop up
  34. noSuitableRegex = re.compile("(No suitable Nim \$ operator found for type: \w+\s*)")
  35. for i, expected in enumerate(outputs):
  36. gdb.write(f"\x1b[38;5;105m{i+1}) expecting: {expected}: \x1b[0m", gdb.STDLOG)
  37. gdb.flush()
  38. currFrame = gdb.selected_frame()
  39. functionSymbol = currFrame.block().function
  40. assert functionSymbol.line == 24, str(functionSymbol.line)
  41. raw = ""
  42. if i == 6:
  43. # myArray is passed as pointer to int to myDebug. I look up myArray up in the stack
  44. gdb.execute("up")
  45. raw = gdb.parse_and_eval("myArray")
  46. elif i == 9:
  47. # myOtherArray is passed as pointer to int to myDebug. I look up myOtherArray up in the stack
  48. gdb.execute("up")
  49. raw = gdb.parse_and_eval("myOtherArray")
  50. else:
  51. rawArg = re.sub(noSuitableRegex, "", gdb.execute("info args", to_string = True))
  52. raw = rawArg.split("=", 1)[-1].strip()
  53. output = str(raw)
  54. if output != expected:
  55. gdb.write(f"\x1b[38;5;196m ({output}) != expected: ({expected})\x1b[0m\n", gdb.STDERR)
  56. gdb.execute("quit 1")
  57. else:
  58. gdb.write("\x1b[38;5;34mpassed\x1b[0m\n", gdb.STDLOG)
  59. gdb.execute("continue")