Debian Codegraph @

mingw-w64-crt/profile/profil.c

raw
1 /* profil.c -- win32 profil.c equivalent
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 /*
12 * This file is taken from Cygwin distribution. Please keep it in sync.
13 * The differences should be within __MINGW32__ guard.
14 */
15
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
18 #endif
19 #include <windows.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 #include <math.h>
24 #include "profil.h"
25
26 #define SLEEPTIME (1000 / PROF_HZ)
27
28 /* global profinfo for profil() call */
29 static struct profinfo prof;
30
31 /* Get the pc for thread THR */
32
33 static size_t
34 get_thrpc (HANDLE thr)
35 {
36 CONTEXT ctx;
37 size_t pc;
38 int res;
39
40 res = SuspendThread (thr);
41 if (res == -1)
42 return (size_t) - 1;
43 ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
44 pc = (size_t) - 1;
45 if (GetThreadContext (thr, &ctx)) {
46 #if defined(_AMD64_) || defined(__x86_64__)
47 pc = ctx.Rip;
48 #elif defined(_ARM_) || defined(__arm__)
49 pc = ctx.Pc;
50 #elif defined(_X86_) || defined(__i386__)
51 pc = ctx.Eip;
52 #endif
53 }
54 ResumeThread (thr);
55 return pc;
56 }
57
58 /* Display cell of profile buffer */
59 #if 0
60 static void
61 print_prof (struct profinfo *p)
62 {
63 printf ("profthr %x\ttarget thr %x\n", p->profthr, p->targthr);
64 printf ("pc: %x - %x\n", p->lowpc, p->highpc);
65 printf ("scale: %x\n", p->scale);
66 return;
67 }
68 #endif
69
70 /* Everytime we wake up use the main thread pc to hash into the cell in the
71 profile buffer ARG. */
72
73 static DWORD CALLBACK profthr_func (LPVOID);
74
75 static DWORD CALLBACK
76 profthr_func (LPVOID arg)
77 {
78 struct profinfo *p = (struct profinfo *) arg;
79 size_t pc, idx;
80
81 for (;;)
82 {
83 pc = (size_t) get_thrpc (p->targthr);
84 if (pc >= p->lowpc && pc < p->highpc)
85 {
86 idx = PROFIDX (pc, p->lowpc, p->scale);
87 p->counter[idx]++;
88 }
89 #if 0
90 print_prof (p);
91 #endif
92 /* Check quit condition, WAIT_OBJECT_0 or WAIT_TIMEOUT */
93 if (WaitForSingleObject (p->quitevt, SLEEPTIME) == WAIT_OBJECT_0)
94 return 0;
95 }
96 }
97
98 /* Stop profiling to the profiling buffer pointed to by P. */
99
100 static int
101 profile_off (struct profinfo *p)
102 {
103 if (p->profthr)
104 {
105 SignalObjectAndWait (p->quitevt, p->profthr, INFINITE, FALSE);
106 CloseHandle (p->quitevt);
107 CloseHandle (p->profthr);
108 }
109 if (p->targthr)
110 CloseHandle (p->targthr);
111 return 0;
112 }
113
114 /* Create a timer thread and pass it a pointer P to the profiling buffer. */
115
116 static int
117 profile_on (struct profinfo *p)
118 {
119 DWORD thrid;
120
121 /* get handle for this thread */
122 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
123 GetCurrentProcess (), &p->targthr, 0, FALSE,
124 DUPLICATE_SAME_ACCESS))
125 {
126 errno = ESRCH;
127 return -1;
128 }
129
130 p->quitevt = CreateEvent (NULL, TRUE, FALSE, NULL);
131
132 if (!p->quitevt)
133 {
134 CloseHandle (p->quitevt);
135 p->targthr = 0;
136 errno = EAGAIN;
137 return -1;
138 }
139
140 p->profthr = CreateThread (0, 0, profthr_func, (void *) p, 0, &thrid);
141
142 if (!p->profthr)
143 {
144 CloseHandle (p->targthr);
145 CloseHandle (p->quitevt);
146 p->targthr = 0;
147 errno = EAGAIN;
148 return -1;
149 }
150
151 /* Set profiler thread priority to highest to be sure that it gets the
152 processor as soon it request it (i.e. when the Sleep terminate) to get
153 the next data out of the profile. */
154
155 SetThreadPriority (p->profthr, THREAD_PRIORITY_TIME_CRITICAL);
156
157 return 0;
158 }
159
160 /*
161 * start or stop profiling
162 *
163 * profiling goes into the SAMPLES buffer of size SIZE (which is treated
164 * as an array of u_shorts of size size/2)
165 *
166 * each bin represents a range of pc addresses from OFFSET. The number
167 * of pc addresses in a bin depends on SCALE. (A scale of 65536 maps
168 * each bin to two addresses, A scale of 32768 maps each bin to 4 addresses,
169 * a scale of 1 maps each bin to 128k addreses). Scale may be 1 - 65536,
170 * or zero to turn off profiling
171 */
172 int
173 profile_ctl (struct profinfo * p, char *samples, size_t size,
174 size_t offset, u_int scale)
175 {
176 size_t maxbin;
177
178 if (scale > 65536)
179 {
180 errno = EINVAL;
181 return -1;
182 }
183
184 profile_off (p);
185 if (scale)
186 {
187 memset (samples, 0, size);
188 *p = (struct profinfo){0};
189 maxbin = size >> 1;
190 prof.counter = (u_short *) samples;
191 prof.lowpc = offset;
192 prof.highpc = PROFADDR (maxbin, offset, scale);
193 prof.scale = scale;
194
195 return profile_on (p);
196 }
197 return 0;
198 }
199
200 /* Equivalent to unix profil()
201 Every SLEEPTIME interval, the user's program counter (PC) is examined:
202 offset is subtracted and the result is multiplied by scale.
203 The word pointed to by this address is incremented. Buf is unused. */
204
205 int
206 profil (char *samples, size_t size, size_t offset, u_int scale)
207 {
208 return profile_ctl (&prof, samples, size, offset, scale);
209 }
210