[ VM / Debugger ] Use existing Function object to set breakpoints instead of finding "best fit" when possible.

Change-Id: I8bad37129f907d500a024acc8e17ad265b8b04b1
Reviewed-on: https://dart-review.googlesource.com/65793
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/observatory/tests/service/async_next_regession_18877_test.dart b/runtime/observatory/tests/service/async_next_regession_18877_test.dart
new file mode 100644
index 0000000..bd74274
--- /dev/null
+++ b/runtime/observatory/tests/service/async_next_regession_18877_test.dart
@@ -0,0 +1,51 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+// VMOptions=--error_on_bad_type --error_on_bad_override  --verbose_debug
+
+import 'dart:developer';
+import 'service_test_common.dart';
+import 'test_helper.dart';
+
+const int LINE_A = 23;
+const int LINE_B = 24;
+const int LINE_C = 25;
+
+foo() async {}
+
+doAsync(stop) async {
+  // Flutter issue 18877:
+  // If a closure is defined in the context of an async method, stepping over
+  // an await causes the implicit breakpoint to be set for that closure instead
+  // of the async_op, resulting in the debugger falling through.
+  final baz = () => print('doAsync($stop) done!');
+  if (stop) debugger();
+  await foo(); // Line A.
+  await foo(); // Line B.
+  await foo(); // Line C.
+  baz();
+  return null;
+}
+
+testMain() {
+  // With two runs of doAsync floating around, async step should only cause
+  // us to stop in the run we started in.
+  doAsync(false);
+  doAsync(true);
+}
+
+var tests = <IsolateTest>[
+  hasStoppedAtBreakpoint,
+  stoppedAtLine(LINE_A),
+  stepOver, // foo()
+  asyncNext,
+  hasStoppedAtBreakpoint,
+  stoppedAtLine(LINE_B),
+  stepOver, // foo()
+  asyncNext,
+  hasStoppedAtBreakpoint,
+  stoppedAtLine(LINE_C),
+  resumeIsolate,
+];
+
+main(args) => runIsolateTests(args, tests, testeeConcurrent: testMain);
diff --git a/runtime/vm/debugger.cc b/runtime/vm/debugger.cc
index bb87152..8db8d44 100644
--- a/runtime/vm/debugger.cc
+++ b/runtime/vm/debugger.cc
@@ -2860,11 +2860,17 @@
                                             TokenPosition token_pos,
                                             TokenPosition last_token_pos,
                                             intptr_t requested_line,
-                                            intptr_t requested_column) {
+                                            intptr_t requested_column,
+                                            const Function& function) {
   Function& func = Function::Handle();
-  if (!FindBestFit(script, token_pos, last_token_pos, &func)) {
-    return NULL;
+  if (function.IsNull()) {
+    if (!FindBestFit(script, token_pos, last_token_pos, &func)) {
+      return NULL;
+    }
+  } else {
+    func = function.raw();
   }
+
   if (!func.IsNull()) {
     // There may be more than one function object for a given function
     // in source code. There may be implicit closure functions, and
@@ -2971,7 +2977,7 @@
   const Script& script = Script::Handle(target_function.script());
   BreakpointLocation* bpt_location = SetBreakpoint(
       script, target_function.token_pos(), target_function.end_token_pos(), -1,
-      -1 /* no requested line/col */);
+      -1 /* no requested line/col */, target_function);
   if (bpt_location == NULL) {
     return NULL;
   }
@@ -2990,8 +2996,9 @@
   }
   const Function& func = Function::Handle(Closure::Cast(closure).function());
   const Script& script = Script::Handle(func.script());
-  BreakpointLocation* bpt_location = SetBreakpoint(
-      script, func.token_pos(), func.end_token_pos(), -1, -1 /* no line/col */);
+  BreakpointLocation* bpt_location =
+      SetBreakpoint(script, func.token_pos(), func.end_token_pos(), -1,
+                    -1 /* no line/col */, func);
   return bpt_location->AddPerClosure(this, closure, for_over_await);
 }
 
@@ -3108,7 +3115,7 @@
   ASSERT(first_token_idx <= last_token_idx);
   while ((bpt == NULL) && (first_token_idx <= last_token_idx)) {
     bpt = SetBreakpoint(script, first_token_idx, last_token_idx, line_number,
-                        column_number);
+                        column_number, Function::Handle());
     first_token_idx.Next();
   }
   if ((bpt == NULL) && FLAG_verbose_debug) {
diff --git a/runtime/vm/debugger.h b/runtime/vm/debugger.h
index fedfed1..2128046 100644
--- a/runtime/vm/debugger.h
+++ b/runtime/vm/debugger.h
@@ -664,7 +664,8 @@
                                     TokenPosition token_pos,
                                     TokenPosition last_token_pos,
                                     intptr_t requested_line,
-                                    intptr_t requested_column);
+                                    intptr_t requested_column,
+                                    const Function& function);
   bool RemoveBreakpointFromTheList(intptr_t bp_id, BreakpointLocation** list);
   Breakpoint* GetBreakpointByIdInTheList(intptr_t id, BreakpointLocation* list);
   void RemoveUnlinkedCodeBreakpoints();