Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
win/afw_os.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework OS Specific Functions for Windows
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 /* Min windows 7. */
15 #define NTDDI_VERSION 0x06010000
16 #define _WIN32_WINNT 0x0601
17 
18 #include "afw.h"
19 #include "Dbghelp.h"
20 
21 static const afw_utf8_t
22 impl_s_no_memory_for_backtrace =
23  AFW_UTF8_LITERAL("No memory for backtrace");
24 
25 static const afw_utf8_t
26 impl_dso_suffix =
27  AFW_UTF8_LITERAL(".dll");
28 
29 static const afw_utf8_t
30 impl_s_os =
31  AFW_UTF8_LITERAL("os");
32 
33 
34 #ifdef __EXAMPLE_FOR_CREATING_WITH_CB_WRAPPER_
35 static const afw_object_t *
36 impl_get_<objectId>_object_cb(
37  void *data,
38  const afw_pool_t *p,
39  afw_xctx_t *xctx)
40 {
41  const afw_object_t *result;
42 
43  /* Construct system specific object. */
44  result = afw_object_create(p, xctx);
46  &afw_s_afw,
47  &afw_s__AdaptiveSystemInfo_,
48  &afw_s_<objectId>,
49  xctx);
50  afw_object_meta_set_read_only(result, xctx);
52  result, &afw_s_description,
53  "<objectId> description.",
54  xctx);
55 
56  /* Add in system specific info properties. */
58  afw_object_set_property_from_utf8_z(result,
59  &impl_s_os,
60  "windows",
61  xctx);
62 
64  result, &impl_s_os, &afw_s_label,
65  "OS",
66  xctx);
67 
69  result, &impl_s_os, &afw_s_description,
70  "Operating system.",
71  xctx);
72 
73  /* Return result. */
74  return result;
75 }
76 
77 #endif /* __EXAMPLE_FOR_CREATING_WITH_CB_WRAPPER_ */
78 
79 static const afw_object_t *
80 impl_create_general_object(afw_xctx_t *xctx)
81 {
82  const afw_object_t *result;
83 
84  /* Construct system specific object. */
85  result = afw_object_create(xctx->p, xctx);
87  &afw_s_afw,
88  &afw_s__AdaptiveSystemInfo_,
89  &afw_s_general,
90  xctx);
91  afw_object_meta_set_read_only(result, xctx);
93  result, &afw_s_description,
94  "General systems info.",
95  xctx);
96 
97  /* Add in system specific info properties. */
100  &impl_s_os,
101  "windows",
102  xctx);
103 
105  result, &impl_s_os, &afw_s_label,
106  "OS",
107  xctx);
108 
110  result, &impl_s_os, &afw_s_description,
111  "Operating system.",
112  xctx);
113 
116  &afw_s_S, 1923, 12, 31, -4, 0, xctx);
117 
119  &afw_s_N, 1923, 12, 31, 12, 30, 14, 999999, -4, 0, xctx);
120 
122  &afw_s_M, 12, 30, 14, 999999, -4, 0, xctx);
123 
125  &afw_s_B, true, 12, 34, 56, 78, 90, xctx);
126 
128  &afw_s_L, false, 12, 34, 56, 78, 90, xctx);
129 
131  &afw_s_SS, true, 100, 200, xctx);
132 
134  &afw_s_NS, false, 100, 200, xctx);
135 
136  /* Return result. */
137  return result;
138 }
139 
140 
141 
142 
143 /* afw_os environment initialize */
144 AFW_DEFINE(void)
146 {
147  const afw_log_factory_t *log_factory;
148  const afw_object_t *object;
149 
150  /* Register factory for os log. */
151  log_factory = afw_os_log_factory_get();
153  &log_factory->log_type,
154  log_factory, xctx);
155 
156  /* Create and set general object. */
157  object = impl_create_general_object(xctx);
158  afw_runtime_env_set_object(object, false, xctx);
159 
160 #ifdef __EXAMPLE_FOR_CREATING_WITH_CB_WRAPPER_
161 
162  /* Set system info object wrapper. */
164  &afw_s__AdaptiveSystemInfo_, &afw_s_<objectId>,
165  impl_get_<objectId>_object_cb, NULL, false, xctx);
166 
167 #endif /* __EXAMPLE_FOR_CREATING_WITH_CB_WRAPPER_ */
168 
169 }
170 
171 
172 /* Return a process id or similar number. */
175 {
176  return GetCurrentProcessId();
177 }
178 
179 
180 /* Return the suffix appended to dso file names for this system. */
181 AFW_DEFINE(const afw_utf8_t *)
183 {
184  return &impl_dso_suffix;
185 }
186 
187 
188 /* Provide a backtrace if possible. */
189 AFW_DEFINE(const afw_utf8_t *)
191  afw_error_code_t code,
192  int max_backtrace,
193  afw_xctx_t *xctx)
194 {
195 
196  afw_utf8_t *result;
197  afw_byte_t *s;
198  afw_size_t len;
199  int wlen;
200 
201  USHORT frames;
202  ULONG max;
203  void *stack[100];
204  unsigned int i;
205  HANDLE process;
206  DWORD error;
207  IMAGEHLP_LINE64 line;
208  const char *filename, *c;
209  char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
210  DWORD dwDisplacement = 0;
211  DWORD64 dwDisplacement64 = 0;
212  PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
213  pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
214  pSymbol->MaxNameLen = MAX_SYM_NAME;
215 
216  process = GetCurrentProcess();
217  SymInitialize(process, NULL, TRUE);
218  SymSetOptions(SYMOPT_LOAD_LINES);
219  line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
220 
221  if (max_backtrace == 0) return NULL;
222  if (max_backtrace < 0) max = 100;
223  else if (max_backtrace > 99) max = 100;
224  else max = max_backtrace + 1;
225 
226  /*
227  * Allocate and initialize memory to hold result.
228  *
229  * Variable len is an approximation of the buffer needed to build the
230  * result. if it is not large enough, the the trace returned will be
231  * truncated to what will fit. The average line size is assumed to
232  * be 100 plus 10 extra lines for other error info.
233  */
234  len = (max + 10) * 100;
235  s = apr_palloc(afw_pool_get_apr_pool(xctx->p), len);
236  if (!s) return &impl_s_no_memory_for_backtrace;
237  result = apr_palloc(afw_pool_get_apr_pool(xctx->p), sizeof(afw_utf8_t));
238  if (!result) return &impl_s_no_memory_for_backtrace;
239  result->s = s;
240 
241  /* Capture backtrace stack. */
242  frames = CaptureStackBackTrace(0, max, stack, NULL);
243 
244  /* Process each frame of stack. */
245  for (i = 1; i < frames; i++) {
246  if (SymGetLineFromAddr64(
247  process,(DWORD64)(stack[i]), &dwDisplacement, &line))
248  {
249  filename = c = line.FileName;
250  if (filename) {
251  for (; *c; c++) if (*c == '\\') filename = c + 1;
252  }
253  else {
254  filename = "";
255  }
256 
257  SymFromAddr(process, (DWORD64)( stack[i] ),
258  &dwDisplacement64, pSymbol);
259 
260  /* Add line for frame. Stop if preallocated string is full. */
261  wlen = snprintf(s, len, "%i: %s:%d %s\n", i - 1,
262  filename, line.LineNumber,
263  (pSymbol->Name) ? pSymbol->Name : "");
264  if (wlen < 0 || (afw_size_t)wlen > len) break;
265  s += wlen;
266  len -= wlen;
267  }
268  else {
269  error = GetLastError();
270  break;
271  }
272  }
273 
274  result->len = s - result->s;
275  return result;
276 }
Adaptive Framework Core API.
AFW_DEFINE(const afw_object_t *)
#define AFW_UTF8_LITERAL(A_STRING)
String literal initializer.
Definition: afw_common.h:582
enum afw_error_code_e afw_error_code_t
unsigned char afw_byte_t
A byte of memory (unsigned).
Definition: afw_common.h:208
apr_uint32_t afw_uint32_t
32-bit unsigned integer.
Definition: afw_common.h:184
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
void afw_environment_register_log_type(const afw_utf8_t *log_type, const afw_log_factory_t *log_factory, afw_xctx_t *xctx)
Register an log factory.
#define afw_object_meta_set_property_from_utf8_z(instance, property_name, string_z, xctx)
Set a string property in the meta of an object from utf8_z.
afw_object_meta_set_read_only(const afw_object_t *instance, afw_xctx_t *xctx)
Set object's meta to indicate object is read-only.
#define afw_object_meta_set_property_type_property_from_utf8_z(instance, property_name, property_type_property_name, string_z, xctx)
Set a property type string property for a property in the meta of an object from utf8_z.
afw_object_meta_set_ids(const afw_object_t *instance, const afw_utf8_t *adaptor_id, const afw_utf8_t *object_type_id, const afw_utf8_t *object_id, afw_xctx_t *xctx)
Set object's ids.
afw_object_set_property_as_date_from_parts(const afw_object_t *instance, const afw_utf8_t *property_name, int year, int month, int day, int tz_hours_offset, int tz_minutes_offset, afw_xctx_t *xctx)
Set a date property from parts.
Definition: afw_object.c:66
afw_object_set_property_as_dayTimeDuration_from_parts(const afw_object_t *instance, const afw_utf8_t *property_name, afw_boolean_t is_positive, int days, int hours, int minutes, int seconds, int microseconds, afw_xctx_t *xctx)
Set a dayTimeDuration property from parts.
Definition: afw_object.c:121
#define afw_object_create(p, xctx)
Create an empty unmanaged object in memory.
Definition: afw_object.h:948
afw_object_set_property_as_dateTime_from_parts(const afw_object_t *instance, const afw_utf8_t *property_name, int year, int month, int day, int hour, int minute, int second, int microsecond, int tz_hours_offset, int tz_minutes_offset, afw_xctx_t *xctx)
Set a dateTime property from parts.
Definition: afw_object.c:91
afw_object_set_property_as_time_from_parts(const afw_object_t *instance, const afw_utf8_t *property_name, int hour, int minute, int second, int microsecond, int tz_hours_offset, int tz_minutes_offset, afw_xctx_t *xctx)
Set a time property from parts.
Definition: afw_object.c:146
afw_object_set_property_as_yearMonthDuration_from_parts(const afw_object_t *instance, const afw_utf8_t *property_name, afw_boolean_t is_positive, int years, int months, afw_xctx_t *xctx)
Set a yearMonthDuration property from parts.
Definition: afw_object.c:172
afw_object_set_property_as_string_from_utf8_z(const afw_object_t *instance, const afw_utf8_t *property_name, const afw_utf8_z_t *string_z, afw_xctx_t *xctx)
Set an string property from utf8_z.
Definition: afw_object.c:194
afw_os_environment_initialize(afw_xctx_t *xctx)
afw_os environment initialize
Definition: win/afw_os.c:145
afw_os_backtrace(afw_error_code_t code, int max_backtrace, afw_xctx_t *xctx)
Provide a backtrace if possible.
Definition: win/afw_os.c:190
afw_os_get_dso_suffix()
Return the suffix appended to dso file names for this system.
Definition: win/afw_os.c:182
afw_os_get_pid()
Return a process id or similar number.
Definition: win/afw_os.c:174
const afw_log_factory_t * afw_os_log_factory_get()
Get the factory for OS log.
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
afw_runtime_env_set_object(const afw_object_t *object, afw_boolean_t overwrite, afw_xctx_t *xctx)
Set an object pointer in the environment's runtime objects.
Definition: afw_runtime.c:210
afw_runtime_env_set_object_cb_wrapper(const afw_utf8_t *object_type_id, const afw_utf8_t *object_id, afw_runtime_object_wrapper_p_cb_t callback, void *data, afw_boolean_t overwrite, afw_xctx_t *xctx)
Set environment object accessed via callback.
Definition: afw_runtime.c:240
Interface afw_log_factory public struct.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.