Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: runtime/bin/dbg_message.cc

Issue 11028040: - Add support to interrupt a running isolate (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/dbg_connection.h" 5 #include "bin/dbg_connection.h"
6 #include "bin/dbg_message.h" 6 #include "bin/dbg_message.h"
7 #include "bin/dartutils.h" 7 #include "bin/dartutils.h"
8 #include "bin/thread.h" 8 #include "bin/thread.h"
9 #include "bin/utils.h" 9 #include "bin/utils.h"
10 10
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 in_msg->SendErrorReply(msg_id, Dart_GetError(res)); 789 in_msg->SendErrorReply(msg_id, Dart_GetError(res));
790 return false; 790 return false;
791 } 791 }
792 dart::TextBuffer msg(32); 792 dart::TextBuffer msg(32);
793 msg.Printf("{ \"id\": %d }", msg_id); 793 msg.Printf("{ \"id\": %d }", msg_id);
794 in_msg->SendReply(&msg); 794 in_msg->SendReply(&msg);
795 return false; 795 return false;
796 } 796 }
797 797
798 798
799 void DbgMessageQueue::AddMessage(int32_t cmd_idx, 799 void DbgMsgQueue::AddMessage(int32_t cmd_idx,
800 const char* start, 800 const char* start,
hausner 2012/10/05 18:06:34 Indentation
siva 2012/10/05 22:35:06 Done.
801 const char* end, 801 const char* end,
802 int debug_fd) { 802 int debug_fd) {
803 if ((end > start) && ((end - start) < kMaxInt32)) { 803 if ((end > start) && ((end - start) < kMaxInt32)) {
804 MonitorLocker ml(&msg_queue_lock_); 804 MonitorLocker ml(&msg_queue_lock_);
805 DbgMessage* msg = new DbgMessage(cmd_idx, start, end, debug_fd); 805 DbgMessage* msg = new DbgMessage(cmd_idx, start, end, debug_fd);
806 if (msglist_head_ == NULL) { 806 if (msglist_head_ == NULL) {
807 ASSERT(msglist_tail_ == NULL); 807 ASSERT(msglist_tail_ == NULL);
808 msglist_head_ = msg; 808 msglist_head_ = msg;
809 msglist_tail_ = msg; 809 msglist_tail_ = msg;
810 ml.Notify(); 810 ml.Notify();
811 } else { 811 } else {
812 ASSERT(msglist_tail_ != NULL); 812 ASSERT(msglist_tail_ != NULL);
813 msglist_tail_->set_next(msg); 813 msglist_tail_->set_next(msg);
814 msglist_tail_ = msg; 814 msglist_tail_ = msg;
815 } 815 }
816 } 816 }
817 } 817 }
818 818
819 819
820 void DbgMessageQueue::HandleMessages() { 820 void DbgMsgQueue::HandleMessages() {
821 bool resume_requested = false; 821 bool resume_requested = false;
822 MonitorLocker ml(&msg_queue_lock_); 822 MonitorLocker ml(&msg_queue_lock_);
823 is_running_ = false; 823 is_running_ = false;
824 while (!resume_requested) { 824 while (!resume_requested) {
825 while (msglist_head_ == NULL) { 825 while (msglist_head_ == NULL) {
826 ASSERT(msglist_tail_ == NULL); 826 ASSERT(msglist_tail_ == NULL);
827 dart::Monitor::WaitResult res = ml.Wait(); // Wait for debugger commands. 827 dart::Monitor::WaitResult res = ml.Wait(); // Wait for debugger commands.
828 ASSERT(res == dart::Monitor::kNotified); 828 ASSERT(res == dart::Monitor::kNotified);
829 } 829 }
830 while (msglist_head_ != NULL && !resume_requested) { 830 while (msglist_head_ != NULL && !resume_requested) {
831 ASSERT(msglist_tail_ != NULL); 831 ASSERT(msglist_tail_ != NULL);
832 DbgMessage* msg = msglist_head_; 832 DbgMessage* msg = msglist_head_;
833 msglist_head_ = msglist_head_->next(); 833 msglist_head_ = msglist_head_->next();
834 resume_requested = msg->HandleMessage(); 834 resume_requested = msg->HandleMessage();
835 delete msg; 835 delete msg;
836 } 836 }
837 if (msglist_head_ == NULL) { 837 if (msglist_head_ == NULL) {
838 msglist_tail_ = NULL; 838 msglist_tail_ = NULL;
839 } 839 }
840 } 840 }
841 is_interrupted_ = false;
841 is_running_ = true; 842 is_running_ = true;
842 } 843 }
843 844
844 845
845 void DbgMessageQueue::QueueOutputMsg(dart::TextBuffer* msg) { 846 void DbgMsgQueue::InterruptIsolate() {
847 Dart_Isolate isolate = Dart_GetIsolate(isolate_id_);
848 ASSERT(DbgMsgQueueList::GetIsolateMsgQueue(isolate_id_) == this);
849 MonitorLocker ml(&msg_queue_lock_);
850 if (is_running_ && !is_interrupted_) {
851 is_interrupted_ = true;
852 Dart_InterruptIsolate(isolate);
853 }
854 }
855
856
857 void DbgMsgQueue::QueueOutputMsg(dart::TextBuffer* msg) {
846 queued_output_messages_.Printf("%s", msg->buf()); 858 queued_output_messages_.Printf("%s", msg->buf());
847 } 859 }
848 860
849 861
850 void DbgMessageQueue::SendQueuedMsgs() { 862 void DbgMsgQueue::SendQueuedMsgs() {
851 if (queued_output_messages_.length() > 0) { 863 if (queued_output_messages_.length() > 0) {
852 DebuggerConnectionHandler::BroadcastMsg(&queued_output_messages_); 864 DebuggerConnectionHandler::BroadcastMsg(&queued_output_messages_);
853 queued_output_messages_.Clear(); 865 queued_output_messages_.Clear();
854 } 866 }
855 } 867 }
856 868
857 869
858 void DbgMessageQueue::SendBreakpointEvent(Dart_StackTrace trace) { 870 void DbgMsgQueue::SendBreakpointEvent(Dart_StackTrace trace) {
859 dart::TextBuffer msg(128); 871 dart::TextBuffer msg(128);
860 msg.Printf("{ \"event\": \"paused\", \"params\": { "); 872 msg.Printf("{ \"event\": \"paused\", \"params\": { ");
861 msg.Printf("\"reason\": \"breakpoint\", "); 873 msg.Printf("\"reason\": \"breakpoint\", ");
874 msg.Printf("\"id\": %"Pd64", ", isolate_id_);
862 FormatCallFrames(&msg, trace); 875 FormatCallFrames(&msg, trace);
863 msg.Printf("}}"); 876 msg.Printf("}}");
864 DebuggerConnectionHandler::BroadcastMsg(&msg); 877 DebuggerConnectionHandler::BroadcastMsg(&msg);
865 } 878 }
866 879
867 880
868 void DbgMessageQueue::SendExceptionEvent(Dart_Handle exception, 881 void DbgMsgQueue::SendExceptionEvent(Dart_Handle exception,
869 Dart_StackTrace stack_trace) { 882 Dart_StackTrace stack_trace) {
870 intptr_t exception_id = Dart_CacheObject(exception); 883 intptr_t exception_id = Dart_CacheObject(exception);
871 ASSERT(exception_id >= 0); 884 ASSERT(exception_id >= 0);
872 dart::TextBuffer msg(128); 885 dart::TextBuffer msg(128);
873 msg.Printf("{ \"event\": \"paused\", \"params\": {"); 886 msg.Printf("{ \"event\": \"paused\", \"params\": {");
874 msg.Printf("\"reason\": \"exception\", "); 887 msg.Printf("\"reason\": \"exception\", ");
888 msg.Printf("\"id\": %"Pd64", ", isolate_id_);
875 msg.Printf("\"exception\":"); 889 msg.Printf("\"exception\":");
876 FormatRemoteObj(&msg, exception); 890 FormatRemoteObj(&msg, exception);
877 msg.Printf(", "); 891 msg.Printf(", ");
878 FormatCallFrames(&msg, stack_trace); 892 FormatCallFrames(&msg, stack_trace);
879 msg.Printf("}}"); 893 msg.Printf("}}");
880 DebuggerConnectionHandler::BroadcastMsg(&msg); 894 DebuggerConnectionHandler::BroadcastMsg(&msg);
881 } 895 }
882 896
883 897
884 // TODO(asiva): Get rid of this static variable one we have a means 898 void DbgMsgQueue::SendIsolateEvent(Dart_IsolateId isolate_id,
885 // for associating an isolate with a debugger message queue object. 899 Dart_IsolateEvent kind) {
886 static DbgMessageQueue* message_queue = NULL; 900 dart::TextBuffer msg(128);
901 if (kind == kInterrupted) {
902 Dart_StackTrace trace;
903 Dart_Handle res = Dart_GetStackTrace(&trace);
904 ASSERT_NOT_ERROR(res);
905 msg.Printf("{ \"event\": \"paused\", \"params\": { ");
906 msg.Printf("\"reason\": \"interrupted\", ");
907 msg.Printf("\"id\": %"Pd64", ", isolate_id);
908 FormatCallFrames(&msg, trace);
909 msg.Printf("}}");
910 } else {
911 msg.Printf("{ \"event\": \"isolate\", \"params\": { ");
912 if (kind == kCreated) {
913 msg.Printf("\"reason\": \"created\", ");
914 } else {
915 ASSERT(kind == kShutdown);
916 msg.Printf("\"reason\": \"shutdown\", ");
917 }
918 msg.Printf("\"id\": %"Pd64" ", isolate_id);
919 msg.Printf("}}");
920 }
921 DebuggerConnectionHandler::BroadcastMsg(&msg);
922 }
887 923
888 924
889 void DbgMessageQueue::Initialize() { 925 DbgMsgQueue* DbgMsgQueueList::list_ = NULL;
890 // TODO(asiva): Need to setup a message queue when an Isolate is created. 926 dart::Mutex DbgMsgQueueList::msg_queue_list_lock_;
891 // For now we use a static message queue object as we are only supporting
892 // debugging of a single isolate.
893 message_queue = new DbgMessageQueue();
894 927
928
929 void DbgMsgQueueList::Initialize() {
895 // Setup handlers for isolate events, breakpoints, exceptions and 930 // Setup handlers for isolate events, breakpoints, exceptions and
896 // delayed breakpoints. 931 // delayed breakpoints.
897 Dart_SetIsolateEventHandler(IsolateEventHandler); 932 Dart_SetIsolateEventHandler(IsolateEventHandler);
898 Dart_SetBreakpointHandler(BreakpointHandler); 933 Dart_SetBreakpointHandler(BreakpointHandler);
899 Dart_SetBreakpointResolvedHandler(BptResolvedHandler); 934 Dart_SetBreakpointResolvedHandler(BptResolvedHandler);
900 Dart_SetExceptionThrownHandler(ExceptionThrownHandler); 935 Dart_SetExceptionThrownHandler(ExceptionThrownHandler);
901 } 936 }
902 937
903 938
904 int32_t DbgMessageQueue::LookupIsolateCommand(const char* buf, 939 int32_t DbgMsgQueueList::LookupIsolateCommand(const char* buf,
905 int32_t buflen) { 940 int32_t buflen) {
906 // Check if we have a isolate specific debugger command. 941 // Check if we have a isolate specific debugger command.
907 int32_t i = 0; 942 int32_t i = 0;
908 while (debugger_commands[i].cmd_string != NULL) { 943 while (debugger_commands[i].cmd_string != NULL) {
909 if (strncmp(buf, debugger_commands[i].cmd_string, buflen) == 0) { 944 if (strncmp(buf, debugger_commands[i].cmd_string, buflen) == 0) {
910 return i; 945 return i;
911 } 946 }
912 i++; 947 i++;
913 } 948 }
914 return kInvalidCommand; 949 return kInvalidCommand;
915 } 950 }
916 951
917 952
918 DbgMessageQueue* DbgMessageQueue::GetIsolateMessageQueue(Dart_Isolate isolate) { 953 DbgMsgQueue* DbgMsgQueueList::AddIsolateMsgQueue(Dart_IsolateId isolate_id) {
919 // TODO(asiva): Return a message queue corresponding to the isolate. 954 MutexLocker ml(&msg_queue_list_lock_);
920 // For now we use a static message queue object as we are only supporting 955
921 // debugging of a single isolate. 956 DbgMsgQueue* queue = new DbgMsgQueue(isolate_id, list_);
922 return message_queue; 957 ASSERT(queue != NULL);
958 list_ = queue;
959 return queue;
923 } 960 }
924 961
925 962
926 void DbgMessageQueue::BptResolvedHandler(intptr_t bp_id, 963 DbgMsgQueue* DbgMsgQueueList::GetIsolateMsgQueue(Dart_IsolateId isolate_id) {
964 MutexLocker ml(&msg_queue_list_lock_);
965
966 if (list_ == NULL) {
967 return NULL; // No items in the list.
968 }
969
970 // TODO(asiva): Remove once debug wire protocol has isolate id.
971 // For now we return the first item in the list as we are only supporting
972 // debugging of a single isolate.
973 if (isolate_id == ILLEGAL_ISOLATE_ID) {
974 return list_;
975 }
976
977 // Find message queue corresponding to isolate id.
978 DbgMsgQueue* iterator = list_;
979 while (iterator != NULL && iterator->isolate_id() != isolate_id) {
980 iterator = iterator->next();
981 }
982 return iterator;
983 }
984
985
986 void DbgMsgQueueList::RemoveIsolateMsgQueue(Dart_IsolateId isolate_id) {
987 MutexLocker ml(&msg_queue_list_lock_);
988 if (list_ == NULL) {
989 return; // No items in the list.
990 }
991 DbgMsgQueue* queue = list_;
992 if (queue->isolate_id() == isolate_id) {
993 list_ = queue->next(); // Remove from list.
994 delete queue; // Delete the message queue.
995 } else {
996 DbgMsgQueue* iterator = queue;
997 queue = queue->next();
998 while (queue != NULL) {
999 if (queue->isolate_id() != isolate_id) {
1000 iterator->set_next(queue->next()); // Remove from list.
1001 delete queue; // Delete the message queue.
1002 break;
1003 }
1004 iterator = queue;
1005 queue = queue->next();
1006 }
1007 }
1008 }
1009
1010
1011 void DbgMsgQueueList::BptResolvedHandler(Dart_IsolateId isolate_id,
1012 intptr_t bp_id,
927 Dart_Handle url, 1013 Dart_Handle url,
928 intptr_t line_number) { 1014 intptr_t line_number) {
1015 ASSERT(Dart_GetIsolate(isolate_id) == Dart_CurrentIsolate());
929 Dart_EnterScope(); 1016 Dart_EnterScope();
930 dart::TextBuffer msg(128); 1017 dart::TextBuffer msg(128);
931 msg.Printf("{ \"event\": \"breakpointResolved\", \"params\": {"); 1018 msg.Printf("{ \"event\": \"breakpointResolved\", \"params\": {");
932 msg.Printf("\"breakpointId\": %"Pd", \"url\":", bp_id); 1019 msg.Printf("\"breakpointId\": %"Pd", \"url\":", bp_id);
933 FormatEncodedString(&msg, url); 1020 FormatEncodedString(&msg, url);
934 msg.Printf(",\"line\": %"Pd" }}", line_number); 1021 msg.Printf(",\"line\": %"Pd" }}", line_number);
935 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate()); 1022 DbgMsgQueue* msg_queue = GetIsolateMsgQueue(isolate_id);
936 ASSERT(msg_queue != NULL); 1023 ASSERT(msg_queue != NULL);
937 msg_queue->QueueOutputMsg(&msg); 1024 msg_queue->QueueOutputMsg(&msg);
938 Dart_ExitScope(); 1025 Dart_ExitScope();
939 } 1026 }
940 1027
941 1028
942 void DbgMessageQueue::BreakpointHandler(Dart_Breakpoint bpt, 1029 void DbgMsgQueueList::BreakpointHandler(Dart_IsolateId isolate_id,
1030 Dart_Breakpoint bpt,
943 Dart_StackTrace trace) { 1031 Dart_StackTrace trace) {
1032 ASSERT(Dart_GetIsolate(isolate_id) == Dart_CurrentIsolate());
944 DebuggerConnectionHandler::WaitForConnection(); 1033 DebuggerConnectionHandler::WaitForConnection();
945 Dart_EnterScope(); 1034 Dart_EnterScope();
946 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate()); 1035 DbgMsgQueue* msg_queue = GetIsolateMsgQueue(isolate_id);
947 ASSERT(msg_queue != NULL); 1036 ASSERT(msg_queue != NULL);
948 msg_queue->SendQueuedMsgs(); 1037 msg_queue->SendQueuedMsgs();
949 msg_queue->SendBreakpointEvent(trace); 1038 msg_queue->SendBreakpointEvent(trace);
950 msg_queue->HandleMessages(); 1039 msg_queue->HandleMessages();
951 Dart_ExitScope(); 1040 Dart_ExitScope();
952 } 1041 }
953 1042
954 1043
955 void DbgMessageQueue::ExceptionThrownHandler(Dart_Handle exception, 1044 void DbgMsgQueueList::ExceptionThrownHandler(Dart_IsolateId isolate_id,
1045 Dart_Handle exception,
956 Dart_StackTrace stack_trace) { 1046 Dart_StackTrace stack_trace) {
1047 ASSERT(Dart_GetIsolate(isolate_id) == Dart_CurrentIsolate());
957 DebuggerConnectionHandler::WaitForConnection(); 1048 DebuggerConnectionHandler::WaitForConnection();
958 Dart_EnterScope(); 1049 Dart_EnterScope();
959 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate()); 1050 DbgMsgQueue* msg_queue = GetIsolateMsgQueue(isolate_id);
960 ASSERT(msg_queue != NULL); 1051 ASSERT(msg_queue != NULL);
961 msg_queue->SendQueuedMsgs(); 1052 msg_queue->SendQueuedMsgs();
962 msg_queue->SendExceptionEvent(exception, stack_trace); 1053 msg_queue->SendExceptionEvent(exception, stack_trace);
963 msg_queue->HandleMessages(); 1054 msg_queue->HandleMessages();
964 Dart_ExitScope(); 1055 Dart_ExitScope();
965 } 1056 }
966 1057
967 1058
968 void DbgMessageQueue::IsolateEventHandler(Dart_IsolateId isolate_id, 1059 void DbgMsgQueueList::IsolateEventHandler(Dart_IsolateId isolate_id,
969 Dart_IsolateEvent kind) { 1060 Dart_IsolateEvent kind) {
970 DebuggerConnectionHandler::WaitForConnection(); 1061 DebuggerConnectionHandler::WaitForConnection();
971 // TODO(asiva): Add code to send isolate events over to the debugger client. 1062 Dart_EnterScope();
1063 if (kind == kCreated) {
1064 DbgMsgQueue* msg_queue = AddIsolateMsgQueue(isolate_id);
1065 msg_queue->SendIsolateEvent(isolate_id, kind);
1066 } else {
1067 ASSERT(Dart_GetIsolate(isolate_id) == Dart_CurrentIsolate());
1068 DbgMsgQueue* msg_queue = GetIsolateMsgQueue(isolate_id);
1069 ASSERT(msg_queue != NULL);
1070 msg_queue->SendQueuedMsgs();
1071 msg_queue->SendIsolateEvent(isolate_id, kind);
1072 if (kind == kInterrupted) {
1073 msg_queue->HandleMessages();
1074 } else {
1075 ASSERT(kind == kShutdown);
1076 RemoveIsolateMsgQueue(isolate_id);
1077 }
1078 }
1079 Dart_ExitScope();
972 } 1080 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698