kDefs.h: Added risc-v to the K_ARCH_XXX defines.
[kstuff-mirror.git] / kProfiler2 / prfcoreterm.cpp.h
blob8d1d5555abe4d55ae9b9ac1a50943588795f237e
1 /* $Id$ */
2 /** @file
3 * kProfiler Mark 2 - Core Termination Code Template.
4 */
6 /*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
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:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
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.
32 /**
33 * Unwinds and terminates all the threads, and frees the stack space.
35 * @returns The new data set size. (pHdr->cb)
36 * @param pHdr The profiler data set header.
38 KPRF_DECL_FUNC(KU32, TerminateAll)(KPRF_TYPE(P,HDR) pHdr)
40 KU64 TS = KPRF_NOW();
41 if (!pHdr)
42 return 0;
45 * Iterate the threads and terminate all which are non-terminated.
47 KPRF_TYPE(P,THREAD) paThread = KPRF_OFF2PTR(P,THREAD, pHdr->offThreads, pHdr);
48 for (KU32 i = 0; i < pHdr->cThreads; i++)
50 KPRF_TYPE(P,THREAD) pCur = &paThread[i];
51 switch (pCur->enmState)
53 /* these states needs no work. */
54 case KPRF_TYPE(,THREADSTATE_TERMINATED):
55 case KPRF_TYPE(,THREADSTATE_UNUSED):
56 default:
57 break;
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;
70 * Free the stacks.
72 if (pHdr->offStacks)
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;
81 return pHdr->cb;
85 /**
86 * Sets the commandline.
88 * This is typically done after TerminateAll, when the stacks has
89 * been freed up and there is plenty free space.
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.
96 KPRF_DECL_FUNC(KU32, SetCommandLine)(KPRF_TYPE(P,HDR) pHdr, unsigned cArgs, const char * const *papszArgs)
98 if (!pHdr)
99 return 0;
102 * Any space at all?
104 if (pHdr->cb + 16 > pHdr->cbAllocated) /* 16 bytes min */
105 return pHdr->cb;
108 * Encode untill we run out of space.
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++)
115 if (i > 0)
116 *psz++ = ' ';
117 *psz++ = '\'';
118 const char *pszArg = papszArgs[i];
119 while (psz < pszMax)
121 char ch = *pszArg++;
122 if (!ch)
123 break;
124 if (ch == '\'')
126 if (psz + 1 >= pszMax)
127 break;
128 *psz++ = '\\';
130 *psz++ = ch;
132 if (psz < pszMax)
133 *psz++ = '\'';
135 *psz++ = '\0';
136 pHdr->cb = psz - (char *)pHdr;
137 pHdr->cchCommandLine = pHdr->cb - pHdr->offCommandLine - 1;
139 return pHdr->cb;