/** * Get the display name and line number of the source file containing the specified address. * * @param process Process handle * @param address Address to find * @param returnedSourceAndLine Returned source code file name with line number */ static void getSourceFileAndLineNumber( HANDLE process, DWORD64 address, std::string* returnedSourceAndLine ) { IMAGEHLP_LINE64 line64; memset( &line64, 0, sizeof(line64) ); line64.SizeOfStruct = sizeof(line64); DWORD displacement32; BOOL ret = SymGetLineFromAddr64( process, address, &displacement32, &line64 ); if ( FALSE == ret ) { returnedSourceAndLine->clear(); return; } std::string filename( line64.FileName ); std::string::size_type start = filename.find( "\\src\\mongo\\" ); if ( start == std::string::npos ) { start = filename.find( "\\src\\third_party\\" ); } if ( start != std::string::npos ) { std::string shorter( "..." ); shorter += filename.substr( start ); filename.swap( shorter ); } static const size_t bufferSize = 32; boost::scoped_array<char> lineNumber( new char[bufferSize] ); _snprintf( lineNumber.get(), bufferSize, "(%u)", line64.LineNumber ); filename += lineNumber.get(); returnedSourceAndLine->swap( filename ); }
static void make_recursive_dir(const SkString& path) { if (sk_exists(path.c_str())) { return; } const char* pathStr = path.c_str(); int last = (int) path.size(); do { while (last > 0 && pathStr[--last] != PATH_SLASH[0]) ; SkASSERT(last > 0); SkString shorter(pathStr, last); if (sk_mkdir(shorter.c_str())) { break; } } while (true); do { while (last < (int) path.size() && pathStr[++last] != PATH_SLASH[0]) ; SkString shorter(pathStr, last); SkAssertResult(sk_mkdir(shorter.c_str())); } while (last < (int) path.size()); }
int main() { const char *d = "1380994682285"; std::stringstream is(d); // ISO C++98 does not support long long // ... it should be uint64_t, but supported in C++11... // or rather, just long but be sure you compile for 64bit std::cout << sizeof (long) << "\n"; // very likely 4 on 32bit std::cout << sizeof (long long) << "\n"; // 8 unsigned long long epoch64; // if you need strict ISO C++98 conformance, you must use unsigned long, // see below. is >> epoch64; std::cout << epoch64 << "\n"; epoch64 /= 1000; // this goes ok (if epoch64 is big enough), since /1000 makes it // small enough to go into time_t time_t t = epoch64; std::cout << std::ctime(&t) << "\n"; // if you use unsigned long for epoch, and it turns out to // be sizeof (unsigned long) == 4, then you get the wrong output. // Then, you must make it shorter before to convert it: std::string shorter(d, strlen(d)-3); // assert(strlen(d)>3) std::stringstream is2(shorter); unsigned long epoch32; // I am on a 32bit machine is2 >> epoch32; t = epoch32; std::cout << epoch32 << "\n"; std::cout << std::ctime(&t) << "\n"; return 0; }
void printStackTrace( std::ostream &os ) { HANDLE process = GetCurrentProcess(); BOOL ret = SymInitialize( process, NULL, TRUE ); if ( ret == FALSE ) { DWORD dosError = GetLastError(); os << "Stack trace failed, SymInitialize failed with error " << std::dec << dosError << std::endl; return; } DWORD options = SymGetOptions(); options |= SYMOPT_LOAD_LINES | SYMOPT_FAIL_CRITICAL_ERRORS; SymSetOptions( options ); CONTEXT context; memset( &context, 0, sizeof(context) ); context.ContextFlags = CONTEXT_CONTROL; RtlCaptureContext( &context ); STACKFRAME64 frame64; memset( &frame64, 0, sizeof(frame64) ); #if defined(_M_AMD64) DWORD imageType = IMAGE_FILE_MACHINE_AMD64; frame64.AddrPC.Offset = context.Rip; frame64.AddrFrame.Offset = context.Rbp; frame64.AddrStack.Offset = context.Rsp; #elif defined(_M_IX86) DWORD imageType = IMAGE_FILE_MACHINE_I386; frame64.AddrPC.Offset = context.Eip; frame64.AddrFrame.Offset = context.Ebp; frame64.AddrStack.Offset = context.Esp; #else #error Neither _M_IX86 nor _M_AMD64 were defined #endif frame64.AddrPC.Mode = AddrModeFlat; frame64.AddrFrame.Mode = AddrModeFlat; frame64.AddrStack.Mode = AddrModeFlat; const size_t nameSize = 1024; const size_t symbolRecordSize = sizeof(SYMBOL_INFO) + nameSize; boost::scoped_array<char> symbolBuffer( new char[symbolRecordSize] ); memset( symbolBuffer.get(), 0, symbolRecordSize ); SYMBOL_INFO* symbol = reinterpret_cast<SYMBOL_INFO*>( symbolBuffer.get() ); symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->MaxNameLen = nameSize; std::vector<TraceItem> traceList; TraceItem traceItem; size_t moduleWidth = 0; size_t fileWidth = 0; size_t frameCount = 0; for ( size_t i = 0; i < maxBackTraceFrames; ++i ) { ret = StackWalk64( imageType, process, GetCurrentThread(), &frame64, &context, NULL, NULL, NULL, NULL ); if ( ret == FALSE || frame64.AddrReturn.Offset == 0 ) { frameCount = i; break; } // module (executable) name IMAGEHLP_MODULE64 module; memset ( &module, 0, sizeof(module) ); module.SizeOfStruct = sizeof(module); ret = SymGetModuleInfo64( process, frame64.AddrPC.Offset, &module ); char* moduleName = module.LoadedImageName; char* backslash = strrchr( moduleName, '\\' ); if ( backslash ) { moduleName = backslash + 1; } traceItem.module = moduleName; size_t len = traceItem.module.length(); if ( len > moduleWidth ) { moduleWidth = len; } // source code filename and line number IMAGEHLP_LINE64 line; memset( &line, 0, sizeof(line) ); line.SizeOfStruct = sizeof(line); DWORD displacement32; ret = SymGetLineFromAddr64( process, frame64.AddrPC.Offset, &displacement32, &line ); if ( ret ) { std::string filename( line.FileName ); std::string::size_type start = filename.find( "\\src\\mongo\\" ); if ( start == std::string::npos ) { start = filename.find( "\\src\\third_party\\" ); } if ( start != std::string::npos ) { std::string shorter( "..." ); shorter += filename.substr( start ); traceItem.sourceFile.swap( shorter ); } else { traceItem.sourceFile.swap( filename ); } len = traceItem.sourceFile.length() + 3; traceItem.lineNumber = line.LineNumber; if ( traceItem.lineNumber < 10 ) { len += 1; } else if ( traceItem.lineNumber < 100 ) { len += 2; } else if ( traceItem.lineNumber < 1000 ) { len += 3; } else if ( traceItem.lineNumber < 10000 ) { len += 4; } else { len += 5; } traceItem.sourceLength = len; if ( len > fileWidth ) { fileWidth = len; } } else { traceItem.sourceFile.clear(); traceItem.sourceLength = 0; } // symbol name and offset from symbol DWORD64 displacement; ret = SymFromAddr( process, frame64.AddrPC.Offset, &displacement, symbol ); if ( ret ) { traceItem.symbol = symbol->Name; traceItem.instructionOffset = displacement; } else { traceItem.symbol = "???"; traceItem.instructionOffset = 0; } // add to list traceList.push_back( traceItem ); } SymCleanup( process ); // print list ++moduleWidth; ++fileWidth; for ( size_t i = 0; i < frameCount; ++i ) { os << traceList[i].module << " "; size_t width = traceList[i].module.length(); while ( width < moduleWidth ) { os << " "; ++width; } if ( traceList[i].sourceFile.length() ) { os << traceList[i].sourceFile << "(" << std::dec << traceList[i].lineNumber << ") "; } width = traceList[i].sourceLength; while ( width < fileWidth ) { os << " "; ++width; } os << traceList[i].symbol << "+0x" << std::hex << traceList[i].instructionOffset; os << std::endl; } }
bool setBasicOpt(CURL* curl_handle, MemoryStruct* chunkP, HeaderStruct* headerChunkP, const char* linkP, struct curl_slist* headerListP) { if(curl_easy_setopt(curl_handle, CURLOPT_URL, linkP)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_URL failed"); return false; } /* reading response to memory instead of file */ if(curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeMemoryCallback)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_WRITEFUNCTION failed"); return false; } if(curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writeHeaderCallback)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_HEADERFUNCTION failed"); return false; } if(curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) chunkP)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_WRITEDATA failed"); return false; } if(curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void *) headerChunkP)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_WRITEHEADER failed"); return false; } if(curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headerListP)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_HTTPHEADER failed"); return false; } /* some servers don't like requests that are made without a user-agent field, so we provide one */ if(curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "rpa/1.0")!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_USERAGENT failed"); return false; } if(curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_USERAGENT failed"); return false; } saveCertFile(certificateFilePath_, CA_CERTIFICATES); LOGD("curl_easy_setopt CURLOPT_CAINFO %s", certificateFilePath_); if(curl_easy_setopt(curl_handle, CURLOPT_CAINFO, certificateFilePath_)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_CAINFO failed"); return false; } LOGD("curl_easy_setopt CURLOPT_CAPATH %s", certificatePath_); if(curl_easy_setopt(curl_handle, CURLOPT_CAPATH, certificatePath_)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_CAPATH failed"); return false; } long int se_connection_timeout=120L; // timeout after 120 seconds #ifdef __DEBUG curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION, debug_function); if(strncmp(linkP, NONEXISTENT_TEST_URL, shorter(strlen(NONEXISTENT_TEST_URL), strlen(linkP)))==0) { se_connection_timeout=3L; // reducing the connection timeout for testing purposes MAX_ATTEMPTS=1; // this is for testint code, we are using nonexitent url here so no unncessary attempts LOGD("setBasicOpt timeout set to %ld", se_connection_timeout); } #endif if(curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, se_connection_timeout)!=CURLE_OK) { LOGE("curl_easy_setopt CURLOPT_TIMEOUT failed"); return false; } /** libcurl uses the http_proxy and https_proxy environment variables for proxy settings. That variable is set in the OS specific wrapper. These are left here in order to make this comment earier to be found in searches. curl_easy_setopt(curl_handle,CURLOPT_PROXY, "http://proxyaddress"); curl_easy_setopt(curl_handle,CURLOPT_PROXYPORT, "read_proxy_port"); curl_easy_setopt(curl_handle,CURLOPT_PROXYUSERNAME, "read_proxy_username"); curl_easy_setopt(curl_handle,CURLOPT_PROXYPASSWORD, "read_proxy_password"); */ return true; }