FreeRDP
execinfo/debug.c
1 
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 
25 #include <execinfo.h>
26 
27 #include "debug.h"
28 
29 typedef struct
30 {
31  void** buffer;
32  size_t max;
33  size_t used;
34 } t_execinfo;
35 
36 void winpr_execinfo_backtrace_free(void* buffer)
37 {
38  t_execinfo* data = (t_execinfo*)buffer;
39  if (!data)
40  return;
41 
42  free(data->buffer);
43  free(data);
44 }
45 
46 void* winpr_execinfo_backtrace(DWORD size)
47 {
48  t_execinfo* data = calloc(1, sizeof(t_execinfo));
49 
50  if (!data)
51  return NULL;
52 
53  data->buffer = calloc(size, sizeof(void*));
54 
55  if (!data->buffer)
56  {
57  free(data);
58  return NULL;
59  }
60 
61  assert(size <= INT32_MAX);
62  const int rc = backtrace(data->buffer, (int)size);
63  if (rc < 0)
64  {
65  free(data);
66  return NULL;
67  }
68  data->max = size;
69  data->used = (size_t)rc;
70  return data;
71 }
72 
73 char** winpr_execinfo_backtrace_symbols(void* buffer, size_t* used)
74 {
75  t_execinfo* data = (t_execinfo*)buffer;
76  if (used)
77  *used = 0;
78 
79  if (!data)
80  return NULL;
81 
82  if (used)
83  *used = data->used;
84 
85  assert(data->used < INT32_MAX);
86  return backtrace_symbols(data->buffer, (int)data->used);
87 }
88 
89 void winpr_execinfo_backtrace_symbols_fd(void* buffer, int fd)
90 {
91  t_execinfo* data = (t_execinfo*)buffer;
92 
93  if (!data)
94  return;
95 
96  assert(data->used <= INT32_MAX);
97  backtrace_symbols_fd(data->buffer, (int)data->used, fd);
98 }