This is an archive of the discontinued LLVM Phabricator instance.

WIP: [lit] Enable debugging lit tests with python's pdb
Needs ReviewPublic

Authored by jdenny on Aug 27 2023, 8:42 AM.

Details

Summary

This patch enables using python's default debugger, pdb, to interactively step through and debug directives in lit tests, even existing tests that do *not* use PYTHON directives (proposed in D154987). For example, let's say we have a test llvm/test/example.txt:

$ LIT_OPTS=--pdb LIT_FILTER=example.txt ninja check-llvm
[0/1] Running the LLVM regression tests
-- Testing: 1 of 49248 tests, 0 workers --
******************** STARTED DEBUGGING 'LLVM :: example.txt' ********************
# started executing 'PYTHON:' directive from line 1 to 8

# At the pdb prompt, you can type:
#
# - 'q', press enter, and then immediately hit Ctrl-C to terminate lit.
# - '!help(lit)' for help with the 'lit' object.
# - 'help' for help with other pdb commands.
#
# All RUN, DEFINE, and REDEFINE directives are automatically converted to
# equivalent PYTHON directives for execution by the debugger.

> /home/jdenny/llvm-project/llvm/test/example.txt(1)<module>()
-> ; DEFINE: %{msg} =
(Pdb) list
  1  -> ; DEFINE: %{msg} =
  2     ; DEFINE: %{greet} = echo %{msg}
  3
  4     ; REDEFINE: %{msg} = hello world
  5     ; RUN: %{greet}
  6
  7     ; REDEFINE: %{msg} = goodby world
  8     ; RUN: %{greet}
[EOF]
(Pdb) next
> /home/jdenny/llvm-project/llvm/test/example.txt(2)<module>()
-> ; DEFINE: %{greet} = echo %{msg}
(Pdb) next
> /home/jdenny/llvm-project/llvm/test/example.txt(4)<module>()
-> ; REDEFINE: %{msg} = hello world
(Pdb) next
> /home/jdenny/llvm-project/llvm/test/example.txt(5)<module>()
-> ; RUN: %{greet}
(Pdb) p lit.expand("%{greet}")
'echo hello world'
(Pdb) next
# lit.run called from /home/jdenny/llvm-project/llvm/test/example.txt:5
#         called from /usr/lib/python3.8/bdb.py:587
#         called from /usr/lib/python3.8/pdb.py:1595
echo hello world
# executed command: echo hello world
# .---command stdout------------
# | hello world
# `-----------------------------
> /home/jdenny/llvm-project/llvm/test/example.txt(7)<module>()
-> ; REDEFINE: %{msg} = goodby world
(Pdb) next
> /home/jdenny/llvm-project/llvm/test/example.txt(8)<module>()
-> ; RUN: %{greet}
(Pdb) p lit.expand("%{greet}")
'echo goodby world'
(Pdb) p lit.expand("%{msg}")
'goodby world'
(Pdb) !lit.redefine("%{msg}", "goodbye world")
(Pdb) p lit.expand("%{greet}")
'echo goodbye world'
(Pdb) next
# lit.run called from /home/jdenny/llvm-project/llvm/test/example.txt:8
#         called from /usr/lib/python3.8/bdb.py:587
#         called from /usr/lib/python3.8/pdb.py:1595
echo goodbye world
# executed command: echo goodbye world
# .---command stdout------------
# | goodbye world
# `-----------------------------
--Return--
> /home/jdenny/llvm-project/llvm/test/example.txt(8)<module>()->None
-> ; RUN: %{greet}
(Pdb) next
# finished executing 'PYTHON:' directive from line 1 to 8
******************** FINISHED DEBUGGING 'LLVM :: example.txt' ********************

Testing Time: 87.06s
  Excluded: 48678
  Passed  :     1

A fundamental component of this patch is that, when lit is passed the --pdb command-line option, lit treats RUN, DEFINE, and REDEFINE directives as if they were PYTHON directives with corresponding calls to the lit object. That's why lit claims it's executing a PYTHON directive. Of course, --pdb also works fine for tests that actually use PYTHON directives or config.prologue.

(For the above example, I configured the test suite to use lit's internal shell in order to improve the execution trace, but I have found bash works fine here too.)

This patch also introduces lit.define(name, value) and lit.redefine(name, value) functions in order to support DEFINE and REDEFINE directives. Those are likely useful on their own, and I'll probably extract them into a parent patch later and improve them.

This patch is a WIP and is not ready for a detailed review. First, I still need to extend the documentation and lit's test suite. Second, I wonder if the patch should be generalized to work with other python debuggers besides pdb, but I haven't given that idea enough thought yet. I am sharing this patch early to demonstrate another benefit of extending lit with PYTHON directives, as proposed in D154987.

Diff Detail

Event Timeline

jdenny created this revision.Aug 27 2023, 8:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2023, 8:42 AM
Herald added a subscriber: delcypher. · View Herald Transcript
jdenny requested review of this revision.Aug 27 2023, 8:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2023, 8:42 AM
jdenny updated this revision to Diff 556912.Sep 17 2023, 7:28 AM
jdenny edited the summary of this revision. (Show Details)

Rebased onto the current D154987. See its summary for how to apply it successfully first.

Added a brief help message before the pdb prompt, and updated example in commit log.

Applied python formatting suggestions from darker.

jdenny updated this revision to Diff 557641.Oct 7 2023, 1:59 PM
jdenny edited the summary of this revision. (Show Details)

Rebased onto latest D154987. Improved help message a little.