1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/* Sysprof -- Sampling, systemwide CPU profiler
* Copyright 2005, Lorenzo Colitti
* Copyright 2005, Soren Sandmann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <glib.h>
#include <stdio.h>
#include "stackstash.h"
#include "profile.h"
#include "watch.h"
#include "signal-handler.h"
#include "collector.h"
typedef struct Application Application;
struct Application
{
Collector * collector;
char * outfile;
GMainLoop * main_loop;
};
static void
dump_data (Application *app)
{
GError *err = NULL;
Profile *profile;
printf ("Saving profile in %s ... ", app->outfile);
fflush (stdout);
collector_stop (app->collector);
profile = collector_create_profile (app->collector);
profile_save (profile, app->outfile, &err);
if (err)
{
printf ("failed\n");
fprintf (stderr, "Error saving %s: %s\n", app->outfile, err->message);
exit (1);
}
else
{
printf ("done\n\n");
}
}
static void
signal_handler (int signo,
gpointer data)
{
Application *app = data;
dump_data (app);
while (g_main_context_iteration (NULL, FALSE))
;
g_main_loop_quit (app->main_loop);
}
static char *
usage_msg (const char *name)
{
return g_strdup_printf (
"Usage: \n"
" %s <outfile>\n"
"\n"
"On SIGTERM or SIGINT (Ctrl-C) the profile will be written to <outfile>",
name);
}
static gboolean
file_exists_and_is_dir (const char *name)
{
return
g_file_test (name, G_FILE_TEST_EXISTS) &&
g_file_test (name, G_FILE_TEST_IS_DIR);
}
static void
die (const char *err_msg)
{
if (err_msg)
fprintf (stderr, "\n%s\n\n", err_msg);
exit (-1);
}
int
main (int argc,
char *argv[])
{
Application *app = g_new0 (Application, 1);
GError *err;
app->collector = collector_new (NULL, NULL);
app->outfile = g_strdup (argv[1]);
app->main_loop = g_main_loop_new (NULL, 0);
err = NULL;
if (!collector_start (app->collector, &err))
die (err->message);
if (argc < 2)
die (usage_msg (argv[0]));
if (!signal_set_handler (SIGTERM, signal_handler, app, &err))
die (err->message);
if (!signal_set_handler (SIGINT, signal_handler, app, &err))
die (err->message);
if (file_exists_and_is_dir (app->outfile))
{
char *msg = g_strdup_printf ("Can't write to %s: is a directory\n",
app->outfile);
die (msg);
}
g_main_loop_run (app->main_loop);
signal_unset_handler (SIGTERM);
signal_unset_handler (SIGINT);
return 0;
}
|