[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-25 Thread Don Hinton via Phabricator via cfe-commits
hintonda updated this revision to Diff 120308.
hintonda added a comment.

Reimplement at a python module.


https://reviews.llvm.org/D36347

Files:
  utils/clangdiag.py

Index: utils/clangdiag.py
===
--- /dev/null
+++ utils/clangdiag.py
@@ -0,0 +1,124 @@
+#!/usr/bin/python
+
+#--
+# Be sure to add the python path that points to the LLDB shared library.
+#
+# # To use this in the embedded python interpreter using "lldb" just
+# import it with the full path using the "command script import"
+# command
+#   (lldb) command script import /path/to/clandiag.py
+#--
+
+import lldb
+import argparse
+import commands
+import shlex
+import os
+from subprocess import check_output as qx;
+
+# State
+Breakpoints = []
+target = None
+diagtool = None
+
+class MyParser(argparse.ArgumentParser):
+ def format_help(self):
+ return ''' Commands for operating on clang diagnostic breakpoints
+
+Syntax: clangdiag 
+
+The following subcommands are supported:
+
+  enable  -- Enable clang diagnostic breakpoints.
+  disable -- Disable all clang diagnostic breakpoints. 
+'''
+
+def create_diag_options():
+parser = MyParser(prog='clangdiag')
+subparsers = parser.add_subparsers(
+title='subcommands',
+dest='subcommands',
+metavar='')
+disable_parser = subparsers.add_parser('disable')
+enable_parser = subparsers.add_parser('enable')
+return parser
+
+def setDiagBreakpoint(frame, bp_loc, dict):
+id = frame.FindVariable("DiagID").GetValue()
+if id is None:
+print('id is None')
+return False
+
+global target
+global diagtool
+global Breakpoints
+
+name = qx([diagtool, "find-diagnostic-id", id]).rstrip();
+bp = target.BreakpointCreateBySourceRegex(name, lldb.SBFileSpec())
+Breakpoints.append(bp)
+
+return False
+
+def enable(debugger, args):
+# Always disable existing breakpoints
+disable(debugger)
+
+global target
+global diagtool
+global Breakpoints
+
+target = debugger.GetSelectedTarget()
+exe = target.GetExecutable()
+if not exe.Exists():
+print('Target (%s) not set.' % exe.GetFilename())
+return
+diagtool = os.path.join(exe.GetDirectory(), 'diagtool')
+if not os.path.exists(diagtool):
+print('diagtool not found along side %s' % exe)
+return
+
+bp = target.BreakpointCreateByName('DiagnosticsEngine::Report')
+bp.SetScriptCallbackFunction('clangdiag.setDiagBreakpoint')
+Breakpoints.append(bp)
+
+return
+
+def disable(debugger):
+global target
+global diagtool
+global Breakpoints
+# Remove all diag breakpoints.
+for bp in Breakpoints:
+target.BreakpointDelete(bp.GetID())
+target = None
+diagtool = None
+Breakpoints = []
+return
+
+def the_diag_command(debugger, command, result, dict):
+# Use the Shell Lexer to properly parse up command options just like a
+# shell would
+command_args = shlex.split(command)
+parser = create_diag_options()
+try:
+args = parser.parse_args(command_args)
+except:
+return
+
+if args.subcommands == 'enable':
+enable(debugger, args)
+else:
+disable(debugger)
+
+return
+
+def __lldb_init_module(debugger, dict):
+# This initializer is being run from LLDB in the embedded command interpreter
+# Make the options so we can generate the help text for the new LLDB
+# command line command prior to registering it with LLDB below
+parser = create_diag_options()
+the_diag_command.__doc__ = parser.format_help()
+# Add any commands contained in this module to LLDB
+debugger.HandleCommand(
+'command script add -f clangdiag.the_diag_command clangdiag')
+print 'The "clangdiag" command has been installed, type "help clangdiag" or "clangdiag --help" for detailed help.'
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-23 Thread Don Hinton via Phabricator via cfe-commits
hintonda added a comment.

Thanks for all the feedback.  I'll report back once I've addressed all your 
suggestions.

Thanks again...


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Jason Molenda via Phabricator via cfe-commits
jasonmolenda added a comment.

Sorry for missing this back in August.

I think it'd be clearer to import your python once in the startup, like

-o "script import $module" \

Multiple imports are a no-op IIUC so it's harmless to re-import the module 
every time the breakpoint is hit (I'm guessing it's only hit once, for that 
matter), but I think it's clearer to have this on its own line.

For what it's worth, if you use breakpoint command add -F python-function, your 
function is passed in the frame as well as the SBBreakpointLocation.  See 'help 
breakpoint add' for more information about this; I used this in a breakpoint 
python command I wrote a while back,

def handle_pthread_create_return(frame, bp_loc, dict):

  global pthreads_being_created
  
  thread_index_id = frame.GetThread().GetIndexID()

[...]

it might be a good idea to name the breakpoint you're adding and then you can 
delete it explicitly, in case the user is doing something unexpected with 
breakpoints.  e.g.

% ~/k/svn/lldb/build/Debug/lldb /tmp/a.out
(lldb) target create "/tmp/a.out"
Current executable set to '/tmp/a.out' (x86_64).
(lldb) br s -n main -N worker
Breakpoint 1: where = a.out`main, address = 0x00010ec0
(lldb) br del worker
1 breakpoints deleted; 0 breakpoint locations disabled.
(lldb) br li
No breakpoints currently set.
(lldb)


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Jim Ingham via Phabricator via cfe-commits
jingham added a comment.

I can't see anything wrong with the SB API use here.  I don't feel qualified to 
comment on the most effective workflow for an analysis I've never had to do, 
however.


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Greg Clayton via Phabricator via cfe-commits
clayborg added a comment.

If you want to run the script from the command line, then it is necessary. If 
it is run from within LLDB it will just work. I like to have my LLDB python 
scripts work both ways.

This might be better implemented as a new command that gets installed and can 
be used within LLDB. See:

http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/cmdtemplate.py


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Zachary Turner via Phabricator via cfe-commits
zturner added a comment.

In https://reviews.llvm.org/D36347#902157, @clayborg wrote:

> Please do convert to python. Just know that you can use "lldb -P" to get the 
> python path that is needed in order to do "import lldb" in the python script. 
> So you can try doing a "import lldb", and if that fails, catch the exception, 
> run "lldb -P", add that path to the python path:
>
>   try:
>   # Just try for LLDB in case the lldb module is already in the python 
> search
>   # paths
>   import lldb
>   except ImportError:
>   # We failed to import lldb automatically. Run lldb with the -P option so
>   # it tells us the python path we should use.
>   lldb_py_dirs = list()
>   (status, output) = commands.getstatusoutput("lldb -P")
>   dir = os.path.realpath(output)
>   if status == 0 and os.path.isdir(dir):
>   lldb_py_dirs.append(dir)
>   success = False
>   for lldb_py_dir in lldb_py_dirs:
>   if os.path.exists(lldb_py_dir):
>   if not (sys.path.__contains__(lldb_py_dir)):
>   sys.path.append(lldb_py_dir)
>   try:
>   import lldb
>   except ImportError:
>   pass
>   else:
>   success = True
>   break
>   if not success:
>   print("error: couldn't locate the 'lldb' module, please set "
> "PYTHONPATH correctly")
>   sys.exit(1)
>  
>


Is any of this really necessary?  If you load this script via `command script 
add` (which is definitely better than having this be a post-processing script 
that someone has to manually run) then it is guaranteed to be in the path.  
Just import it, like the other examples in `lldb/examples/python/jump.py` for 
an example.  The idea is to have it do the indexing when the command is loaded 
and save it to a global, and then each time it runs it uses the global index.  
This way it's invisible to the user, you just run `bcd -Wcovered-switch` or 
something without worrying about it.


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Greg Clayton via Phabricator via cfe-commits
clayborg added a comment.

Please do convert to python. Just know that you can use "lldb -P" to get the 
python path that is needed in order to do "import lldb" in the python script. 
So you can try doing a "import lldb", and if that fails, catch the exception, 
run "lldb -P", add that path to the python path:

  try:
  # Just try for LLDB in case the lldb module is already in the python 
search
  # paths
  import lldb
  except ImportError:
  # We failed to import lldb automatically. Run lldb with the -P option so
  # it tells us the python path we should use.
  lldb_py_dirs = list()
  (status, output) = commands.getstatusoutput("lldb -P")
  dir = os.path.realpath(output)
  if status == 0 and os.path.isdir(dir):
  lldb_py_dirs.append(dir)
  success = False
  for lldb_py_dir in lldb_py_dirs:
  if os.path.exists(lldb_py_dir):
  if not (sys.path.__contains__(lldb_py_dir)):
  sys.path.append(lldb_py_dir)
  try:
  import lldb
  except ImportError:
  pass
  else:
  success = True
  break
  if not success:
  print("error: couldn't locate the 'lldb' module, please set "
"PYTHONPATH correctly")
  sys.exit(1)


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Zachary Turner via cfe-commits
+jingham


On Fri, Oct 20, 2017 at 6:57 AM Don Hinton via Phabricator <
revi...@reviews.llvm.org> wrote:

> hintonda added a comment.
>
> In https://reviews.llvm.org/D36347#901885, @zturner wrote:
>
> > One possible reason for why this never got any traction is that
> `lldb-commits` wasn't added as a subscriber.  While it's true that the
> tagged people should have chimed in, having the whole commits list will get
> some more visibility.  I never saw this come to my inbox.
> >
> > I think this would be most suitable in the `lldb/examples` folder.
> >
> > I can't really review this thoroughly, because it relies on a bash
> script, and I use Windows where we bash isn't really a thing.  My bash is
> rusty, but it looks like you're embedding a python script in the bash
> script?  It might be good if this were just an lldb script command.  Take a
> look at `command script add` in LLDB, and in the `examples` folder for some
> examples of existing commands that work this way.  The nice thing about
> doing it this way is that you could just be inside LLDB and write `(lldb)
> break-diag -Wcovered-switch`, for example, which would be a much tighter
> integration with the debugger.
>
>
> Thanks for taking a look.
>
> I mainly work on *nix systems, hence the initial shell script, but if
> there's an interest, I'll be happy to convert it to a single python script
> as you suggest, and resubmit it as a patch to lldb.
>
> Thanks again...
>
>
> https://reviews.llvm.org/D36347
>
>
>
>
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-20 Thread Don Hinton via Phabricator via cfe-commits
hintonda added a comment.

In https://reviews.llvm.org/D36347#901885, @zturner wrote:

> One possible reason for why this never got any traction is that 
> `lldb-commits` wasn't added as a subscriber.  While it's true that the tagged 
> people should have chimed in, having the whole commits list will get some 
> more visibility.  I never saw this come to my inbox.
>
> I think this would be most suitable in the `lldb/examples` folder.
>
> I can't really review this thoroughly, because it relies on a bash script, 
> and I use Windows where we bash isn't really a thing.  My bash is rusty, but 
> it looks like you're embedding a python script in the bash script?  It might 
> be good if this were just an lldb script command.  Take a look at `command 
> script add` in LLDB, and in the `examples` folder for some examples of 
> existing commands that work this way.  The nice thing about doing it this way 
> is that you could just be inside LLDB and write `(lldb) break-diag 
> -Wcovered-switch`, for example, which would be a much tighter integration 
> with the debugger.


Thanks for taking a look.

I mainly work on *nix systems, hence the initial shell script, but if there's 
an interest, I'll be happy to convert it to a single python script as you 
suggest, and resubmit it as a patch to lldb.

Thanks again...


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-10-19 Thread Zachary Turner via Phabricator via cfe-commits
zturner added a subscriber: lldb-commits.
zturner added a comment.

One possible reason for why this never got any traction is that `lldb-commits` 
wasn't added as a subscriber.  While it's true that the tagged people should 
have chimed in, having the whole commits list will get some more visibility.  I 
never saw this come to my inbox.

I think this would be most suitable in the `lldb/examples` folder.

I can't really review this thoroughly, because it relies on a bash script, and 
I use Windows where we bash isn't really a thing.  My bash is rusty, but it 
looks like you're embedding a python script in the bash script?  It might be 
good if this were just an lldb script command.  Take a look at `command script 
add` in LLDB, and in the `examples` folder for some examples of existing 
commands that work this way.  The nice thing about doing it this way is that 
you could just be inside LLDB and write `(lldb) break-diag -Wcovered-switch`, 
for example, which would be a much tighter integration with the debugger.


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-09-04 Thread Don Hinton via Phabricator via cfe-commits
hintonda added a comment.

ping...


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-08-21 Thread don hinton via Phabricator via cfe-commits
hintonda added a comment.

ping...


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-08-06 Thread don hinton via Phabricator via cfe-commits
hintonda updated this revision to Diff 109949.
hintonda added a comment.

- Use temp files instead of temp dir.


https://reviews.llvm.org/D36347

Files:
  CMakeLists.txt
  utils/CMakeLists.txt
  utils/run_lldb.sh.in

Index: utils/run_lldb.sh.in
===
--- /dev/null
+++ utils/run_lldb.sh.in
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+#
+# This script runs the given command in lldb and sets breakpoints on all reported diagnostics.
+
+function usage() {
+  cat <
+EOF
+  exit 1
+}
+
+if [ $# -eq 0 ]; then
+  echo "Error: No arguments supplied"
+  usage
+fi
+
+cmd=""
+if [ "$2" == "-cc1" ]; then
+  cmd=`echo "$@" 2>&1 | sed -ne "s/\(.*\) -cc1/-f \1 -- -cc1/p"`
+else
+  cmd=`$@ -### 2>&1 | sed -ne "s/.*\"\(.*\)\" .*\-cc1\"/-f \1 -- -cc1/p"`
+fi
+
+tmpdir=/tmp/run_lldb
+test ! -d $tmpdir && mkdir $tmpdir
+
+indexfile=$(mktemp $tmpdir/diag.idx.XX)
+cd @CLANG_SOURCE_DIR@
+find lib include tools \( -name \*.cpp -or -name \*.h \) -exec grep -nHE 'diag::(err_|warn_|ext_)[a-z_]+' \{\} \; |\
+  sed -e 's/^\(.*:[0-9]*:\).*diag::\([a-z_]*\).*/\2:\1/' | \
+  sort > $indexfile
+cd - > /dev/null
+
+root=$(mktemp $tmpdir/diagXX) && rm $root
+module=$(basename $root)
+script=$root.py
+cat << EOF > $script
+import lldb
+import re
+from subprocess import check_output as qx;
+
+def setDiagBreakpoint(frame):
+  id = frame.FindVariable("DiagID").GetValue()
+  if id is None:
+return False
+
+  name = qx(["@CMAKE_RUNTIME_OUTPUT_DIRECTORY@/diagtool", "find-diagnostic-id", id]).rstrip();
+  target = frame.GetThread().GetProcess().GetTarget()
+
+  file = open("$indexfile", "r")
+  for line in file:
+if re.search(name, line):
+  i = filter(None, re.split(r'.*:.*/(\w.*):(\w.*):.*\n', line))
+  bp = target.BreakpointCreateByLocation(i[0], int(i[1]))
+  bp.SetEnabled(False)
+
+  return False
+EOF
+
+PYTHONPATH=$PYTHONPATH:$tmpdir lldb \
+  -o "breakpoint set -n DiagnosticsEngine::Report" \
+  -o "break command add -s p -o 'import $module; return $module.setDiagBreakpoint(frame)'" \
+  -o "run" \
+  -o "breakpoint delete -f 1" \
+  -o "breakpoint enable" \
+  -o "run" \
+  $cmd
+
+rm $indexfile
+rm $script
Index: utils/CMakeLists.txt
===
--- /dev/null
+++ utils/CMakeLists.txt
@@ -0,0 +1,17 @@
+
+set(FILENAME run_lldb.sh)
+# Configure to a temparary directory
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}.in
+  ${CMAKE_CURRENT_BINARY_DIR}/.tmp/${FILENAME}
+  @ONLY
+  )
+# Copy temparary file to real destination and set permissions/
+file(COPY
+  ${CMAKE_CURRENT_BINARY_DIR}/.tmp/${FILENAME}
+  DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
+  FILE_PERMISSIONS
+OWNER_READ OWNER_WRITE OWNER_EXECUTE
+GROUP_READ GROUP_EXECUTE
+WORLD_READ WORLD_EXECUTE
+)
Index: CMakeLists.txt
===
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -408,6 +408,8 @@
 
 add_subdirectory(utils/TableGen)
 
+add_subdirectory(utils)
+
 add_subdirectory(include)
 
 # All targets below may depend on all tablegen'd files.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D36347: Add new script to launch lldb and set breakpoints for diagnostics all diagnostics seen.

2017-08-04 Thread John McCall via Phabricator via cfe-commits
rjmccall edited reviewers, added: jasonmolenda, spyffe; removed: rjmccall.
rjmccall added a comment.

Since this is fundamentally an LLDB script, I've tagged a couple of LLDB people 
to review it.

Jason, Sean: the idea here is to make it easier for clang developers to debug 
unexpected diagnostics by automatically setting breakpoints on the places that 
originate the observed diagnostics.


https://reviews.llvm.org/D36347



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits