1 | /* $Id: prfcoreterm.cpp.h 29 2009-07-01 20:30:29Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kProfiler Mark 2 - Core Termination Code Template.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unwinds and terminates all the threads, and frees the stack space.
|
---|
34 | *
|
---|
35 | * @returns The new data set size. (pHdr->cb)
|
---|
36 | * @param pHdr The profiler data set header.
|
---|
37 | */
|
---|
38 | KPRF_DECL_FUNC(KU32, TerminateAll)(KPRF_TYPE(P,HDR) pHdr)
|
---|
39 | {
|
---|
40 | KU64 TS = KPRF_NOW();
|
---|
41 | if (!pHdr)
|
---|
42 | return 0;
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Iterate the threads and terminate all which are non-terminated.
|
---|
46 | */
|
---|
47 | KPRF_TYPE(P,THREAD) paThread = KPRF_OFF2PTR(P,THREAD, pHdr->offThreads, pHdr);
|
---|
48 | for (KU32 i = 0; i < pHdr->cThreads; i++)
|
---|
49 | {
|
---|
50 | KPRF_TYPE(P,THREAD) pCur = &paThread[i];
|
---|
51 | switch (pCur->enmState)
|
---|
52 | {
|
---|
53 | /* these states needs no work. */
|
---|
54 | case KPRF_TYPE(,THREADSTATE_TERMINATED):
|
---|
55 | case KPRF_TYPE(,THREADSTATE_UNUSED):
|
---|
56 | default:
|
---|
57 | break;
|
---|
58 |
|
---|
59 | /* these are active and requires unwinding.*/
|
---|
60 | case KPRF_TYPE(,THREADSTATE_ACTIVE):
|
---|
61 | case KPRF_TYPE(,THREADSTATE_SUSPENDED):
|
---|
62 | case KPRF_TYPE(,THREADSTATE_OVERFLOWED):
|
---|
63 | KPRF_NAME(TerminateThread)(pHdr, pCur, TS);
|
---|
64 | break;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Free the stacks.
|
---|
71 | */
|
---|
72 | if (pHdr->offStacks)
|
---|
73 | {
|
---|
74 | /* only if the stack is at the end of the data set. */
|
---|
75 | const KU32 cbStacks = KPRF_ALIGN(pHdr->cMaxStacks * pHdr->cbStack, 32);
|
---|
76 | if (pHdr->offStacks + cbStacks == pHdr->cb)
|
---|
77 | pHdr->cb -= cbStacks;
|
---|
78 | pHdr->offStacks = 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return pHdr->cb;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Sets the commandline.
|
---|
87 | *
|
---|
88 | * This is typically done after TerminateAll, when the stacks has
|
---|
89 | * been freed up and there is plenty free space.
|
---|
90 | *
|
---|
91 | * @returns The new data set size. (pHdr->cb)
|
---|
92 | * @param pHdr The profiler data set header.
|
---|
93 | * @param cArgs The number of arguments in the array.
|
---|
94 | * @param papszArgs Pointer to an array of arguments.
|
---|
95 | */
|
---|
96 | KPRF_DECL_FUNC(KU32, SetCommandLine)(KPRF_TYPE(P,HDR) pHdr, unsigned cArgs, const char * const *papszArgs)
|
---|
97 | {
|
---|
98 | if (!pHdr)
|
---|
99 | return 0;
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Any space at all?
|
---|
103 | */
|
---|
104 | if (pHdr->cb + 16 > pHdr->cbAllocated) /* 16 bytes min */
|
---|
105 | return pHdr->cb;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Encode untill we run out of space.
|
---|
109 | */
|
---|
110 | pHdr->offCommandLine = pHdr->cb;
|
---|
111 | char *psz = (char *)pHdr + pHdr->cb;
|
---|
112 | char *pszMax = (char *)pHdr + pHdr->cbAllocated - 1;
|
---|
113 | for (unsigned i = 0; i < cArgs && psz + 7 < pszMax; i++)
|
---|
114 | {
|
---|
115 | if (i > 0)
|
---|
116 | *psz++ = ' ';
|
---|
117 | *psz++ = '\'';
|
---|
118 | const char *pszArg = papszArgs[i];
|
---|
119 | while (psz < pszMax)
|
---|
120 | {
|
---|
121 | char ch = *pszArg++;
|
---|
122 | if (!ch)
|
---|
123 | break;
|
---|
124 | if (ch == '\'')
|
---|
125 | {
|
---|
126 | if (psz + 1 >= pszMax)
|
---|
127 | break;
|
---|
128 | *psz++ = '\\';
|
---|
129 | }
|
---|
130 | *psz++ = ch;
|
---|
131 | }
|
---|
132 | if (psz < pszMax)
|
---|
133 | *psz++ = '\'';
|
---|
134 | }
|
---|
135 | *psz++ = '\0';
|
---|
136 | pHdr->cb = psz - (char *)pHdr;
|
---|
137 | pHdr->cchCommandLine = pHdr->cb - pHdr->offCommandLine - 1;
|
---|
138 |
|
---|
139 | return pHdr->cb;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|