aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/symbianutils/launcher.cpp
blob: 87152572f78db5ad4e036e1dc2ba1445c1531195 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "launcher.h"
#include "trkutils.h"
#include "trkutils_p.h"
#include "trkdevice.h"
#include "bluetoothlistener.h"
#include "symbiandevicemanager.h"

#include <QtCore/QTimer>
#include <QtCore/QDateTime>
#include <QtCore/QVariant>
#include <QtCore/QDebug>
#include <QtCore/QQueue>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QScopedPointer>

#include <cstdio>

namespace trk {

struct CrashReportState {
    CrashReportState();
    void clear();

    typedef uint Thread;
    typedef QList<Thread> Threads;
    Threads threads;

    QList<uint> registers;
    QByteArray stack;
    uint sp;
    uint fetchingStackPID;
    uint fetchingStackTID;
};

CrashReportState::CrashReportState()
{
    clear();
}

void CrashReportState::clear()
{
    threads.clear();
    stack.clear();
    sp = fetchingStackPID = fetchingStackTID = 0;
}

struct LauncherPrivate {
    struct TransferState {
        int currentFileName;
        uint copyFileHandle;
        QScopedPointer<QByteArray> data;
        qint64 position;
        QScopedPointer<QFile> localFile;
    };

    struct CopyState : public TransferState {
        QStringList sourceFileNames;
        QStringList destinationFileNames;
    };

    struct DownloadState : public TransferState {
        QString sourceFileName;
        QString destinationFileName;
    };

    explicit LauncherPrivate(const TrkDevicePtr &d);

    TrkDevicePtr m_device;
    QByteArray m_trkReadBuffer;
    Launcher::State m_state;

    void logMessage(const QString &msg);
    // Debuggee state
    Session m_session; // global-ish data (process id, target information)

    CopyState m_copyState;
    DownloadState m_downloadState;
    QString m_fileName;
    QString m_commandLineArgs;
    QStringList m_installFileNames;
    int m_currentInstallFileName;
    int m_verbose;
    Launcher::Actions m_startupActions;
    bool m_closeDevice;
    CrashReportState m_crashReportState;
    Launcher::InstallationMode m_installationMode;
    Launcher::InstallationMode m_currentInstallationStep;
    char m_installationDrive;
};

LauncherPrivate::LauncherPrivate(const TrkDevicePtr &d) :
    m_device(d),
    m_state(Launcher::Disconnected),
    m_verbose(0),
    m_closeDevice(true),
    m_installationMode(Launcher::InstallationModeSilentAndUser),
    m_currentInstallationStep(Launcher::InstallationModeSilent),
    m_installationDrive('C')
{
    if (m_device.isNull())
        m_device = TrkDevicePtr(new TrkDevice);
}

Launcher::Launcher(Actions startupActions,
                   const TrkDevicePtr &dev,
                   QObject *parent) :
    QObject(parent),
    d(new LauncherPrivate(dev))
{
    d->m_startupActions = startupActions;
    connect(d->m_device.data(), SIGNAL(messageReceived(trk::TrkResult)), this, SLOT(handleResult(trk::TrkResult)));
}

Launcher::~Launcher()
{
    // Destroyed before protocol was through: Close
    if (d->m_closeDevice && d->m_device->isOpen())
        d->m_device->close();
    emit destroyed(d->m_device->port());
    logMessage("Shutting down.\n");
    delete d;
}

Launcher::State Launcher::state() const
{
    return d->m_state;
}

void Launcher::setState(State s)
{
    if (s != d->m_state) {
        d->m_state = s;
        emit stateChanged(s);
    }
}

void Launcher::setInstallationMode(InstallationMode installation)
{
    d->m_installationMode = installation;
}

void Launcher::setInstallationDrive(char drive)
{
    d->m_installationDrive = drive;
}

void Launcher::addStartupActions(trk::Launcher::Actions startupActions)
{
    d->m_startupActions = Actions(d->m_startupActions | startupActions);
}

void Launcher::setTrkServerName(const QString &name)
{
    d->m_device->setPort(name);
}

QString Launcher::trkServerName() const
{
    return d->m_device->port();
}

TrkDevicePtr Launcher::trkDevice() const
{
    return d->m_device;
}

void Launcher::setFileName(const QString &name)
{
    d->m_fileName = name;
}

void Launcher::setCopyFileNames(const QStringList &srcNames, const QStringList &dstNames)
{
    d->m_copyState.sourceFileNames = srcNames;
    d->m_copyState.destinationFileNames = dstNames;
    d->m_copyState.currentFileName = 0;
}

void Launcher::setDownloadFileName(const QString &srcName, const QString &dstName)
{
    d->m_downloadState.sourceFileName = srcName;
    d->m_downloadState.destinationFileName = dstName;
}

void Launcher::setInstallFileNames(const QStringList &names)
{
    d->m_installFileNames = names;
    d->m_currentInstallFileName = 0;
}

void Launcher::setCommandLineArgs(const QString &args)
{
    d->m_commandLineArgs = args;
}

void Launcher::setSerialFrame(bool b)
{
    d->m_device->setSerialFrame(b);
}

bool Launcher::serialFrame() const
{
    return d->m_device->serialFrame();
}

bool Launcher::closeDevice() const
{
    return d->m_closeDevice;
}

void Launcher::setCloseDevice(bool c)
{
    d->m_closeDevice = c;
}

Launcher::InstallationMode Launcher::installationMode() const
{
    return d->m_installationMode;
}

char Launcher::installationDrive() const
{
    return d->m_installationDrive;
}

bool Launcher::startServer(QString *errorMessage)
{
    errorMessage->clear();
    d->m_crashReportState.clear();
    if (d->m_verbose) {
        QString msg;
        QTextStream str(&msg);
        str.setIntegerBase(16);
        str << "Actions=0x" << d->m_startupActions;
        str.setIntegerBase(10);
        str << " Port=" << trkServerName();
        if (!d->m_fileName.isEmpty())
            str << " Executable=" << d->m_fileName;
        if (!d->m_commandLineArgs.isEmpty())
            str << " Arguments= " << d->m_commandLineArgs;
        for (int i = 0; i < d->m_copyState.sourceFileNames.size(); ++i) {
            str << " Package/Source=" << d->m_copyState.sourceFileNames.at(i);
            str << " Remote Package/Destination=" << d->m_copyState.destinationFileNames.at(i);
        }
        if (!d->m_downloadState.sourceFileName.isEmpty())
            str << " Source=" << d->m_downloadState.sourceFileName;
        if (!d->m_downloadState.destinationFileName.isEmpty())
            str << " Destination=" << d->m_downloadState.destinationFileName;
        if (!d->m_installFileNames.isEmpty())
            foreach (const QString &installFileName, d->m_installFileNames)
                str << " Install file=" << installFileName;
        logMessage(msg);
    }
    if (d->m_startupActions & ActionCopy) {
        if (d->m_copyState.sourceFileNames.isEmpty()) {
            qWarning("No local filename given for copying package.");
            return false;
        } else if (d->m_copyState.destinationFileNames.isEmpty()) {
            qWarning("No remote filename given for copying package.");
            return false;
        }
    }
    if (d->m_startupActions & ActionInstall && d->m_installFileNames.isEmpty()) {
        qWarning("No package name given for installing.");
        return false;
    }
    if (d->m_startupActions & ActionRun && d->m_fileName.isEmpty()) {
        qWarning("No remote executable given for running.");
        return false;
    }
    if (!d->m_device->isOpen() && !d->m_device->open(errorMessage))
        return false;
    setState(Connecting);
    // Set up the temporary 'waiting' state if we do not get immediate connection
    QTimer::singleShot(1000, this, SLOT(slotWaitingForTrk()));
    d->m_device->sendTrkInitialPing();
    d->m_device->sendTrkMessage(TrkDisconnect); // Disconnect, as trk might be still connected
    d->m_device->sendTrkMessage(TrkSupported, TrkCallback(this, &Launcher::handleSupportMask));
    d->m_device->sendTrkMessage(TrkCpuType, TrkCallback(this, &Launcher::handleCpuType));
    d->m_device->sendTrkMessage(TrkVersions, TrkCallback(this, &Launcher::handleTrkVersion));
    if (d->m_startupActions != ActionPingOnly)
        d->m_device->sendTrkMessage(TrkConnect, TrkCallback(this, &Launcher::handleConnect));
    return true;
}

void Launcher::slotWaitingForTrk()
{
    // Set temporary state if we are still in connected state
    if (state() == Connecting)
        setState(WaitingForTrk);
}

void Launcher::handleConnect(const TrkResult &result)
{
    if (result.errorCode()) {
        emit canNotConnect(result.errorString());
        return;
    }
    setState(Connected);
    if (d->m_startupActions & ActionCopy)
        copyFileToRemote();
    else if (d->m_startupActions & ActionInstall)
        installRemotePackage();
    else if (d->m_startupActions & ActionRun)
        startInferiorIfNeeded();
    else if (d->m_startupActions & ActionDownload)
        copyFileFromRemote();
}

void Launcher::setVerbose(int v)
{
    d->m_verbose = v;
    d->m_device->setVerbose(v);
}

void Launcher::logMessage(const QString &msg)
{
    if (d->m_verbose)
        qDebug() << "LAUNCHER: " << qPrintable(msg);
}

void Launcher::handleFinished()
{
    if (d->m_closeDevice)
        d->m_device->close();
    emit finished();
}

void Launcher::terminate()
{
    switch (state()) {
    case DeviceDescriptionReceived:
    case Connected:
        if (d->m_session.pid) {
            QByteArray ba;
            appendShort(&ba, 0x0000, TargetByteOrder);
            appendInt(&ba, d->m_session.pid, TargetByteOrder);
            d->m_device->sendTrkMessage(TrkDeleteItem, TrkCallback(this, &Launcher::handleRemoteProcessKilled), ba);
            return;
        }
        if (d->m_copyState.copyFileHandle)
            closeRemoteFile(true);
        disconnectTrk();
        break;
    case Disconnected:
        break;
    case Connecting:
    case WaitingForTrk:
        setState(Disconnected);
        handleFinished();
        break;
    }
}

void Launcher::handleRemoteProcessKilled(const TrkResult &result)
{
    Q_UNUSED(result)
    disconnectTrk();
}

QString Launcher::msgStopped(uint pid, uint tid, uint address, const QString &why)
{
    return QString::fromLatin1("Process %1, thread %2 stopped at 0x%3: %4").
            arg(pid).arg(tid).arg(address, 0, 16).
            arg(why.isEmpty() ? QString::fromLatin1("<Unknown reason>") : why);
}

bool Launcher::parseNotifyStopped(const QByteArray &dataBA,
                                  uint *pid, uint *tid, uint *address,
                                  QString *why /* = 0 */)
{
    if (why)
        why->clear();
    *address = *pid = *tid = 0;
    if (dataBA.size() < 12)
        return false;
    const char *data = dataBA.data();
    *address = extractInt(data);
    *pid = extractInt(data + 4);
    *tid = extractInt(data + 8);
    if (why && dataBA.size() >= 14) {
        const unsigned short len = extractShort(data + 12);
        if (len > 0)
            *why = QString::fromLatin1(data + 14, len);
    }
    return true;
}

void Launcher::handleResult(const TrkResult &result)
{
    QByteArray prefix = "READ BUF:                                       ";
    QByteArray str = result.toString().toUtf8();
    if (result.isDebugOutput) { // handle application output
        QString msg;
        if (result.multiplex == MuxTextTrace) {
            if (result.data.length() > 8) {
            quint64 timestamp = extractInt64(result.data) & 0x0FFFFFFFFFFFFFFFULL;
            quint64 secs = timestamp / 1000000000;
            quint64 ns = timestamp % 1000000000;
            msg = QString("[%1.%2] %3").arg(secs).arg(ns).arg(QString(result.data.mid(8)));
            logMessage("TEXT TRACE: " + msg);
            }
        } else {
            logMessage("APPLICATION OUTPUT: " + stringFromArray(result.data));
            msg = result.data;
        }
        msg.replace("\r\n", "\n");
        if(!msg.endsWith('\n')) msg.append('\n');
        emit applicationOutputReceived(msg);
        return;
    }
    switch (result.code) {
        case TrkNotifyAck:
            break;
        case TrkNotifyNak: { // NAK
            logMessage(prefix + QLatin1String("NAK: ") + str);
            //logMessage(prefix << "TOKEN: " << result.token);
            logMessage(prefix + QLatin1String("ERROR: ") + errorMessage(result.data.at(0)));
            break;
        }
        case TrkNotifyStopped: { // Notified Stopped
            QString reason;
            uint pc;
            uint pid;
            uint tid;
            parseNotifyStopped(result.data, &pid, &tid, &pc, &reason);
            logMessage(prefix + msgStopped(pid, tid, pc, reason));
            emit(processStopped(pc, pid, tid, reason));
            d->m_device->sendTrkAck(result.token);
            break;
        }
        case TrkNotifyException: { // Notify Exception (obsolete)
            logMessage(prefix + QLatin1String("NOTE: EXCEPTION  ") + str);
            d->m_device->sendTrkAck(result.token);
            break;
        }
        case TrkNotifyInternalError: { //
            logMessage(prefix + QLatin1String("NOTE: INTERNAL ERROR: ") + str);
            d->m_device->sendTrkAck(result.token);
            break;
        }

        // target->host OS notification
        case TrkNotifyCreated: { // Notify Created

            if (result.data.size() < 10)
                break;
            const char *data = result.data.constData();
            const byte error = result.data.at(0);
            Q_UNUSED(error)
            const byte type = result.data.at(1); // type: 1 byte; for dll item, this value is 2.
            const uint tid = extractInt(data + 6); //threadID: 4 bytes
            Q_UNUSED(tid)
            if (type == kDSOSDLLItem && result.data.size() >=20) {
                const Library lib = Library(result);
                d->m_session.libraries.push_back(lib);
                emit libraryLoaded(lib);
            }
            QByteArray ba;
            ba.append(result.data.mid(2, 8));
            d->m_device->sendTrkMessage(TrkContinue, TrkCallback(), ba, "CONTINUE");
            break;
        }
        case TrkNotifyDeleted: { // NotifyDeleted
            const ushort itemType = (unsigned char)result.data.at(1);
            const uint pid = result.data.size() >= 6 ? extractShort(result.data.constData() + 2) : 0;
            const uint tid = result.data.size() >= 10 ? extractShort(result.data.constData() + 6) : 0;
            Q_UNUSED(tid)
            const ushort len = result.data.size() > 12 ? extractShort(result.data.constData() + 10) : ushort(0);
            const QString name = len ? QString::fromAscii(result.data.mid(12, len)) : QString();
            logMessage(QString::fromLatin1("%1 %2 UNLOAD: %3").
                       arg(QString::fromAscii(prefix)).arg(itemType ? QLatin1String("LIB") : QLatin1String("PROCESS")).
                       arg(name));
            d->m_device->sendTrkAck(result.token);
            if (itemType == kDSOSProcessItem // process
                && result.data.size() >= 10
                && d->m_session.pid == extractInt(result.data.data() + 6)) {
                    if (d->m_startupActions & ActionDownload)
                        copyFileFromRemote();
                    else
                        disconnectTrk();
            }
            else if (itemType == kDSOSDLLItem && len) {
                // Remove libraries of process.
                for (QList<Library>::iterator it = d->m_session.libraries.begin(); it != d->m_session.libraries.end(); ) {
                    if ((*it).pid == pid && (*it).name == name) {
                        emit libraryUnloaded(*it);
                        it = d->m_session.libraries.erase(it);
                    } else {
                        ++it;
                    }
                }
            }
            break;
        }
        case TrkNotifyProcessorStarted: { // NotifyProcessorStarted
            logMessage(prefix + QLatin1String("NOTE: PROCESSOR STARTED: ") + str);
            d->m_device->sendTrkAck(result.token);
            break;
        }
        case TrkNotifyProcessorStandBy: { // NotifyProcessorStandby
            logMessage(prefix + QLatin1String("NOTE: PROCESSOR STANDBY: ") + str);
            d->m_device->sendTrkAck(result.token);
            break;
        }
        case TrkNotifyProcessorReset: { // NotifyProcessorReset
            logMessage(prefix + QLatin1String("NOTE: PROCESSOR RESET: ") + str);
            d->m_device->sendTrkAck(result.token);
            break;
        }
        default: {
            logMessage(prefix + QLatin1String("INVALID: ") + str);
            break;
        }
    }
}

QString Launcher::deviceDescription(unsigned verbose) const
{
    return d->m_session.deviceDescription(verbose);
}

void Launcher::handleTrkVersion(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 5) {
        if (d->m_startupActions == ActionPingOnly) {
            setState(Disconnected);
            handleFinished();
        }
        return;
    }
    d->m_session.trkAppVersion.trkMajor = result.data.at(1);
    d->m_session.trkAppVersion.trkMinor = result.data.at(2);
    d->m_session.trkAppVersion.protocolMajor = result.data.at(3);
    d->m_session.trkAppVersion.protocolMinor = result.data.at(4);
    setState(DeviceDescriptionReceived);
    const QString msg = deviceDescription();
    emit deviceDescriptionReceived(trkServerName(), msg);
    // Ping mode: Log & Terminate
    if (d->m_startupActions == ActionPingOnly) {
        qWarning("%s", qPrintable(msg));
        setState(Disconnected);
        handleFinished();
    }
}

static inline QString msgCannotOpenRemoteFile(const QString &fileName, const QString &message)
{
    return Launcher::tr("Cannot open remote file '%1': %2").arg(fileName, message);
}

static inline QString msgCannotOpenLocalFile(const QString &fileName, const QString &message)
{
    return Launcher::tr("Cannot open '%1': %2").arg(fileName, message);
}

void Launcher::handleFileCreation(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 6) {
        const QString msg = msgCannotOpenRemoteFile(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName), result.errorString());
        logMessage(msg);
        emit canNotCreateFile(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName), msg);
        disconnectTrk();
        return;
    }
    const char *data = result.data.data();
    d->m_copyState.copyFileHandle = extractInt(data + 2);
    const QString localFileName = d->m_copyState.sourceFileNames.at(d->m_copyState.currentFileName);
    QFile file(localFileName);
    d->m_copyState.position = 0;
    if (!file.open(QIODevice::ReadOnly)) {
        const QString msg = msgCannotOpenLocalFile(localFileName, file.errorString());
        logMessage(msg);
        emit canNotOpenLocalFile(localFileName, msg);
        closeRemoteFile(true);
        disconnectTrk();
        return;
    }
    d->m_copyState.data.reset(new QByteArray(file.readAll()));
    file.close();
    continueCopying();
}

void Launcher::handleFileOpened(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 6) {
        const QString msg = msgCannotOpenRemoteFile(d->m_downloadState.sourceFileName, result.errorString());
        logMessage(msg);
        emit canNotOpenFile(d->m_downloadState.sourceFileName, msg);
        disconnectTrk();
        return;
    }
    d->m_downloadState.position = 0;
    const QString localFileName = d->m_downloadState.destinationFileName;
    bool opened = false;
    if (localFileName == QLatin1String("-")) {
        d->m_downloadState.localFile.reset(new QFile);
        opened = d->m_downloadState.localFile->open(stdout, QFile::WriteOnly);
    } else {
        d->m_downloadState.localFile.reset(new QFile(localFileName));
        opened = d->m_downloadState.localFile->open(QFile::WriteOnly | QFile::Truncate);
    }
    if (!opened) {
        const QString msg = msgCannotOpenLocalFile(localFileName, d->m_downloadState.localFile->errorString());
        logMessage(msg);
        emit canNotOpenLocalFile(localFileName, msg);
        closeRemoteFile(true);
        disconnectTrk();
    }
    continueReading();
}

void Launcher::continueReading()
{
    QByteArray ba;
    appendInt(&ba, d->m_downloadState.copyFileHandle, TargetByteOrder);
    appendShort(&ba, 2048, TargetByteOrder);
    d->m_device->sendTrkMessage(TrkReadFile, TrkCallback(this, &Launcher::handleRead), ba);
}

void Launcher::handleRead(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 4) {
        d->m_downloadState.localFile->close();
        closeRemoteFile(true);
        disconnectTrk();
    } else {
        int length = extractShort(result.data.data() + 2);
        //TRK doesn't tell us the file length, so we need to keep reading until it returns 0 length
        if (length > 0) {
            d->m_downloadState.localFile->write(result.data.data() + 4, length);
            continueReading();
        } else {
            closeRemoteFile(true);
            disconnectTrk();
        }
    }
}

void Launcher::handleCopy(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 4) {
        closeRemoteFile(true);
        emit canNotWriteFile(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName), result.errorString());
        disconnectTrk();
    } else {
        continueCopying(extractShort(result.data.data() + 2));
    }
}

void Launcher::continueCopying(uint lastCopiedBlockSize)
{
    qint64 size = d->m_copyState.data->length();
    d->m_copyState.position += lastCopiedBlockSize;
    if (size == 0)
        emit copyProgress(100);
    else {
        const qint64 hundred = 100;
        const qint64 percent = qMin( (d->m_copyState.position * hundred) / size, hundred);
        emit copyProgress(static_cast<int>(percent));
    }
    if (d->m_copyState.position < size) {
        QByteArray ba;
        appendInt(&ba, d->m_copyState.copyFileHandle, TargetByteOrder);
        appendString(&ba, d->m_copyState.data->mid(d->m_copyState.position, 2048), TargetByteOrder, false);
        d->m_device->sendTrkMessage(TrkWriteFile, TrkCallback(this, &Launcher::handleCopy), ba);
    } else {
        closeRemoteFile();
    }
}

void Launcher::closeRemoteFile(bool failed)
{
    QByteArray ba;
    appendInt(&ba, d->m_copyState.copyFileHandle, TargetByteOrder);
    appendDateTime(&ba, QDateTime::currentDateTime(), TargetByteOrder);
    d->m_device->sendTrkMessage(TrkCloseFile,
                               failed ? TrkCallback() : TrkCallback(this, &Launcher::handleFileCopied),
                               ba);
    d->m_copyState.data.reset(0);
    d->m_copyState.copyFileHandle = 0;
    d->m_copyState.position = 0;
}

void Launcher::handleFileCopied(const TrkResult &result)
{
    if (result.errorCode())
        emit canNotCloseFile(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName), result.errorString());

    ++d->m_copyState.currentFileName;

    if (d->m_startupActions & ActionInstall && d->m_copyState.currentFileName < d->m_copyState.sourceFileNames.size()) {
        copyFileToRemote();
    } else if (d->m_startupActions & ActionInstall) {
        installRemotePackage();
    } else if (d->m_startupActions & ActionRun) {
        startInferiorIfNeeded();
    } else if (d->m_startupActions & ActionDownload) {
        copyFileFromRemote();
    } else {
        disconnectTrk();
    }
}

void Launcher::handleCpuType(const TrkResult &result)
{
    logMessage("HANDLE CPU TYPE: " + result.toString());
    if(result.errorCode() || result.data.size() < 7)
        return;
    //---TRK------------------------------------------------------
    //  Command: 0x80 Acknowledge
    //    Error: 0x00
    // [80 03 00  04 00 00 04 00 00 00]
    d->m_session.cpuMajor = result.data.at(1);
    d->m_session.cpuMinor = result.data.at(2);
    d->m_session.bigEndian = result.data.at(3);
    d->m_session.defaultTypeSize = result.data.at(4);
    d->m_session.fpTypeSize = result.data.at(5);
    d->m_session.extended1TypeSize = result.data.at(6);
    //d->m_session.extended2TypeSize = result.data[6];
}

void Launcher::handleCreateProcess(const TrkResult &result)
{
    if (result.errorCode()) {
        emit canNotRun(result.errorString());
        disconnectTrk();
        return;
    }
    //  40 00 00]
    //logMessage("       RESULT: " + result.toString());
    // [80 08 00   00 00 01 B5   00 00 01 B6   78 67 40 00   00 40 00 00]
    const char *data = result.data.data();
    d->m_session.pid = extractInt(data + 1);
    d->m_session.tid = extractInt(data + 5);
    d->m_session.codeseg = extractInt(data + 9);
    d->m_session.dataseg = extractInt(data + 13);
    if (d->m_verbose) {
        const QString msg = QString::fromLatin1("Process id: %1 Thread id: %2 code: 0x%3 data: 0x%4").
                            arg(d->m_session.pid).arg(d->m_session.tid).arg(d->m_session.codeseg, 0, 16).
                            arg(d->m_session.dataseg,  0 ,16);
        logMessage(msg);
    }
    emit applicationRunning(d->m_session.pid);
    //create a "library" entry for the executable which launched the process
    Library lib;
    lib.pid = d->m_session.pid;
    lib.codeseg = d->m_session.codeseg;
    lib.dataseg = d->m_session.dataseg;
    lib.name = d->m_fileName.toUtf8();
    d->m_session.libraries << lib;
    emit libraryLoaded(lib);

    QByteArray ba;
    appendInt(&ba, d->m_session.pid);
    appendInt(&ba, d->m_session.tid);
    d->m_device->sendTrkMessage(TrkContinue, TrkCallback(), ba, "CONTINUE");
}

void Launcher::handleWaitForFinished(const TrkResult &result)
{
    logMessage("   FINISHED: " + stringFromArray(result.data));
    setState(Disconnected);
    handleFinished();
}

void Launcher::handleSupportMask(const TrkResult &result)
{
    if (result.errorCode() || result.data.size() < 32)
        return;
    const char *data = result.data.data() + 1;

    if (d->m_verbose > 1) {
        QString str = QLatin1String("SUPPORTED: ");
        for (int i = 0; i < 32; ++i) {
            for (int j = 0; j < 8; ++j) {
                if (data[i] & (1 << j)) {
                    str.append(QString::number(i * 8 + j, 16));
                    str.append(QLatin1Char(' '));
                }
            }
        }
        logMessage(str);
    }
}

void Launcher::cleanUp()
{
    //
    //---IDE------------------------------------------------------
    //  Command: 0x41 Delete Item
    //  Sub Cmd: Delete Process
    //ProcessID: 0x0000071F (1823)
    // [41 24 00 00 00 00 07 1F]
    QByteArray ba(2, char(0));
    appendInt(&ba, d->m_session.pid);
    d->m_device->sendTrkMessage(TrkDeleteItem, TrkCallback(), ba, "Delete process");

    //---TRK------------------------------------------------------
    //  Command: 0x80 Acknowledge
    //    Error: 0x00
    // [80 24 00]

    //---IDE------------------------------------------------------
    //  Command: 0x1C Clear Break
    // [1C 25 00 00 00 0A 78 6A 43 40]

        //---TRK------------------------------------------------------
        //  Command: 0xA1 Notify Deleted
        // [A1 09 00 00 00 00 00 00 00 00 07 1F]
        //---IDE------------------------------------------------------
        //  Command: 0x80 Acknowledge
        //    Error: 0x00
        // [80 09 00]

    //---TRK------------------------------------------------------
    //  Command: 0x80 Acknowledge
    //    Error: 0x00
    // [80 25 00]

    //---IDE------------------------------------------------------
    //  Command: 0x1C Clear Break
    // [1C 26 00 00 00 0B 78 6A 43 70]
    //---TRK------------------------------------------------------
    //  Command: 0x80 Acknowledge
    //    Error: 0x00
    // [80 26 00]


    //---IDE------------------------------------------------------
    //  Command: 0x02 Disconnect
    // [02 27]
//    sendTrkMessage(0x02, TrkCallback(this, &Launcher::handleDisconnect));
    //---TRK------------------------------------------------------
    //  Command: 0x80 Acknowledge
    // Error: 0x00
}

void Launcher::disconnectTrk()
{
    d->m_device->sendTrkMessage(TrkDisconnect, TrkCallback(this, &Launcher::handleWaitForFinished));
}

void Launcher::copyFileToRemote()
{
    QFileInfo fileInfo(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName));
    emit copyingStarted(fileInfo.fileName());
    QByteArray ba;
    ba.append(char(10)); //kDSFileOpenWrite | kDSFileOpenBinary
    appendString(&ba, d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName).toLocal8Bit(), TargetByteOrder, false);
    d->m_device->sendTrkMessage(TrkOpenFile, TrkCallback(this, &Launcher::handleFileCreation), ba);
}

void Launcher::copyFileFromRemote()
{
    QFileInfo fileInfo(d->m_copyState.destinationFileNames.at(d->m_copyState.currentFileName));
    emit copyingStarted(fileInfo.fileName());
    QByteArray ba;
    ba.append(char(9)); //kDSFileOpenRead | kDSFileOpenBinary
    appendString(&ba, d->m_downloadState.sourceFileName.toLocal8Bit(), TargetByteOrder, false);
    d->m_device->sendTrkMessage(TrkOpenFile, TrkCallback(this, &Launcher::handleFileOpened), ba);
}

void Launcher::installRemotePackageSilently()
{
    emit installingStarted(d->m_installFileNames.at(d->m_currentInstallFileName));
    d->m_currentInstallationStep = InstallationModeSilent;
    QByteArray ba;
    ba.append(static_cast<char>(QChar::toUpper((ushort)d->m_installationDrive)));
    appendString(&ba, d->m_installFileNames.at(d->m_currentInstallFileName).toLocal8Bit(), TargetByteOrder, false);
    d->m_device->sendTrkMessage(TrkInstallFile, TrkCallback(this, &Launcher::handleInstallPackageFinished), ba);
}

void Launcher::installRemotePackageByUser()
{
    emit installingStarted(d->m_installFileNames.at(d->m_currentInstallFileName));
    d->m_currentInstallationStep = InstallationModeUser;
    QByteArray ba;
    appendString(&ba, d->m_installFileNames.at(d->m_currentInstallFileName).toLocal8Bit(), TargetByteOrder, false);
    d->m_device->sendTrkMessage(TrkInstallFile2, TrkCallback(this, &Launcher::handleInstallPackageFinished), ba);
}

void Launcher::installRemotePackage()
{
    switch (installationMode()) {
    case InstallationModeSilent:
    case InstallationModeSilentAndUser:
        installRemotePackageSilently();
        break;
    case InstallationModeUser:
        installRemotePackageByUser();
        break;
    default:
        break;
    }
}

void Launcher::handleInstallPackageFinished(const TrkResult &result)
{
    if (result.errorCode()) {
        if (installationMode() == InstallationModeSilentAndUser
            && d->m_currentInstallationStep & InstallationModeSilent) {
            installRemotePackageByUser();
            return;
        }
        emit canNotInstall(d->m_installFileNames.at(d->m_currentInstallFileName), result.errorString());
        disconnectTrk();
        return;
    }

    ++d->m_currentInstallFileName;

    if (d->m_currentInstallFileName == d->m_installFileNames.size())
        emit installingFinished();

    if (d->m_startupActions & ActionInstall && d->m_currentInstallFileName < d->m_installFileNames.size()) {
        installRemotePackage();
    } else if (d->m_startupActions & ActionRun) {
        startInferiorIfNeeded();
    } else if (d->m_startupActions & ActionDownload) {
        copyFileFromRemote();
    } else {
        disconnectTrk();
    }
}

QByteArray Launcher::startProcessMessage(const QString &executable,
                                         const QString &arguments)
{
    // It's not started yet
    QByteArray ba;
    appendShort(&ba, 0, TargetByteOrder); // create new process (kDSOSProcessItem)
    ba.append(char(0)); // options - currently unused
    // One string consisting of binary terminated by '\0' and arguments terminated by '\0'
    QByteArray commandLineBa = executable.toLocal8Bit();
    commandLineBa.append(char(0));
    if (!arguments.isEmpty())
        commandLineBa.append(arguments.toLocal8Bit());
    appendString(&ba, commandLineBa, TargetByteOrder, true);
    return ba;
}

QByteArray Launcher::readMemoryMessage(uint pid, uint tid, uint from, uint len)
{
    QByteArray ba;
    ba.reserve(11);
    ba.append(char(0x8)); // Options, FIXME: why?
    appendShort(&ba, len);
    appendInt(&ba, from);
    appendInt(&ba, pid);
    appendInt(&ba, tid);
    return ba;
}

QByteArray Launcher::readRegistersMessage(uint pid, uint tid)
{
    QByteArray ba;
    ba.reserve(15);
    ba.append(char(0)); // Register set, only 0 supported
    appendShort(&ba, 0); //R0
    appendShort(&ba, 16); // last register CPSR
    appendInt(&ba, pid);
    appendInt(&ba, tid);
    return ba;
}

void Launcher::startInferiorIfNeeded()
{
    emit startingApplication();
    if (d->m_session.pid != 0) {
        logMessage("Process already 'started'");
        return;
    }

    d->m_device->sendTrkMessage(TrkCreateItem, TrkCallback(this, &Launcher::handleCreateProcess),
                                startProcessMessage(d->m_fileName, d->m_commandLineArgs)); // Create Item
}

void Launcher::resumeProcess(uint pid, uint tid)
{
    QByteArray ba;
    appendInt(&ba, pid, BigEndian);
    appendInt(&ba, tid, BigEndian);
    d->m_device->sendTrkMessage(TrkContinue, TrkCallback(), ba, "CONTINUE");
}

// Acquire a device from SymbianDeviceManager, return 0 if not available.
Launcher *Launcher::acquireFromDeviceManager(const QString &serverName,
                                             QObject *parent,
                                             QString *errorMessage)
{
    SymbianUtils::SymbianDeviceManager *sdm = SymbianUtils::SymbianDeviceManager::instance();
    const QSharedPointer<trk::TrkDevice> device = sdm->acquireDevice(serverName);
    if (device.isNull()) {
        if (serverName.isEmpty())
            *errorMessage = tr("No device is connected. Please connect a device and try again.");
        else
            *errorMessage = tr("Unable to acquire a device for port '%1'. It appears to be in use.").arg(serverName);
        return 0;
    }
    // Wire release signal.
    Launcher *rc = new Launcher(trk::Launcher::ActionPingOnly, device, parent);
    connect(rc, SIGNAL(deviceDescriptionReceived(QString,QString)),
            sdm, SLOT(setAdditionalInformation(QString,QString)));
    connect(rc, SIGNAL(destroyed(QString)), sdm, SLOT(releaseDevice(QString)));
    return rc;
}

// Preliminary release of device, disconnecting the signal.
void Launcher::releaseToDeviceManager(Launcher *launcher)
{
    Q_ASSERT(launcher);

    SymbianUtils::SymbianDeviceManager *sdm = SymbianUtils::SymbianDeviceManager::instance();
    // Disentangle launcher and its device, remove connection from destroyed
    launcher->setCloseDevice(false);
    TrkDevice *device = launcher->trkDevice().data();
    launcher->disconnect(device);
    device->disconnect(launcher);
    launcher->disconnect(sdm);
    sdm->releaseDevice(launcher->trkServerName());
}

void Launcher::getRegistersAndCallStack(uint pid, uint tid)
{
    d->m_device->sendTrkMessage(TrkReadRegisters,
                                TrkCallback(this, &Launcher::handleReadRegisters),
                                Launcher::readRegistersMessage(pid, tid));
    d->m_crashReportState.fetchingStackPID = pid;
    d->m_crashReportState.fetchingStackTID = tid;
}

void Launcher::handleReadRegisters(const TrkResult &result)
{
    if(result.errorCode() || result.data.size() < (17*4)) {
        terminate();
        return;
    }
    const char* data = result.data.constData() + 1;
    d->m_crashReportState.registers.clear();
    d->m_crashReportState.stack.clear();
    for (int i=0;i<17;i++) {
        uint r = extractInt(data);
        data += 4;
        d->m_crashReportState.registers.append(r);
    }
    d->m_crashReportState.sp = d->m_crashReportState.registers.at(13);

    const ushort len = 1024 - (d->m_crashReportState.sp % 1024); //read to 1k boundary first
    const QByteArray ba = Launcher::readMemoryMessage(d->m_crashReportState.fetchingStackPID,
                                                      d->m_crashReportState.fetchingStackTID,
                                                      d->m_crashReportState.sp,
                                                      len);
    d->m_device->sendTrkMessage(TrkReadMemory, TrkCallback(this, &Launcher::handleReadStack), ba);
    d->m_crashReportState.sp += len;
}

void Launcher::handleReadStack(const TrkResult &result)
{
    if (result.errorCode()) {
        //error implies memory fault when reaching end of stack
        emit registersAndCallStackReadComplete(d->m_crashReportState.registers, d->m_crashReportState.stack);
        return;
    }

    const uint len = extractShort(result.data.constData() + 1);
    d->m_crashReportState.stack.append(result.data.mid(3, len));

    if (d->m_crashReportState.sp - d->m_crashReportState.registers.at(13) > 0x10000) {
        //read enough stack, stop here
        emit registersAndCallStackReadComplete(d->m_crashReportState.registers, d->m_crashReportState.stack);
        return;
    }
    //read 1k more
    const QByteArray ba = Launcher::readMemoryMessage(d->m_crashReportState.fetchingStackPID,
                                                      d->m_crashReportState.fetchingStackTID,
                                                      d->m_crashReportState.sp,
                                                      1024);
    d->m_device->sendTrkMessage(TrkReadMemory, TrkCallback(this, &Launcher::handleReadStack), ba);
    d->m_crashReportState.sp += 1024;
}

} // namespace trk