LCOV - code coverage report
Current view: directory - js/xpconnect/src - XPCStack.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 115 78 67.8 %
Date: 2012-06-02 Functions: 19 15 78.9 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  *
       3                 :  * ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is Mozilla Communicator client code, released
      17                 :  * March 31, 1998.
      18                 :  *
      19                 :  * The Initial Developer of the Original Code is
      20                 :  * Netscape Communications Corporation.
      21                 :  * Portions created by the Initial Developer are Copyright (C) 1999
      22                 :  * the Initial Developer. All Rights Reserved.
      23                 :  *
      24                 :  * Contributor(s):
      25                 :  *   John Bandhauer <jband@netscape.com> (original author)
      26                 :  *
      27                 :  * Alternatively, the contents of this file may be used under the terms of
      28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      31                 :  * of those above. If you wish to allow use of your version of this file only
      32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      33                 :  * use your version of this file under the terms of the MPL, indicate your
      34                 :  * decision by deleting the provisions above and replace them with the notice
      35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      36                 :  * the provisions above, a recipient may use your version of this file under
      37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      38                 :  *
      39                 :  * ***** END LICENSE BLOCK ***** */
      40                 : 
      41                 : /* Implements nsIStackFrame. */
      42                 : 
      43                 : #include "xpcprivate.h"
      44                 : 
      45                 : class XPCJSStackFrame : public nsIStackFrame
      46                 : {
      47                 : public:
      48                 :     NS_DECL_ISUPPORTS
      49                 :     NS_DECL_NSISTACKFRAME
      50                 : 
      51                 :     static nsresult CreateStack(JSContext* cx, JSStackFrame* fp,
      52                 :                                 XPCJSStackFrame** stack);
      53                 : 
      54                 :     static nsresult CreateStackFrameLocation(PRUint32 aLanguage,
      55                 :                                              const char* aFilename,
      56                 :                                              const char* aFunctionName,
      57                 :                                              PRInt32 aLineNumber,
      58                 :                                              nsIStackFrame* aCaller,
      59                 :                                              XPCJSStackFrame** stack);
      60                 : 
      61                 :     XPCJSStackFrame();
      62                 :     virtual ~XPCJSStackFrame();
      63                 : 
      64            1238 :     JSBool IsJSFrame() const
      65            1238 :         {return mLanguage == nsIProgrammingLanguage::JAVASCRIPT;}
      66                 : 
      67                 : private:
      68                 :     nsCOMPtr<nsIStackFrame> mCaller;
      69                 : 
      70                 :     char* mFilename;
      71                 :     char* mFunname;
      72                 :     PRInt32 mLineno;
      73                 :     PRUint32 mLanguage;
      74                 : };
      75                 : 
      76                 : /**********************************************/
      77                 : 
      78                 : // static
      79                 : 
      80                 : nsresult
      81          206181 : XPCJSStack::CreateStack(JSContext* cx, nsIStackFrame** stack)
      82                 : {
      83          206181 :     if (!cx)
      84               0 :         return NS_ERROR_FAILURE;
      85                 : 
      86          206181 :     JSStackFrame *fp = NULL;
      87          206181 :     if (!JS_FrameIterator(cx, &fp))
      88            8966 :         return NS_ERROR_FAILURE;
      89          197215 :     return XPCJSStackFrame::CreateStack(cx, fp, (XPCJSStackFrame**) stack);
      90                 : }
      91                 : 
      92                 : // static
      93                 : nsresult
      94               0 : XPCJSStack::CreateStackFrameLocation(PRUint32 aLanguage,
      95                 :                                      const char* aFilename,
      96                 :                                      const char* aFunctionName,
      97                 :                                      PRInt32 aLineNumber,
      98                 :                                      nsIStackFrame* aCaller,
      99                 :                                      nsIStackFrame** stack)
     100                 : {
     101                 :     return XPCJSStackFrame::CreateStackFrameLocation(aLanguage,
     102                 :                                                      aFilename,
     103                 :                                                      aFunctionName,
     104                 :                                                      aLineNumber,
     105                 :                                                      aCaller,
     106               0 :                                                      (XPCJSStackFrame**) stack);
     107                 : }
     108                 : 
     109                 : 
     110                 : /**********************************************/
     111                 : 
     112         1496139 : XPCJSStackFrame::XPCJSStackFrame()
     113                 :     :   mFilename(nsnull),
     114                 :         mFunname(nsnull),
     115                 :         mLineno(0),
     116         1496139 :         mLanguage(nsIProgrammingLanguage::UNKNOWN)
     117                 : {
     118         1496139 : }
     119                 : 
     120         4488417 : XPCJSStackFrame::~XPCJSStackFrame()
     121                 : {
     122         1496139 :     if (mFilename)
     123         1496110 :         nsMemory::Free(mFilename);
     124         1496139 :     if (mFunname)
     125          936746 :         nsMemory::Free(mFunname);
     126         5984556 : }
     127                 : 
     128        15064469 : NS_IMPL_THREADSAFE_ISUPPORTS1(XPCJSStackFrame, nsIStackFrame)
     129                 : 
     130                 : nsresult
     131          197215 : XPCJSStackFrame::CreateStack(JSContext* cx, JSStackFrame* fp,
     132                 :                              XPCJSStackFrame** stack)
     133                 : {
     134                 :     static const unsigned MAX_FRAMES = 3000;
     135          197215 :     unsigned numFrames = 0;
     136                 : 
     137          394430 :     nsRefPtr<XPCJSStackFrame> first = new XPCJSStackFrame();
     138          394430 :     nsRefPtr<XPCJSStackFrame> self = first;
     139         1890569 :     while (fp && self) {
     140         1496139 :         if (!JS_IsScriptFrame(cx, fp)) {
     141              29 :             self->mLanguage = nsIProgrammingLanguage::CPLUSPLUS;
     142                 :         } else {
     143         1496110 :             self->mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
     144         1496110 :             JSScript* script = JS_GetFrameScript(cx, fp);
     145         1496110 :             jsbytecode* pc = JS_GetFramePC(cx, fp);
     146         1496110 :             if (script && pc) {
     147         2992220 :                 JS::AutoEnterFrameCompartment ac;
     148         1496110 :                 if (ac.enter(cx, fp)) {
     149         1496110 :                     const char* filename = JS_GetScriptFilename(cx, script);
     150         1496110 :                     if (filename) {
     151         1496110 :                         self->mFilename = (char*)
     152                 :                             nsMemory::Clone(filename,
     153         1496110 :                                             sizeof(char)*(strlen(filename)+1));
     154                 :                     }
     155                 : 
     156         1496110 :                     self->mLineno = (PRInt32) JS_PCToLineNumber(cx, script, pc);
     157                 : 
     158         1496110 :                     JSFunction* fun = JS_GetFrameFunction(cx, fp);
     159         1496110 :                     if (fun) {
     160         1344101 :                         JSString *funid = JS_GetFunctionId(fun);
     161         1344101 :                         if (funid) {
     162          936746 :                             size_t length = JS_GetStringEncodingLength(cx, funid);
     163          936746 :                             if (length != size_t(-1)) {
     164          936746 :                                 self->mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
     165          936746 :                                 if (self->mFunname) {
     166          936746 :                                     JS_EncodeStringToBuffer(funid, self->mFunname, length);
     167          936746 :                                     self->mFunname[length] = '\0';
     168                 :                                 }
     169                 :                             }
     170                 :                         }
     171                 :                     }
     172         1496110 :                 }
     173                 :             } else {
     174               0 :                 self->mLanguage = nsIProgrammingLanguage::CPLUSPLUS;
     175                 :             }
     176                 :         }
     177                 : 
     178         1496139 :         if (++numFrames > MAX_FRAMES) {
     179               0 :             fp = NULL;
     180         1496139 :         } else if (JS_FrameIterator(cx, &fp)) {
     181         1298924 :             XPCJSStackFrame* frame = new XPCJSStackFrame();
     182         1298924 :             self->mCaller = frame;
     183         1298924 :             self = frame;
     184                 :         }
     185                 :     }
     186                 : 
     187          197215 :     *stack = first.forget().get();
     188          197215 :     return NS_OK;
     189                 : }
     190                 : 
     191                 : // static
     192                 : nsresult
     193               0 : XPCJSStackFrame::CreateStackFrameLocation(PRUint32 aLanguage,
     194                 :                                           const char* aFilename,
     195                 :                                           const char* aFunctionName,
     196                 :                                           PRInt32 aLineNumber,
     197                 :                                           nsIStackFrame* aCaller,
     198                 :                                           XPCJSStackFrame** stack)
     199                 : {
     200               0 :     JSBool failed = false;
     201               0 :     XPCJSStackFrame* self = new XPCJSStackFrame();
     202               0 :     if (self)
     203               0 :         NS_ADDREF(self);
     204                 :     else
     205               0 :         failed = true;
     206                 : 
     207               0 :     if (!failed) {
     208               0 :         self->mLanguage = aLanguage;
     209               0 :         self->mLineno = aLineNumber;
     210                 :     }
     211                 : 
     212               0 :     if (!failed && aFilename) {
     213                 :         self->mFilename = (char*)
     214                 :                 nsMemory::Clone(aFilename,
     215               0 :                                 sizeof(char)*(strlen(aFilename)+1));
     216               0 :         if (!self->mFilename)
     217               0 :             failed = true;
     218                 :     }
     219                 : 
     220               0 :     if (!failed && aFunctionName) {
     221                 :         self->mFunname = (char*)
     222                 :                 nsMemory::Clone(aFunctionName,
     223               0 :                                 sizeof(char)*(strlen(aFunctionName)+1));
     224               0 :         if (!self->mFunname)
     225               0 :             failed = true;
     226                 :     }
     227                 : 
     228               0 :     if (!failed && aCaller) {
     229               0 :         self->mCaller = aCaller;
     230                 :     }
     231                 : 
     232               0 :     if (failed && self) {
     233               0 :         NS_RELEASE(self);   // sets self to nsnull
     234                 :     }
     235                 : 
     236               0 :     *stack = self;
     237               0 :     return self ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
     238                 : }
     239                 : 
     240                 : /* readonly attribute PRUint32 language; */
     241          237630 : NS_IMETHODIMP XPCJSStackFrame::GetLanguage(PRUint32 *aLanguage)
     242                 : {
     243          237630 :     *aLanguage = mLanguage;
     244          237630 :     return NS_OK;
     245                 : }
     246                 : 
     247                 : /* readonly attribute string languageName; */
     248               0 : NS_IMETHODIMP XPCJSStackFrame::GetLanguageName(char * *aLanguageName)
     249                 : {
     250                 :     static const char js[] = "JavaScript";
     251                 :     static const char cpp[] = "C++";
     252                 :     char* temp;
     253                 : 
     254               0 :     if (IsJSFrame())
     255               0 :         *aLanguageName = temp = (char*) nsMemory::Clone(js, sizeof(js));
     256                 :     else
     257               0 :         *aLanguageName = temp = (char*) nsMemory::Clone(cpp, sizeof(cpp));
     258                 : 
     259               0 :     return temp ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
     260                 : }
     261                 : 
     262                 : /* readonly attribute string filename; */
     263          196690 : NS_IMETHODIMP XPCJSStackFrame::GetFilename(char * *aFilename)
     264                 : {
     265          196690 :     XPC_STRING_GETTER_BODY(aFilename, mFilename);
     266                 : }
     267                 : 
     268                 : /* readonly attribute string name; */
     269          154677 : NS_IMETHODIMP XPCJSStackFrame::GetName(char * *aFunction)
     270                 : {
     271          154677 :     XPC_STRING_GETTER_BODY(aFunction, mFunname);
     272                 : }
     273                 : 
     274                 : /* readonly attribute PRInt32 lineNumber; */
     275          196277 : NS_IMETHODIMP XPCJSStackFrame::GetLineNumber(PRInt32 *aLineNumber)
     276                 : {
     277          196277 :     *aLineNumber = mLineno;
     278          196277 :     return NS_OK;
     279                 : }
     280                 : 
     281                 : /* readonly attribute string sourceLine; */
     282               0 : NS_IMETHODIMP XPCJSStackFrame::GetSourceLine(char * *aSourceLine)
     283                 : {
     284               0 :     *aSourceLine = nsnull;
     285               0 :     return NS_OK;
     286                 : }
     287                 : 
     288                 : /* readonly attribute nsIStackFrame caller; */
     289          156981 : NS_IMETHODIMP XPCJSStackFrame::GetCaller(nsIStackFrame * *aCaller)
     290                 : {
     291          156981 :     NS_IF_ADDREF(*aCaller = mCaller);
     292          156981 :     return NS_OK;
     293                 : }
     294                 : 
     295                 : /* string toString (); */
     296            1238 : NS_IMETHODIMP XPCJSStackFrame::ToString(char **_retval)
     297                 : {
     298            1238 :     const char* frametype = IsJSFrame() ? "JS" : "native";
     299            1238 :     const char* filename = mFilename ? mFilename : "<unknown filename>";
     300            1238 :     const char* funname = mFunname ? mFunname : "<TOP_LEVEL>";
     301                 :     static const char format[] = "%s frame :: %s :: %s :: line %d";
     302                 :     int len = sizeof(char)*
     303            2476 :                 (strlen(frametype) + strlen(filename) + strlen(funname)) +
     304            2476 :               sizeof(format) + 6 /* space for lineno */;
     305                 : 
     306            1238 :     char* buf = (char*) nsMemory::Alloc(len);
     307            1238 :     if (!buf)
     308               0 :         return NS_ERROR_OUT_OF_MEMORY;
     309                 : 
     310            1238 :     JS_snprintf(buf, len, format, frametype, filename, funname, mLineno);
     311            1238 :     *_retval = buf;
     312            1238 :     return NS_OK;
     313                 : }
     314                 : 

Generated by: LCOV version 1.7