Adding a new --python-function (-F) option to breakpoint command add. The option allows the user to specify a Python function name instead of a Python oneliner or interactive script input as a breakpoint command

git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@154026 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandObjectBreakpointCommand.cpp b/source/Commands/CommandObjectBreakpointCommand.cpp
index 5ab93d7..dcc9f9b 100644
--- a/source/Commands/CommandObjectBreakpointCommand.cpp
+++ b/source/Commands/CommandObjectBreakpointCommand.cpp
@@ -37,7 +37,8 @@
     m_use_script_language (false),
     m_script_language (eScriptLanguageNone),
     m_use_one_liner (false),
-    m_one_liner()
+    m_one_liner(),
+    m_function_name()
 {
 }
 
@@ -60,7 +61,7 @@
 OptionDefinition
 CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
 {
-    { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
+    { LLDB_OPT_SET_1, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
         "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
 
     { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, NULL, eArgTypeBoolean,
@@ -69,6 +70,9 @@
     { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, NULL, eArgTypeNone,
         "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
 
+    { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, NULL, eArgTypePythonFunction,
+        "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
+    
     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
 };
 
@@ -104,12 +108,10 @@
 
         if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
         {
-            m_use_commands = false;
             m_use_script_language = true;
         }
         else
         {
-            m_use_commands = true;
             m_use_script_language = false;
         }          
         break;
@@ -122,6 +124,14 @@
                 error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
         }
         break;
+            
+    case 'F':
+        {
+            m_use_one_liner = false;
+            m_use_script_language = true;
+            m_function_name.assign(option_arg);
+        }
+        break;
 
     default:
         break;
@@ -139,6 +149,7 @@
     m_use_one_liner = false;
     m_stop_on_error = true;
     m_one_liner.clear();
+    m_function_name.clear();
 }
 
 //-------------------------------------------------------------------------
@@ -191,7 +202,8 @@
 want to access global variables in the 'loose' code, you need to \n\
 specify that they are global, using the 'global' keyword.  Be sure to \n\
 use correct Python syntax, including indentation, when entering Python \n\
-breakpoint commands. \n\
+breakpoint commands. \nAs a third option, you can pass the name of an already \
+existing Python function and that function will be attached to the breakpoint. \n\
  \n\
 Example Python one-line breakpoint command: \n\
  \n\
@@ -304,6 +316,13 @@
         return false;
     }
 
+    if (m_options.m_use_script_language == false && m_options.m_function_name.size())
+    {
+        result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
+        result.SetStatus (eReturnStatusFailed);
+        return false;
+    }
+    
     BreakpointIDList valid_bp_ids;
     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
 
@@ -341,11 +360,25 @@
                 {
                     // Special handling for one-liner specified inline.
                     if (m_options.m_use_one_liner)
+                    {
                         m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
                                                                                             m_options.m_one_liner.c_str());
+                    }
+                    // Special handling for using a Python function by name
+                    // instead of extending the breakpoint callback data structures, we just automatize
+                    // what the user would do manually: make their breakpoint command be a function call
+                    else if (m_options.m_function_name.size())
+                    {
+                        std::string oneliner(m_options.m_function_name);
+                        oneliner += "(frame, bp_loc, dict)";
+                        m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
+                                                                                            oneliner.c_str());
+                    }
                     else
+                    {
                         m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
                                                                                                        result);
+                    }
                 }
                 else
                 {