public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Simon Marchi <simark@simark.ca>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/3] gdb: improve disassembler styling when Pygments raises an exception
Date: Mon, 10 Oct 2022 12:03:15 +0100	[thread overview]
Message-ID: <87r0zgdjpo.fsf@redhat.com> (raw)
In-Reply-To: <15cf0c9f-81ae-9bc8-79ba-e5b4eb1f0412@simark.ca>

Simon Marchi <simark@simark.ca> writes:

>> +# Check that, if the user is using Python Pygments for disassembler
>> +# styling, then the styling correctly switches off when an error is
>> +# detected in the Python code.
>> +proc test_disassembler_error_handling { } {
>> +
>> +    # This test requires the Python Pygments module to be installed
>> +    # and used by GDB.
>> +    if { !$::python_disassembly_styling } {
>> +	return
>> +    }
>> +
>> +    save_vars { env(TERM) } {
>> +	# We need an ANSI-capable terminal to get the output.
>> +	setenv TERM ansi
>> +
>> +	# Restart GDB with the correct TERM variable setting, this
>> +	# means that GDB will enable styling.
>> +	clean_restart_and_disable "restart 4" $::binfile
>> +
>> +	# Disable use of libopcodes for styling.  As this function is
>> +	# only called when Python Pygments module is available, we
>> +	# should now be using that module to style the disassembler
>> +	# output.
>> +	gdb_test_no_output "maint set libopcodes-styling enabled off"
>> +
>> +	# Disassemble a single instruction and ensure that the output
>> +	# has styling markers in it.
>> +	set insn_before [get_single_disassembled_insn]
>> +	gdb_assert { [regexp "\033" $insn_before] } \
>> +	    "have style markers when Pygments is working fine"
>> +
>> +	# Now replace the standard function that colorizes the
>> +	# disassembler output, with a new function that always returns
>> +	# None, this should cause GDB to stop using the Pygments
>> +	# module for disassembler styling.
>> +	gdb_py_test_silent_cmd \
>> +	    [multi_line_input \
>> +		 "python" \
>> +		 "def replacement_colorize_disasm(content,gdbarch):" \
>> +		 "  return None" \
>> +		 "gdb.styling.colorize_disasm = replacement_colorize_disasm" \
>> +		 "\004"] \
>
> Any reason you are using \004 here, instead of end?  I don't quite
> understand why, but it seems to cause some random failures.  Running the
> test under `taskset -c 2` makes it fail most of the time.  Running it
> with check-read1 makes it fail consistently:
>
>   FAIL: gdb.base/style.exp: capture_command_output for x/1i *main
>
> When changing \004 for end, it passes.  I don't have an explanation why
> though.

Turns out the explanation is really simple once you know what it is :)

gdb_py_test_silent_cmd forwards to gdb_test_multiple.  The command I was
sending above, once multi_line_input has done its thing is like this:

  python\ndef ...\n  return None\ngdb.styling....\n\004

This gets sent to gdb_test_multiple, which then sends the command to
GDB, along with a trailing newline - the newline which it thinks will
cause the command to dispatch to GDB.  So our command ends like this:

   ....\n\004\n

Of course, a \004 doesn't need a trailing newline, it is handled
immediately, so that final '\n' is not part of the multi-line python
block.

And so, after \004 has cause the Python block to finish, and the
contents to be processed by the Python interpreter, we still have an
unspent newline character in the input stream, this causes GDB to print
a second prompt, the gdb.log contains:

  (gdb) PASS: gdb.base/style.exp: have style markers when Pygments is working fine
  python
  >def replacement_colorize_disasm(content,gdbarch):
  >  return None
  >gdb.styling.colorize_disasm = replacement_colorize_disasm
  >quit
  (gdb) PASS: gdb.base/style.exp: setup replacement colorize_disasm function
  
  (gdb) FAIL: gdb.base/style.exp: capture_command_output for x/1i *main

And so, we have a race with DejaGnu.  If the two prompts appear slowly
enough, then DejaGnu will match the first one with the end of the 'setup
replacement colorize_disasm function', and the second prompt will be
used to prematurely match with the end of the 'capture_command_output
for x/1i *main' test.

If the two prompts appear quickly then both prompts will be matched as
part of the 'setup replacement colorize_disasm function' test.

The patch I already pushed will resolve this issue completely.

Thanks for finding this bug.

Andrew


  parent reply	other threads:[~2022-10-10 11:03 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-30 14:16 [PATCH 0/3] Improvements for Pygments based disassembly styling Andrew Burgess
2022-08-30 14:16 ` [PATCH 1/3] gdb/testsuite: extend styling test for libopcodes styling Andrew Burgess
2022-08-30 14:16 ` [PATCH 2/3] gdb: improve disassembler styling when Pygments raises an exception Andrew Burgess
2022-10-08  2:25   ` Simon Marchi
2022-10-08 16:00     ` Andrew Burgess
2022-10-08 16:01       ` Simon Marchi
2022-10-10  9:32         ` Andrew Burgess
2022-10-10 10:31           ` Tom de Vries
2022-10-10 11:16             ` Andrew Burgess
2022-10-10 13:22               ` Tom de Vries
2022-10-10 14:04                 ` Andrew Burgess
2022-10-10 11:03     ` Andrew Burgess [this message]
2022-08-30 14:16 ` [PATCH 3/3] gdb/disasm: better intel flavour disassembly styling with Pygments Andrew Burgess
2022-10-02 16:38 ` [PATCH 0/3] Improvements for Pygments based disassembly styling Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0zgdjpo.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simark@simark.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).