Skip to content

Commit 5b2f5c2

Browse files
committed
Creating special version with printing to track down why Pd crashes (but only with a particular interface and only on a particular linux as far as we know).
1 parent 251b860 commit 5b2f5c2

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pm_common/portmidi.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "pminternal.h"
1010
#include <assert.h>
1111

12+
#include "stdio.h" /* for this test version only */
13+
1214
#define MIDI_CLOCK 0xf8
1315
#define MIDI_ACTIVE 0xfe
1416
#define MIDI_STATUS_MASK 0x80
@@ -209,14 +211,16 @@ PmError pm_create_virtual(PmInternal *midi, int is_input, const char *interf,
209211
*/
210212
PmError pm_add_device(const char *interf, const char *name, int is_input,
211213
int is_virtual, void *descriptor, pm_fns_type dictionary) {
212-
/* printf("pm_add_device: %s %s %d %p %p\n",
213-
interf, name, is_input, descriptor, dictionary); */
214+
215+
printf("pm_add_device: %s %s %d %p %p\n",
216+
interf, name, is_input, descriptor, dictionary);
217+
214218
int device_id;
215219
PmDeviceInfo *d;
216220
/* if virtual, search for duplicate name or unused ID; otherwise,
217221
* just add a new device at the next integer available:
218222
*/
219-
for (device_id = (is_virtual ? 0 : pm_descriptor_len);
223+
for (device_id = (is_virtual ? 0 : pm_descriptor_len);
220224
device_id < pm_descriptor_len; device_id++) {
221225
d = &pm_descriptors[device_id].pub;
222226
d->structVersion = PM_DEVICEINFO_VERS;
@@ -262,6 +266,7 @@ PmError pm_add_device(const char *interf, const char *name, int is_input,
262266
if (device_id == pm_descriptor_len) {
263267
pm_descriptor_len++; /* extending array of pm_descriptors */
264268
}
269+
265270
d = &pm_descriptors[device_id].pub;
266271
d->interf = interf;
267272
d->name = pm_alloc(strlen(name) + 1);
@@ -287,6 +292,9 @@ PmError pm_add_device(const char *interf, const char *name, int is_input,
287292
/* points to PmInternal, allows automatic device closing */
288293
pm_descriptors[device_id].pm_internal = NULL;
289294

295+
printf(" -> device_id %d pm_internal %p name %s\n",
296+
device_id, pm_descriptors[device_id].pm_internal, d->name);
297+
290298
pm_descriptors[device_id].dictionary = dictionary;
291299

292300
/* set the defaults to the first input and output we see */
@@ -315,6 +323,9 @@ void pm_undo_add_device(int id)
315323
pm_descriptors[id].descriptor = NULL;
316324
pm_descriptors[id].pm_internal = NULL;
317325

326+
printf("pm_undo_add_device: id %d pm_internal %p\n", id,
327+
pm_descriptors[id].pm_internal);
328+
318329
if (id == pm_descriptor_len - 1) {
319330
pm_free(pm_descriptors[id].pub.name);
320331
pm_descriptor_len--;
@@ -938,6 +949,10 @@ PmError pm_create_internal(PmInternal **stream, PmDeviceID device_id,
938949
midi->fill_length = 0;
939950
midi->dictionary = pm_descriptors[device_id].dictionary;
940951
pm_descriptors[device_id].pm_internal = midi;
952+
953+
printf("pm_create_internal: device_id %d midi (pm_internal) %p"
954+
" time_proc %p\n", device_id, midi, midi->time_proc);
955+
941956
return pmNoError;
942957
}
943958

@@ -975,6 +990,10 @@ PMEXPORT PmError Pm_OpenInput(PortMidiStream** stream,
975990
if (err) {
976991
*stream = NULL;
977992
pm_descriptors[inputDevice].pm_internal = NULL;
993+
994+
printf("Pm_OpenInput after error: id %d pm_internal NULL\n",
995+
inputDevice);
996+
978997
/* free portMidi data */
979998
Pm_QueueDestroy(midi->queue);
980999
pm_free(midi);
@@ -1027,6 +1046,10 @@ PMEXPORT PmError Pm_OpenOutput(PortMidiStream** stream,
10271046
if (err) {
10281047
*stream = NULL;
10291048
pm_descriptors[outputDevice].pm_internal = NULL;
1049+
1050+
printf("Pm_OpenOutput after error: id %d pm_internal NULL\n",
1051+
outputDevice);
1052+
10301053
/* free portMidi data */
10311054
pm_free(midi);
10321055
} else {
@@ -1122,6 +1145,9 @@ PmError Pm_DeleteVirtualDevice(PmDeviceID id)
11221145
pm_descriptors[id].deleted = TRUE;
11231146
/* (pm_internal should already be NULL because !pub.opened) */
11241147
pm_descriptors[id].pm_internal = NULL;
1148+
1149+
printf("Pm_DeleteVirtualDevice: id %d pm_internal NULL\n", id);
1150+
11251151
pm_descriptors[id].descriptor = NULL;
11261152
return err;
11271153
}
@@ -1179,6 +1205,10 @@ PMEXPORT PmError Pm_Close(PortMidiStream *stream)
11791205
err = (*midi->dictionary->close)(midi);
11801206
/* even if an error occurred, continue with cleanup */
11811207
pm_descriptors[midi->device_id].pm_internal = NULL;
1208+
1209+
printf("Pm_Close: midi %p id %d pm_internal NULL\n",
1210+
midi, midi->device_id);
1211+
11821212
pm_descriptors[midi->device_id].pub.opened = FALSE;
11831213
if (midi->queue) Pm_QueueDestroy(midi->queue);
11841214
pm_free(midi);

pm_linux/pmlinuxalsa.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/* I used many print statements to debug this code. I left them in the
2626
* source, and you can turn them on by changing false to true below:
2727
*/
28-
#define VERBOSE_ON 0
28+
#define VERBOSE_ON 1
2929
#define VERBOSE if (VERBOSE_ON)
3030

3131
#define MIDI_SYSEX 0xf0
@@ -553,15 +553,30 @@ static PmTimestamp alsa_synchronize(PmInternal *midi)
553553
static void handle_event(snd_seq_event_t *ev)
554554
{
555555
int device_id = ev->dest.port;
556+
557+
printf("(pm) handle_event: ev %p device_id %d\n",
558+
ev, device_id);
559+
560+
assert(device_id >= 0 && device_id < pm_descriptor_len);
561+
assert(pm_descriptors[device_id].pub.name);
562+
563+
printf(" name %s\n", pm_descriptors[device_id].pub.name);
564+
556565
PmInternal *midi = pm_descriptors[device_id].pm_internal;
557566
/* There is a race condition when closing a device and
558567
continuing to poll other open devices. The closed device may
559568
have outstanding events from before the close operation.
560569
*/
561570
if (!midi) {
571+
printf("(pm) handle_event returns because NULL midi (pm_internal)\n");
562572
return;
563573
}
564574
PmEvent pm_ev;
575+
576+
printf("(pm) handle_event: midi (pm_internal) %p midi->time_proc %p\n",
577+
midi, midi->time_proc);
578+
fflush(stdout); /* in case time_proc is NULL and we crash */
579+
565580
PmTimestamp timestamp = midi->time_proc(midi->time_info);
566581

567582
/* time stamp should be in ticks, using our queue where 1 tick = 1ms */

0 commit comments

Comments
 (0)