Skip to content
Snippets Groups Projects
  1. Jan 26, 2023
    • Masahiro Yamada's avatar
      scripts: handle BrokenPipeError for python scripts · 87c7ee67
      Masahiro Yamada authored
      In the follow-up of commit fb3041d6 ("kbuild: fix SIGPIPE error
      message for AR=gcc-ar and AR=llvm-ar"), Kees Cook pointed out that
      tools should _not_ catch their own SIGPIPEs [1] [2].
      
      Based on his feedback, LLVM was fixed [3].
      
      However, Python's default behavior is to show noisy bracktrace when
      SIGPIPE is sent. So, scripts written in Python are basically in the
      same situation as the buggy llvm tools.
      
      Example:
      
        $ make -s allnoconfig
        $ make -s allmodconfig
        $ scripts/diffconfig .config.old .config | head -n1
        -ALIX n
        Traceback (most recent call last):
          File "/home/masahiro/linux/scripts/diffconfig", line 132, in <module>
            main()
          File "/home/masahiro/linux/scripts/diffconfig", line 130, in main
            print_config("+", config, None, b[config])
          File "/home/masahiro/linux/scripts/diffconfig", line 64, in print_config
            print("+%s %s" % (config, new_value))
        BrokenPipeError: [Errno 32] Broken pipe
      
      Python documentation [4] notes how to make scripts die immediately and
      silently:
      
        """
        Piping output of your program to tools like head(1) will cause a
        SIGPIPE signal to be sent to your process when the receiver of its
        standard output closes early. This results in an exception like
        BrokenPipeError: [Errno 32] Broken pipe. To handle this case,
        wrap your entry point to catch this exception as follows:
      
          import os
          import sys
      
          def main():
              try:
                  # simulate large output (your code replaces this loop)
                  for x in range(10000):
                      print("y")
                  # flush output here to force SIGPIPE to be triggered
                  # while inside this try block.
                  sys.stdout.flush()
              except BrokenPipeError:
                  # Python flushes standard streams on exit; redirect remaining output
                  # to devnull to avoid another BrokenPipeError at shutdown
                  devnull = os.open(os.devnull, os.O_WRONLY)
                  os.dup2(devnull, sys.stdout.fileno())
                  sys.exit(1)  # Python exits with error code 1 on EPIPE
      
          if __name__ == '__main__':
              main()
      
        Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid
        BrokenPipeError. Doing that would cause your program to exit
        unexpectedly whenever any socket connection is interrupted while
        your program is still writing to it.
        """
      
      Currently, tools/perf/scripts/python/intel-pt-events.py seems to be the
      only script that fixes the issue that way.
      
      tools/perf/scripts/python/compaction-times.py uses another approach
      signal.signal(signal.SIGPIPE, signal.SIG_DFL) but the Python
      documentation clearly says "Don't do it".
      
      I cannot fix all Python scripts since there are so many.
      I fixed some in the scripts/ directory.
      
      [1]: https://lore.kernel.org/all/202211161056.1B9611A@keescook/
      [2]: https://github.com/llvm/llvm-project/issues/59037
      [3]: https://github.com/llvm/llvm-project/commit/4787efa38066adb51e2c049499d25b3610c0877b
      [4]: https://docs.python.org/3/library/signal.html#note-on-sigpipe
      
      
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      87c7ee67
  2. Jan 22, 2023
    • Masahiro Yamada's avatar
      kbuild: rename cmd_$@ to savedcmd_$@ in *.cmd files · 92215e7a
      Masahiro Yamada authored
      
      The cmd-check macro compares $(cmd_$@) and $(cmd_$1), but a pitfall is
      that you cannot use cmd_<target> as the variable name for the command.
      
      For example, the following code will not work in the top Makefile
      or ./Kbuild.
      
          quiet_cmd_foo = GEN     $@
                cmd_foo = touch $@
      
          targets += foo
          foo: FORCE
                  $(call if_changed,foo)
      
      In this case, both $@ and $1 are expanded to 'foo', so $(cmd_check)
      is always empty.
      
      We do not need to use the same prefix for cmd_$@ and cmd_$1.
      Rename the former to savedcmd_$@.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      92215e7a
  3. Dec 14, 2022
  4. Oct 14, 2022
  5. Oct 02, 2022
  6. Sep 23, 2022
  7. Aug 20, 2022
  8. Jun 29, 2022
    • John Hubbard's avatar
      gen_compile_commands: handle multiple lines per .mod file · a4ab14e1
      John Hubbard authored
      
      scripts/clang-tools/gen_compile_commands.py incorrectly assumes that
      each .mod file only contains one line. That assumption was correct when
      the script was originally created, but commit 9413e764 ("kbuild:
      split the second line of *.mod into *.usyms") changed the .mod file
      format so that there is one entry per line, and potentially many lines.
      
      The problem can be reproduced by using Kbuild to generate
      compile_commands.json, like this:
      
          make CC=clang compile_commands.json
      
      In many cases, the problem might be overlooked because many subsystems
      only have one line anyway. However, in some subsystems (Nouveau, with
      762 entries, is a notable example) it results in skipping most of the
      subsystem.
      
      Fix this by fully processing each .mod file.
      
      Fixes: 9413e764 ("kbuild: split the second line of *.mod into *.usyms")
      Signed-off-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      a4ab14e1
  9. Sep 19, 2021
  10. Sep 03, 2021
  11. Feb 16, 2021
    • Masahiro Yamada's avatar
      gen_compile_commands: prune some directories · 585d32f9
      Masahiro Yamada authored
      
      If directories are passed to gen_compile_commands.py, os.walk() traverses
      all the subdirectories to search for .cmd files, but we know some of them
      are not worth traversing.
      
      Use the 'topdown' parameter of os.walk to prune them.
      
      Documentation about the 'topdown' option of os.walk:
        When topdown is True, the caller can modify the dirnames list
        in-place (perhaps using del or slice assignment), and walk() will
        only recurse into the subdirectories whose names remain in dirnames;
        this can be used to prune the search, impose a specific order of
        visiting, or even to inform walk() about directories the caller
        creates or renames before it resumes walk() again. Modifying
        dirnames when topdown is False has no effect on the behavior of
        the walk, because in bottom-up mode the directories in dirnames
        are generated before dirpath itself is generated.
      
      This commit prunes four directories, .git, Documentation, include, and
      tools.
      
      The first three do not contain any C files, so skipping them makes this
      script work slightly faster. My main motivation is the last one, tools/
      directory.
      
      Commit 6ca4c6d2 ("gen_compile_commands: do not support .cmd files
      under tools/ directory") stopped supporting the tools/ directory.
      The current code no longer picks up .cmd files from the tools/
      directory.
      
      If you run:
      
        ./scripts/clang-tools/gen_compile_commands.py --log_level=INFO
      
      then, you will see several "File ... not found" log messages.
      
      This is expected, and I do not want to support the tools/ directory.
      However, without an explicit comment "do not support tools/", somebody
      might try to get it back. Clarify this.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Acked-by: default avatarNathan Chancellor <nathan@kernel.org>
      585d32f9
  12. Feb 03, 2021
  13. Aug 26, 2020
Loading