{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/4699","repository_url":"https://api.github.com/repos/Microsoft/ChakraCore","labels_url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/4699/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/4699/comments","events_url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/4699/events","html_url":"https://github.com/Microsoft/ChakraCore/issues/4699","id":298119477,"number":4699,"title":"How to use ChakraCore Debug API","user":{"login":"lemmel","id":810483,"avatar_url":"https://avatars2.githubusercontent.com/u/810483?v=4","gravatar_id":"","url":"https://api.github.com/users/lemmel","html_url":"https://github.com/lemmel","followers_url":"https://api.github.com/users/lemmel/followers","following_url":"https://api.github.com/users/lemmel/following{/other_user}","gists_url":"https://api.github.com/users/lemmel/gists{/gist_id}","starred_url":"https://api.github.com/users/lemmel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lemmel/subscriptions","organizations_url":"https://api.github.com/users/lemmel/orgs","repos_url":"https://api.github.com/users/lemmel/repos","events_url":"https://api.github.com/users/lemmel/events{/privacy}","received_events_url":"https://api.github.com/users/lemmel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-18T21:23:03Z","updated_at":"2018-02-18T23:14:08Z","closed_at":null,"author_association":"NONE","body":"Hi.\r\n\r\nOnce the JsDiagStartDebugging call is  done with a certain callback, and a call to [JSRun](https://github.com/Microsoft/ChakraCore/wiki/JsRun) is in the process of the provided code, the provided callback is called with the event JsDiagDebugEventSourceCompile.\r\nThat's ok.\r\nBut the code provided to JSRun is always runned before taking into account any call to JsDiagSetBreakpoint, or JsDiagRequestAsyncBreak, even if these functions are called inside the callback, or called before JSRun.\r\n\r\nAnd so I always end with the JsErrorDiagNotAtBreak error code.\r\nFurthermore with more complex code, any \"echo\" in the JS code is done (and visible in the console) when the call to [JSRun](https://github.com/Microsoft/ChakraCore/wiki/JsRun) is completed. So anything done after the [JSRun](https://github.com/Microsoft/ChakraCore/wiki/JsRun) completion seems to be useless as the JS code will be executed.\r\n\r\nCan you tell how to start the debugging and stop at the first line ?\r\n\r\nP.S.: You can find below, one of my many attempts to get it right.\r\n\r\n````cpp\r\n#include <cstdio>\r\n#include <cstring>\r\n#include <cstdlib>\r\n#include \"ChakraCore.h\"\r\n#define EXCEPTION_MAX_LENGTH 255\r\n\r\nclass ErrorCode\r\n{\r\n  bool withException;\r\n  JsErrorCode errCode;\r\npublic:\r\n  explicit ErrorCode(bool withException = true):withException(withException), errCode(JsNoError){}\r\n  explicit ErrorCode(JsErrorCode errCode, bool withException = true):withException(withException)\r\n  {\r\n    (*this) = errCode;\r\n  }\r\n  ErrorCode(const ErrorCode& errorCode)\r\n    :withException(errorCode.withException), errCode(errorCode.errorCode())\r\n  {}\r\n  operator bool() const\r\n  {\r\n    return errCode == JsNoError;\r\n  }\r\n  JsErrorCode errorCode() const{return errCode;}\r\n  ErrorCode& operator()(JsErrorCode errorCode, const char* errorMessage) noexcept (false)\r\n  {\r\n    this->errCode = errorCode;\r\n    if (errorCode == JsNoError)\r\n      return *this;\r\n    char buffer [EXCEPTION_MAX_LENGTH + 1];\r\n    size_t actualLength;\r\n    JsErrorCode error;\r\n\r\n    JsValueRef exception;\r\n    if ((error = JsGetAndClearException(&exception)) != JsNoError)\r\n    {\r\n      printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\",errorCode, error, errorMessage);\r\n      exit(EXIT_FAILURE);\r\n    }\r\n\r\n    JsPropertyIdRef messageName;\r\n    if ((error = JsCreatePropertyId(\"message\", strlen(\"message\"), &messageName)) != JsNoError)\r\n    {\r\n      printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\",errorCode, error, errorMessage);\r\n      exit(EXIT_FAILURE);\r\n    }\r\n\r\n    JsValueRef messageValue;\r\n    if ((error = JsGetProperty(exception, messageName, &messageValue)) != JsNoError)\r\n    {\r\n      printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\",errorCode, error, errorMessage);\r\n      exit(EXIT_FAILURE);\r\n    }\r\n    if ((error = JsCopyString(messageValue, buffer, EXCEPTION_MAX_LENGTH, &actualLength)) != JsNoError)\r\n    {\r\n      printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\",errorCode, error, errorMessage);\r\n      exit(EXIT_FAILURE);\r\n    }\r\n\r\n    buffer[actualLength] = 0;\r\n    printf(\"Error=%s; extra info=%s\\n\", buffer, errorMessage);\r\n\r\n    exit(EXIT_FAILURE);\r\n  }\r\n  ErrorCode& operator=(JsErrorCode errorCode) noexcept (false)\r\n  {\r\n    return (*this)(errorCode, \"\");\r\n  }\r\n  ErrorCode& operator=(const ErrorCode& errorCode) noexcept (true)\r\n  {\r\n    this->errCode = errorCode.errCode;\r\n    this->withException = errorCode.withException;\r\n    return *this;\r\n  }\r\n};\r\nunsigned retrieveId(JsValueRef value);\r\nstatic unsigned scriptId ;\r\nstatic JsRuntimeHandle runtime;\r\nstatic JsContextRef context;\r\nstatic JsValueRef globalObject;\r\nstatic ErrorCode errCode;\r\nstatic JsValueRef breakpoint;\r\nvoid debugCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState)\r\n{\r\n  printf(\"debugCallback called on %d\\n\", debugEvent);\r\n  fflush(stdout);\r\n  static bool allBreakDone = false;\r\n  scriptId = retrieveId(eventData);\r\n  if (!allBreakDone)\r\n  {\r\n    errCode = JsDiagRequestAsyncBreak(runtime);\r\n    errCode = JsDiagSetBreakpoint(scriptId, 0, 0, &breakpoint);\r\n    allBreakDone = true;\r\n  }\r\n}\r\nint main()\r\n{\r\n  errCode = JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);\r\n  errCode = JsCreateContext(runtime, &context);\r\n  errCode = JsSetCurrentContext(context);\r\n  errCode = JsGetGlobalObject(&globalObject);\r\n  errCode = JsDiagStartDebugging(runtime, debugCallback, nullptr);\r\n  const char code[] = R\"END(\r\n                      var a = 0;\r\n                      for(var i = 0; i < 10 ; i++)\r\n                        a += i;\r\n                      a++;\r\n                      )END\";\r\n  unsigned currentSourceContext = 1;\r\n  JsValueRef fname;\r\n  JsValueRef jsResult;\r\n  errCode = JsCreateString(\"\", 0, &fname);\r\n  JsValueRef scriptSource;\r\n  errCode = JsCreateString(code,strlen(code), &scriptSource);\r\n  errCode = JsRun(scriptSource, currentSourceContext++, fname, JsParseScriptAttributeNone, &jsResult);\r\n\r\n  JsValueRef breakpoint;\r\n  errCode = JsDiagSetBreakpoint(scriptId, 0, 0, &breakpoint);\r\n\r\n  errCode = JsDiagRequestAsyncBreak(runtime);\r\n  errCode = JsDiagSetStepType(JsDiagStepTypeContinue);\r\n\r\n  return EXIT_SUCCESS;\r\n}\r\n````"},"comment":{"url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/comments/366557196","html_url":"https://github.com/Microsoft/ChakraCore/issues/4699#issuecomment-366557196","issue_url":"https://api.github.com/repos/Microsoft/ChakraCore/issues/4699","id":366557196,"user":{"login":"agarwal-sandeep","id":12059066,"avatar_url":"https://avatars3.githubusercontent.com/u/12059066?v=4","gravatar_id":"","url":"https://api.github.com/users/agarwal-sandeep","html_url":"https://github.com/agarwal-sandeep","followers_url":"https://api.github.com/users/agarwal-sandeep/followers","following_url":"https://api.github.com/users/agarwal-sandeep/following{/other_user}","gists_url":"https://api.github.com/users/agarwal-sandeep/gists{/gist_id}","starred_url":"https://api.github.com/users/agarwal-sandeep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agarwal-sandeep/subscriptions","organizations_url":"https://api.github.com/users/agarwal-sandeep/orgs","repos_url":"https://api.github.com/users/agarwal-sandeep/repos","events_url":"https://api.github.com/users/agarwal-sandeep/events{/privacy}","received_events_url":"https://api.github.com/users/agarwal-sandeep/received_events","type":"User","site_admin":false},"created_at":"2018-02-18T23:14:08Z","updated_at":"2018-02-18T23:14:08Z","author_association":"CONTRIBUTOR","body":"@lemmel I don't see a definition of retrieveId in the code above so don't know which scriptId you got. \r\nThe scriptId for the source should be 1 and if I use that inside first call to debugCallback (JsDiagDebugEventSourceCompile) the JsDiagSetBreakpoint gets set and also hits after returning from debugCallback.\r\n\r\nThis is the code I executed\r\n\r\n```c++\r\n#include \"stdafx.h\"\r\n#include <cstdio>\r\n#include <cstring>\r\n#include <cstdlib>\r\n#include \"ChakraCore.h\"\r\n#define EXCEPTION_MAX_LENGTH 255\r\n\r\nclass ErrorCode\r\n{\r\n    bool withException;\r\n    JsErrorCode errCode;\r\npublic:\r\n    explicit ErrorCode(bool withException = true) :withException(withException), errCode(JsNoError) {}\r\n    explicit ErrorCode(JsErrorCode errCode, bool withException = true) :withException(withException)\r\n    {\r\n        (*this) = errCode;\r\n    }\r\n    ErrorCode(const ErrorCode& errorCode)\r\n        :withException(errorCode.withException), errCode(errorCode.errorCode())\r\n    {}\r\n    operator bool() const\r\n    {\r\n        return errCode == JsNoError;\r\n    }\r\n    JsErrorCode errorCode() const { return errCode; }\r\n    ErrorCode& operator()(JsErrorCode errorCode, const char* errorMessage) noexcept (false)\r\n    {\r\n        this->errCode = errorCode;\r\n        if (errorCode == JsNoError)\r\n            return *this;\r\n        char buffer[EXCEPTION_MAX_LENGTH + 1];\r\n        size_t actualLength;\r\n        JsErrorCode error;\r\n\r\n        JsValueRef exception;\r\n        if ((error = JsGetAndClearException(&exception)) != JsNoError)\r\n        {\r\n            printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\", errorCode, error, errorMessage);\r\n            exit(EXIT_FAILURE);\r\n        }\r\n\r\n        JsPropertyIdRef messageName;\r\n        if ((error = JsCreatePropertyId(\"message\", strlen(\"message\"), &messageName)) != JsNoError)\r\n        {\r\n            printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\", errorCode, error, errorMessage);\r\n            exit(EXIT_FAILURE);\r\n        }\r\n\r\n        JsValueRef messageValue;\r\n        if ((error = JsGetProperty(exception, messageName, &messageValue)) != JsNoError)\r\n        {\r\n            printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\", errorCode, error, errorMessage);\r\n            exit(EXIT_FAILURE);\r\n        }\r\n        if ((error = JsCopyString(messageValue, buffer, EXCEPTION_MAX_LENGTH, &actualLength)) != JsNoError)\r\n        {\r\n            printf(\"Error on Error! Original error code=%d, new error code=%d; extra info=%s\\n\", errorCode, error, errorMessage);\r\n            exit(EXIT_FAILURE);\r\n        }\r\n\r\n        buffer[actualLength] = 0;\r\n        printf(\"Error=%s; extra info=%s\\n\", buffer, errorMessage);\r\n\r\n        exit(EXIT_FAILURE);\r\n    }\r\n    ErrorCode& operator=(JsErrorCode errorCode) noexcept (false)\r\n    {\r\n        return (*this)(errorCode, \"\");\r\n    }\r\n    ErrorCode& operator=(const ErrorCode& errorCode) noexcept (true)\r\n    {\r\n        this->errCode = errorCode.errCode;\r\n        this->withException = errorCode.withException;\r\n        return *this;\r\n    }\r\n};\r\nunsigned retrieveId(JsValueRef value);\r\nstatic unsigned scriptId  = 1;\r\nstatic JsRuntimeHandle runtime;\r\nstatic JsContextRef context;\r\nstatic JsValueRef globalObject;\r\nstatic ErrorCode errCode;\r\nstatic JsValueRef breakpoint;\r\nvoid CHAKRA_CALLBACK debugCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState)\r\n{\r\n    printf(\"debugCallback called on %d\\n\", debugEvent);\r\n    fflush(stdout);\r\n    static bool allBreakDone = false;\r\n    scriptId = 1; // retrieveId(eventData);\r\n    if (!allBreakDone)\r\n    {\r\n        //errCode = JsDiagRequestAsyncBreak(runtime);\r\n        errCode = JsDiagSetBreakpoint(scriptId, 0, 0, &breakpoint);\r\n        allBreakDone = true;\r\n    }\r\n}\r\nint main()\r\n{\r\n    errCode = JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);\r\n    errCode = JsCreateContext(runtime, &context);\r\n    errCode = JsSetCurrentContext(context);\r\n    errCode = JsGetGlobalObject(&globalObject);\r\n    errCode = JsDiagStartDebugging(runtime, debugCallback, nullptr);\r\n    const char code[] = R\"END(\r\n                      var a = 0;\r\n                      for(var i = 0; i < 10 ; i++)\r\n                        a += i;\r\n                      a++;\r\n                      )END\";\r\n    unsigned currentSourceContext = 1;\r\n    JsValueRef fname;\r\n    JsValueRef jsResult;\r\n    errCode = JsCreateString(\"\", 0, &fname);\r\n    JsValueRef scriptSource;\r\n    errCode = JsCreateString(code, strlen(code), &scriptSource);\r\n    errCode = JsRun(scriptSource, currentSourceContext++, fname, JsParseScriptAttributeNone, &jsResult);\r\n\r\n    /*\r\n    JsValueRef breakpoint;\r\n    errCode = JsDiagSetBreakpoint(1, 0, 0, &breakpoint);\r\n\r\n    errCode = JsDiagRequestAsyncBreak(runtime);\r\n    errCode = JsDiagSetStepType(JsDiagStepTypeContinue);*/\r\n\r\n    \r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n```"}}
{"actor":{"display_login":"agarwal-sandeep"}}