14 static char sys_timestring[128];
15 char *Sys_TimeString(const char *timeformat)
17 time_t mytime = time(NULL);
20 localtime_s(&mytm, &mytime);
21 strftime(sys_timestring, sizeof(sys_timestring), timeformat, &mytm);
23 strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
25 return sys_timestring;
28 #if defined(__linux__) || defined(MACOSX)
31 static void Sys_Backtrace(void)
34 int n = backtrace(tracebuf, sizeof(tracebuf) / sizeof(*tracebuf));
35 char **trace = backtrace_symbols(tracebuf, n);
37 Con_Printf("Backtrace:\n");
38 for(i = 0; i < n; ++i)
39 Con_Printf("%2d: %s\n", i, trace[i]);
44 // http://gcc.gnu.org/ml/java/2002-04/msg00331.html
45 int backtrace (void **__array, int __size)
47 register void *_ebp __asm__ ("ebp");
48 register void *_esp __asm__ ("esp");
51 for (rfp = *(unsigned int**)_ebp; rfp && i < __size; rfp = *(unsigned int **)rfp)
53 int diff = *rfp - (unsigned int)rfp;
54 if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0)
56 __array[i++] = (void*)(rfp[1]-4);
61 static void Sys_Backtrace(void)
64 int n = backtrace(tracebuf, sizeof(tracebuf) / sizeof(*tracebuf));
65 char **trace = backtrace_symbols(tracebuf, n);
67 Con_Printf("Backtrace:\n");
68 for(i = 0; i < n; ++i)
69 Con_Printf("%2d: %s\n", i, trace[i]);
76 static qboolean Sys_PrintSymbol(void *addr)
81 Con_Printf("%p: File %s Function %s Offset 0x%x\n", addr, da.dli_fname ? da.dli_fname : "(unknown)", da.dli_sname ? da.dli_sname : "(unknown)", (unsigned int)(addr - da.dli_saddr));
82 if(da.dli_sname && (!strcmp(da.dli_sname, "main") || !strcmp(da.dli_sname, "Sys_Backtrace")))
87 void Sys_Backtrace(void) // this function must not be static so gcc can export it
89 // this way of backtracing may potentially crash, but seems reliable enough for me
93 Con_Printf("Backtrace:\n");
94 if(Sys_PrintSymbol((void *) Sys_Backtrace)) // this detects whether dladdr works, and is also nice output
95 for(i = 1; i < 2047; ++i)
96 if(Sys_PrintSymbol(a[i]))
108 extern qboolean host_shuttingdown;
109 void Sys_Quit (int returnvalue)
111 if (COM_CheckParm("-profilegameonly"))
112 Sys_AllowProfiling(false);
113 host_shuttingdown = true;
120 #if defined(__linux__) || defined(__FreeBSD__)
127 void Sys_AllowProfiling(qboolean enable)
129 #if defined(__linux__) || defined(__FreeBSD__)
136 ===============================================================================
140 ===============================================================================
143 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
146 const dllfunction_t *func;
147 dllhandle_t dllhandle = 0;
154 #ifdef PREFER_PRELOAD
155 dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
158 for (func = fcts; func && func->name != NULL; func++)
159 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
164 Con_DPrintf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]);
173 for (func = fcts; func && func->name != NULL; func++)
174 *func->funcvariable = NULL;
176 // Try every possible name
177 Con_DPrintf ("Trying to load library...");
178 for (i = 0; dllnames[i] != NULL; i++)
180 Con_DPrintf (" \"%s\"", dllnames[i]);
182 dllhandle = LoadLibrary (dllnames[i]);
184 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
190 // see if the names can be loaded relative to the executable path
191 // (this is for Mac OSX which does not check next to the executable)
192 if (!dllhandle && strrchr(com_argv[0], '/'))
194 char path[MAX_OSPATH];
195 strlcpy(path, com_argv[0], sizeof(path));
196 strrchr(path, '/')[1] = 0;
197 for (i = 0; dllnames[i] != NULL; i++)
199 char temp[MAX_OSPATH];
200 strlcpy(temp, path, sizeof(temp));
201 strlcat(temp, dllnames[i], sizeof(temp));
202 Con_DPrintf (" \"%s\"", temp);
204 dllhandle = LoadLibrary (temp);
206 dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
216 Con_DPrintf(" - failed.\n");
220 Con_DPrintf(" - loaded.\n");
222 // Get the function adresses
223 for (func = fcts; func && func->name != NULL; func++)
224 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
226 Con_DPrintf ("Missing function \"%s\" - broken library!\n", func->name);
227 Sys_UnloadLibrary (&dllhandle);
238 void Sys_UnloadLibrary (dllhandle_t* handle)
241 if (handle == NULL || *handle == NULL)
245 FreeLibrary (*handle);
254 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
258 return (void *)GetProcAddress (handle, name);
260 return (void *)dlsym (handle, name);