Esempio n. 1
0
void TraceDumpToFile()
{
    if (TraceFile != nullptr)
    {
        TGuard Guard(TracingCriticalSection);

        DWORD Written;

        TDateTime N = Now();
#ifdef TRACE_IN_MEMORY_NO_FORMATTING
        DWORD Ticks = GetTickCount();
#endif

        const UnicodeString TimestampFormat = L"hh:mm:ss.zzz";
        UnicodeString TimeString = FormatDateTime(TimestampFormat, N);

        UTF8String Buffer = UTF8String(
                                FORMAT("[%s] Dumping in-memory tracing =================================\n",
                                       (TimeString)));
        WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr);

        TTracesInMemory::const_iterator i = TracesInMemory.begin();
        while (i != TracesInMemory.end())
        {
#ifdef TRACE_IN_MEMORY_NO_FORMATTING
            const wchar_t * SourceFile = i->SourceFile;
            const wchar_t * Slash = wcsrchr(SourceFile, L'\\');
            if (Slash != nullptr)
            {
                SourceFile = Slash + 1;
            }

            TimeString =
                FormatDateTime(TimestampFormat,
                               IncMilliSecond(N, -static_cast<int>(Ticks - i->Ticks)));
            Buffer = UTF8String(FORMAT(L"[%s] [%.4X] [%s:%d:%s] %s\n",
                                       (TimeString, int(i->Thread), SourceFile,
                                        i->Line, i->Func, i->Message)));
            WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr);
#else
            WriteFile(TraceFile, i->Message.c_str(), i->Message.Length(), &Written, nullptr);
#endif
            ++i;
        }
        TracesInMemory.clear();

        TimeString = FormatDateTime(TimestampFormat, Now());
        Buffer = UTF8String(
                     FORMAT("[%s] Done in-memory tracing =================================\n",
                            (TimeString)));
        WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr);
    }
}
Esempio n. 2
0
void TSessionLog::DoAddToSelf(TLogLineType Type, UnicodeString ALine)
{
  if (LogToFile()) { try
  {
    if (FLogger == nullptr)
    {
      OpenLogFile();
    }

    if (FLogger != nullptr)
    {
      UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now());
      UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + TrimRight(ALine)) + "\r\n";
#if 0
      for (intptr_t Index = 1; Index <= UtfLine.Length(); Index++)
      {
        if ((UtfLine[Index] == '\n') &&
            (UtfLine[Index - 1] != '\r'))
        {
          UtfLine.Insert(L'\r', Index);
        }
      }
#endif // #if 0
      intptr_t ToWrite = UtfLine.Length();
      CheckSize(ToWrite);
      FCurrentFileSize += FLogger->Write(UtfLine.c_str(), ToWrite);
    }}
    catch (...)
    {
      // TODO: log error
      DEBUG_PRINTF("TSessionLog::DoAddToSelf: error");
    }
  }
}
Esempio n. 3
0
//---------------------------------------------------------------------------
void TActionLog::Add(const UnicodeString & Line)
{
  assert(FConfiguration);
  if (FLogging)
  {
    try
    {
      TGuard Guard(FCriticalSection);
      if (FFile == nullptr)
      {
        OpenLogFile();
      }

      if (FFile != nullptr)
      {
        UTF8String UtfLine = UTF8String(Line);
        fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
        fwrite("\n", 1, 1, (FILE *)FFile);
      }
    }
    catch (Exception &E)
    {
      // We failed logging, turn it off and notify user.
      FConfiguration->SetLogActions(false);
      try
      {
        throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
      }
      catch (Exception &E)
      {
        FUI->HandleExtendedException(&E);
      }
    }
  }
}
Esempio n. 4
0
BitmapPtr BitmapLoader::load(const UTF8String& sFName, PixelFormat pf) const
{
    AVG_ASSERT(s_pBitmapLoader != 0);
    GError* pError = 0;
    GdkPixbuf* pPixBuf;
    {
        ScopeTimer timer(GDKPixbufProfilingZone);
        pPixBuf = gdk_pixbuf_new_from_file(sFName.c_str(), &pError);
    }
    if (!pPixBuf) {
        string sErr = pError->message;
        g_error_free(pError);
        throw Exception(AVG_ERR_FILEIO, sErr);
    }
    IntPoint size = IntPoint(gdk_pixbuf_get_width(pPixBuf), 
            gdk_pixbuf_get_height(pPixBuf));
    
    PixelFormat srcPF;
    if (gdk_pixbuf_get_has_alpha(pPixBuf)) {
        srcPF = R8G8B8A8;
    } else {
        srcPF = R8G8B8;
    }
    if (pf == NO_PIXELFORMAT) {
        if (m_bBlueFirst) {
            if (srcPF == R8G8B8A8) {
                pf = B8G8R8A8;
            } else if (srcPF == R8G8B8) {
                pf = B8G8R8X8;
            }
        } else {
            if (srcPF == R8G8B8A8) {
                pf = R8G8B8A8;
            } else if (srcPF == R8G8B8) {
                pf = R8G8B8X8;
            }
        }
    }
    BitmapPtr pBmp(new Bitmap(size, pf, sFName));
    {
        ScopeTimer timer(ConvertProfilingZone);

        int stride = gdk_pixbuf_get_rowstride(pPixBuf);
        guchar* pSrc = gdk_pixbuf_get_pixels(pPixBuf);
        BitmapPtr pSrcBmp(new Bitmap(size, srcPF, pSrc, stride, false));
        {
            ScopeTimer timer(RGBFlipProfilingZone);
            if (pixelFormatIsBlueFirst(pf) != pixelFormatIsBlueFirst(srcPF)) {
                FilterFlipRGB().applyInPlace(pSrcBmp);
            }
        }
        pBmp->copyPixels(*pSrcBmp);
    }
    g_object_unref(pPixBuf);
    return pBmp;
}
Esempio n. 5
0
Pixel32 colorStringToColor(const UTF8String& s)
{
    int r, g, b;
    int numChars;
    int numItems = sscanf(s.c_str(), "%2x%2x%2x%n", &r, &g, &b, &numChars);
    if (s.length() != 6 || numChars != 6 || numItems != 3) {
        throw(Exception (AVG_ERR_INVALID_ARGS, "colorstring cannot be parsed."));
    }
    return Pixel32(r, g, b);
}
Esempio n. 6
0
//---------------------------------------------------------------------------
TKeyType KeyType(UnicodeString FileName)
{
  assert(ktUnopenable == SSH_KEYTYPE_UNOPENABLE);
  assert(ktSSHCom == SSH_KEYTYPE_SSHCOM);
  UTF8String UtfFileName = UTF8String(FileName);
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  TKeyType Result = (TKeyType)key_type(KeyFile);
  filename_free(KeyFile);
  return Result;
}
Esempio n. 7
0
TKeyType KeyType(const UnicodeString & AFileName)
{
  assert(ktUnopenable == SSH_KEYTYPE_UNOPENABLE);
  assert(ktSSHCom == SSH_KEYTYPE_SSHCOM);
  UTF8String UtfFileName = UTF8String(AFileName);
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  TKeyType Result = static_cast<TKeyType>(key_type(KeyFile));
  filename_free(KeyFile);
  return Result;
}
Esempio n. 8
0
TKeyType GetKeyType(const UnicodeString & AFileName)
{
  DebugAssert(ktUnopenable == SSH_KEYTYPE_UNOPENABLE);
#ifndef __linux__
  DebugAssert(ktSSH2PublicOpenSSH == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH);
#endif
  UTF8String UtfFileName = UTF8String(::ExpandEnvironmentVariables(AFileName));
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  TKeyType Result = static_cast<TKeyType>(key_type(KeyFile));
  filename_free(KeyFile);
  return Result;
}
Esempio n. 9
0
TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase)
{
  UTF8String UtfFileName = UTF8String(::ExpandEnvironmentVariables(FileName));
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  AnsiString AnsiPassphrase = AnsiString(Passphrase);
  struct ssh2_userkey * Ssh2Key = nullptr;
  const char * ErrorStr = nullptr;

  switch (KeyType)
  {
    case ktSSH2:
      Ssh2Key = ssh2_load_userkey(KeyFile, (char *)AnsiPassphrase.c_str(), &ErrorStr);
      break;

    case ktOpenSSHPEM:
    case ktOpenSSHNew:
    case ktSSHCom:
      Ssh2Key = import_ssh2(KeyFile, KeyType, (char *)AnsiPassphrase.c_str(), &ErrorStr);
      break;

    default:
      DebugFail();
      break;
  }

  Shred(AnsiPassphrase);

  if (Ssh2Key == nullptr)
  {
    UnicodeString Error = UnicodeString(ErrorStr);
    // While theoretically we may get "unable to open key file" and
    // so we should check system error code,
    // we actully never get here unless we call KeyType previously
    // and handle ktUnopenable accordingly.
    throw Exception(Error);
  }
  else if (Ssh2Key == SSH2_WRONG_PASSPHRASE)
  {
    throw Exception(LoadStr(AUTH_TRANSL_WRONG_PASSPHRASE));
  }

  return reinterpret_cast<TPrivateKey *>(Ssh2Key);
}
Esempio n. 10
0
void PythonLogSink::logMessage(const tm* pTime, unsigned millis,
        const string& category, unsigned severity, const UTF8String& sMsg)
{
    string sSeverity = boost::to_lower_copy(string(Logger::severityToString(severity)));
    PyObject * extra = PyDict_New();
    PyObject * pyCat = PyString_FromString(category.c_str());

    PyDict_SetItemString(extra, "category", pyCat);

    PyObject * pyMsg = PyString_FromString(sMsg.c_str());
    PyObject * args = PyTuple_New(1);
    PyObject * kwargs = PyDict_New();
    PyDict_SetItemString(kwargs, "extra", extra);
    PyTuple_SetItem(args, 0, pyMsg);

    PyObject_Call(PyObject_GetAttrString(m_pyLogger, sSeverity.c_str()), args, kwargs);

    Py_DECREF(extra);
    Py_DECREF(pyCat);
    Py_DECREF(args);
    Py_DECREF(kwargs);
}
Esempio n. 11
0
void DoTrace(const wchar_t * SourceFile, const wchar_t * Func,
             uintptr_t Line, const wchar_t * Message)
{
    DebugAssert(IsTracing);

    UnicodeString TimeString;
    // DateTimeToString(TimeString, L"hh:mm:ss.zzz", Now());
    TODO("use Format");
    const wchar_t * Slash = wcsrchr(SourceFile, L'\\');
    if (Slash != nullptr)
    {
        SourceFile = Slash + 1;
    }
    UTF8String Buffer = UTF8String(FORMAT(L"[%s] [%.4X] [%s:%d:%s] %s\n",
                                          TimeString.c_str(), int(::GetCurrentThreadId()), SourceFile,
                                          Line, Func, Message));
#ifdef TRACE_IN_MEMORY
    if (TracingCriticalSection != nullptr)
    {
        TTraceInMemory TraceInMemory;
        TraceInMemory.Message = Buffer;

        TGuard Guard(TracingCriticalSection);

        if (TracesInMemory.capacity() == 0)
        {
            TracesInMemory.reserve(100000);
            TThreadID ThreadID;
            StartThread(nullptr, 0, TraceThreadProc, nullptr, 0, ThreadID);
        }

        TracesInMemory.push_back(TraceInMemory);
    }
#else
    DWORD Written;
    WriteFile(TraceFile, Buffer.c_str(), static_cast<DWORD>(Buffer.Length()), &Written, nullptr);
#endif TRACE_IN_MEMORY
}
Esempio n. 12
0
void SaveKey(TKeyType KeyType, const UnicodeString & FileName,
  const UnicodeString & Passphrase, TPrivateKey * PrivateKey)
{
  UTF8String UtfFileName = UTF8String(::ExpandEnvironmentVariables(FileName));
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  struct ssh2_userkey * Ssh2Key = reinterpret_cast<struct ssh2_userkey *>(PrivateKey);
  AnsiString AnsiPassphrase = AnsiString(Passphrase);
  const char * PassphrasePtr = (AnsiPassphrase.IsEmpty() ? nullptr : AnsiPassphrase.c_str());
  switch (KeyType)
  {
    case ktSSH2:
      if (!ssh2_save_userkey(KeyFile, Ssh2Key, (char *)PassphrasePtr))
      {
        int Error = errno;
        throw EOSExtException(FMTLOAD(KEY_SAVE_ERROR, FileName.c_str()), Error);
      }
      break;

    default:
      DebugFail();
      break;
  }
}
Esempio n. 13
0
//---------------------------------------------------------------------------
void TSessionLog::DoAddToSelf(TLogLineType AType, const UnicodeString & ALine)
{
  if (FTopIndex < 0)
  {
    FTopIndex = 0;
  }

  TStringList::AddObject(ALine, static_cast<TObject *>(reinterpret_cast<void *>(static_cast<size_t>(AType))));

  FLoggedLines++;

  if (LogToFile())
  {
    if (FFile == nullptr)
    {
      OpenLogFile();
    }

    if (FFile != nullptr)
    {
#if defined(__BORLANDC__)
      UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now());
      UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + Line + "\n");
      fwrite(UtfLine.c_str(), UtfLine.Length(), 1, (FILE *)FFile);
#else
      unsigned short Y, M, D, H, N, S, MS;
      TDateTime DateTime = Now();
      DateTime.DecodeDate(Y, M, D);
      DateTime.DecodeTime(H, N, S, MS);
      UnicodeString dt = FORMAT(L" %04d-%02d-%02d %02d:%02d:%02d.%03d ", Y, M, D, H, N, S, MS);
      UnicodeString Timestamp = dt;
      UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[AType]) + Timestamp + ALine + "\n");
      fprintf_s(static_cast<FILE *>(FFile), "%s", const_cast<char *>(AnsiString(UtfLine).c_str()));
#endif
    }
  }
}
Esempio n. 14
0
bool IsKeyEncrypted(TKeyType KeyType, const UnicodeString & FileName, UnicodeString & Comment)
{
  UTF8String UtfFileName = UTF8String(::ExpandEnvironmentVariables(FileName));
  Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  bool Result;
  char * CommentStr = nullptr;
  switch (KeyType)
  {
    case ktSSH2:
      Result = (ssh2_userkey_encrypted(KeyFile, &CommentStr) != 0);
      break;

    case ktOpenSSHPEM:
    case ktOpenSSHNew:
    case ktSSHCom:
      Result = (import_encrypted(KeyFile, KeyType, &CommentStr) != 0);
      break;

    default:
      DebugFail();
      Result = false;
      break;
  }

  if (CommentStr != nullptr)
  {
    Comment = UnicodeString(AnsiString(CommentStr));
    // ktOpenSSH has no comment, PuTTY defaults to file path
    if (Comment == FileName)
    {
      Comment = base::ExtractFileName(FileName, false);
    }
    sfree(CommentStr);
  }

  return Result;
}
Esempio n. 15
0
   void VTune_RegisterMethod(AvmCore* core, JITCodeInfo* inf) 
   {       
		// assume no method inlining so start/end of JIT code gen = method start/end
       uintptr startAt = inf->startAddr;
       uintptr endAt = inf->endAddr;
		uint32 methodSize = endAt - startAt;

       Stringp name = inf->method->format(core);
       int idx[4];
       bool hasClass = locateNames(core, name, idx);

		// register the method
		iJIT_Method_Load ML;
       Stringp mname = name->substring(idx[2],idx[3]);
		VMPI_memset(&ML, 0, sizeof(iJIT_Method_Load));
       ML.method_id = (inf->vtune) ? inf->vtune->method_id : iJIT_GetNewMethodID();
       ML.method_name = string2char(core, mname);
		ML.method_load_address = (void *)startAt;	// virtual address of that method  - This determines the method range for the iJVM_EVENT_TYPE_ENTER/LEAVE_METHOD_ADDR events
		ML.method_size = methodSize;	// Size in memory - Must be exact

		// md position / line number table
       SortedIntMap<LineNumberRecord*>* tbl = &inf->lineNumTable;
		int size = tbl->size();
		LineNumberInfo* lines = 0;
		if (size)
		{
			int bsz = size*sizeof(LineNumberInfo);
			lines = (LineNumberInfo*) malloc(bsz);
			VMPI_memset(lines, 0, bsz);
		}

       String* fileName = 0;
       int at = 0;
       for(int i=0; i<size; i++) 
		{
			sintptr mdPos = tbl->keyAt(i);
			LineNumberRecord* entry = tbl->at(i);
           if (entry->filename && entry->lineno)
           {
               if (!fileName) fileName = entry->filename;

               // @todo file name should be part of the lines[] record, no?
               lines[at].LineNumber = entry->lineno;
               lines[at].Offset = mdPos - startAt;
               at++;
           }
       }

       // @todo hack for vtune since it can't process multiple records with the same method name 
       if (inf->sid>0 && at>0) {
           mname = core->concatStrings(mname,core->newString("_L"));
           mname = core->concatStrings(mname,core->intToString(lines[0].LineNumber));
           mname = core->concatStrings(mname,core->newString("_"));
           mname = core->concatStrings(mname,core->intToString(inf->sid));
           if (ML.method_name) free(ML.method_name);
           ML.method_name = string2char(core,mname);
       }

       ML.line_number_table = lines;   // Pointer to the begining of the line numbers info array
       ML.line_number_size = at;       // Line Table size in number of entries - Zero if none

       UTF8String* utf = ( fileName ) ? fileName->toUTF8String() : core->kEmptyString->toUTF8String();
		
		ML.class_id = 0;                // uniq class ID
       ML.class_file_name = (hasClass) ? string2char(core,name->substring(idx[0],idx[1])) : 0;     // class file name 
		ML.source_file_name = (char *)(malloc((utf->length()+3)*sizeof(char)));   // +1 for \0 and +2 for wtoc's ()
		wtoc(ML.source_file_name, utf->c_str(), 0);	// source file name 

		ML.user_data = NULL;            // bits supplied by the user for saving in the JIT file...
		ML.user_data_size = 0;          // the size of the user data buffer
		ML.env = iJDE_JittingAPI;

		// DumpVTuneMethodInfo(core, &ML); // Uncommented to debug VTune
		iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &ML);

       if (inf->vtune && core->VTuneStatus == iJIT_CALLGRAPH_ON) 
		{
			MMgc::GCHeap* heap = core->GetGC()->GetGCHeap();
           heap->SetPageProtection(inf->vtune, sizeof (iJIT_Method_NIDS), false, true);   
       }

		// free everything we alloc'd  ( @todo did vtune really copy all the strings?!? )
		if (ML.line_number_table) free(ML.line_number_table);
       if (ML.class_file_name && hasClass) free(ML.class_file_name);
		if (ML.method_name) free(ML.method_name);
		if (ML.source_file_name) free(ML.source_file_name);
	}	
Esempio n. 16
0
const AnsiString & __fastcall AnsiString::operator +=(const UTF8String & rhs)
{
  Data.append(reinterpret_cast<const char *>(rhs.c_str()), rhs.size());
  return *this;
}
Esempio n. 17
0
void TActionLog::Add(UnicodeString Line)
{
  DebugAssert(FConfiguration);
  if (FLogging)
  {
    TGuard Guard(FCriticalSection);
    if (FLogger == nullptr)
    {
      OpenLogFile();
    }

    if (FLogger != nullptr)
    {
      try
      {
        UTF8String UtfLine = UTF8String(Line);
        size_t Written =
          FLogger->Write(UtfLine.c_str(), UtfLine.Length());
        if (Written != static_cast<size_t>(UtfLine.Length()))
        {
          throw ECRTExtException(L"");
        }
        Written =
          FLogger->Write("\n", 1);
        if (Written != 1)
        {
          throw ECRTExtException(L"");
        }
      }
      catch (Exception &E)
      {
//        FCriticalSection.Release();

        // avoid endless loop when trying to close tags when closing log, when logging has failed
        if (!FFailed)
        {
          FFailed = true;
          // We failed logging, turn it off and notify user.
          FConfiguration->SetLogActions(false);
          if (FConfiguration->GetLogActionsRequired())
          {
            throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
          }
          else
          {
            try
            {
              throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
            }
            catch (Exception &E2)
            {
              if (FUI != nullptr)
              {
                FUI->HandleExtendedException(&E2);
              }
            }
          }
        }
      }
    }
  }
}